| <html><head> |
| <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
| <title>3. Runtime Access to DataSource</title><link rel="stylesheet" href="css/docbook.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.72.0"><link rel="start" href="manual.html" title="Apache OpenJPA 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" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="ref_guide_dbsetup_sqlconn"></a>3. |
| Runtime Access to DataSource |
| </h2></div></div></div><a class="indexterm" name="d0e17441"></a><a class="indexterm" name="d0e17446"></a><p> |
| The JPA standard defines how to access JDBC connections from enterprise beans. |
| OpenJPA also provides APIs to access an <code class="classname">EntityManager</code>'s |
| connection, or to retrieve a connection directly from the <code class="classname"> |
| EntityManagerFactory</code>'s <code class="classname">DataSource</code>. |
| </p><p> |
| The |
| <a xmlns:xlink="http://www.w3.org/1999/xlink" href="../javadoc/org/apache/openjpa/persistence/OpenJPAEntityManager.html" target="_top"> |
| <code class="methodname">OpenJPAEntityManager.getConnection</code></a> 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.4. |
| Using the EntityManager's Connection |
| </b></p><div class="example-contents"><pre class="programlisting"> |
| import java.sql.*; |
| import org.apache.openjpa.persistence.*; |
| |
| ... |
| |
| OpenJPAEntityManager kem = OpenJPAPersistence.cast(em); |
| Connection conn = (Connection) kem.getConnection(); |
| |
| // 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.5. |
| 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> |