| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>3. Runtime Access to DataSource</title><base href="display"><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL-NS Stylesheets V1.76.1"><link rel="home" href="manual.html" title="Apache OpenJPA 2.2 User's Guide"><link rel="up" href="ref_guide_dbsetup.html" title="Chapter 4. JDBC"><link rel="prev" href="ref_guide_dbsetup_thirdparty.html" title="2. Using a Third-Party DataSource"><link rel="next" href="ref_guide_dbsetup_dbsupport.html" title="4. Database Support"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">3. |
| Runtime Access to DataSource |
| </th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ref_guide_dbsetup_thirdparty.html">Prev</a> </td><th width="60%" align="center">Chapter 4. |
| JDBC |
| </th><td width="20%" align="right"> <a accesskey="n" href="ref_guide_dbsetup_dbsupport.html">Next</a></td></tr></table><hr></div><div class="section" title="3. Runtime Access to DataSource"><div class="titlepage"><div><div><h2 class="title" style="clear: both" id="ref_guide_dbsetup_sqlconn">3. |
| Runtime Access to DataSource |
| </h2></div></div></div> |
| |
| <a class="indexterm" name="d5e9705"></a> |
| <a class="indexterm" name="d5e9708"></a> |
| <p> |
| The JPA standard defines how to access JDBC connections from enterprise beans. |
| OpenJPA also provides APIs to retrieve a connection directly from the <code class="classname"> |
| EntityManagerFactory</code>'s <code class="classname">DataSource</code>. |
| </p> |
| <p> |
| The <code class="methodname">EntityManager.unwrap(java.sql.Connection.class)</code> method |
| returns an <code class="classname">EntityManager</code>'s connection. If the <code class="classname"> |
| EntityManager</code> does not already have a connection, it will obtain |
| one. The returned connection is only guaranteed to be transactionally consistent |
| with other <code class="classname">EntityManager</code> operations if the <code class="classname"> |
| EntityManager</code> is in a managed or non-optimistic transaction, if the |
| <code class="classname">EntityManager</code> has flushed in the current transaction, or |
| if you have used the <code class="methodname">OpenJPAEntityManager.beginStore</code> |
| method to ensure that a datastore transaction is in progress. Always close the |
| returned connection before attempting any other <code class="classname">EntityManager |
| </code> operations. OpenJPA will ensure that the underlying native |
| connection is not released if a datastore transaction is in progress. |
| </p> |
| <div class="example"><a name="ref_guide_dbsetup_conn_jpa"></a><p class="title"><b>Example 4.5. |
| Using the EntityManager's Connection |
| </b></p><div class="example-contents"> |
| |
| <pre class="programlisting"> |
| import java.sql.Connection; |
| import javax.persistence.EntityManager; |
| import javax.persistence.EntityManagerFactory; |
| |
| ... |
| |
| EntityManager em = emf.createEntityManager(); |
| Connection conn = (Connection) em.unwrap(java.sql.Connection.class); |
| |
| // do JDBC stuff |
| |
| conn.close(); |
| </pre> |
| </div></div><br class="example-break"> |
| <p> |
| The example below shows how to use a connection directly from the <code class="classname"> |
| DataSource</code>, rather than using an <code class="classname"> EntityManager |
| </code>'s connection. |
| </p> |
| <div class="example"><a name="ref_guide_dbsetup_conn_from_factory_jpa"></a><p class="title"><b>Example 4.6. |
| Using the EntityManagerFactory's DataSource |
| </b></p><div class="example-contents"> |
| |
| <pre class="programlisting"> |
| import java.sql.*; |
| import javax.sql.*; |
| import org.apache.openjpa.conf.*; |
| import org.apache.openjpa.persistence.*; |
| |
| ... |
| |
| OpenJPAEntityManagerFactory kemf = OpenJPAPersistence.cast(emf); |
| OpenJPAConfiguration conf = kemf.getConfiguration(); |
| DataSource dataSource = (DataSource) conf.getConnectionFactory(); |
| Connection conn = dataSource.getConnection(); |
| |
| // do JDBC stuff |
| |
| conn.close(); |
| </pre> |
| </div></div><br class="example-break"> |
| </div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ref_guide_dbsetup_thirdparty.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ref_guide_dbsetup.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ref_guide_dbsetup_dbsupport.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">2. |
| Using a Third-Party DataSource |
| </td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top"> 4. |
| Database Support |
| </td></tr></table></div></body></html> |