blob: cbdcbdb997ec7902da2a238e2df5ca522ed076cb [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>2.&nbsp; Using a Third-Party 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&nbsp;4.&nbsp; JDBC"><link rel="prev" href="ref_guide_dbsetup.html" title="Chapter&nbsp;4.&nbsp; JDBC"><link rel="next" href="ref_guide_dbsetup_sqlconn.html" title="3.&nbsp; Runtime Access to DataSource"></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">2.&nbsp;
Using a Third-Party DataSource
</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ref_guide_dbsetup.html">Prev</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;4.&nbsp;
JDBC
</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="ref_guide_dbsetup_sqlconn.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_thirdparty"></a>2.&nbsp;
Using a Third-Party DataSource
</h2></div></div></div><div class="toc"><dl><dt><span class="section"><a href="ref_guide_dbsetup_thirdparty.html#ref_guide_dbsetup_thirdparty_enlist">2.1.
Managed and XA DataSources
</a></span></dt></dl></div><a class="indexterm" name="d0e17420"></a><p>
You can use OpenJPA with any third-party <code class="classname">javax.sql.DataSource
</code>. There are multiple ways of telling OpenJPA about a <code class="classname">
DataSource</code>:
</p><div class="itemizedlist"><ul type="disc"><li><p>
<a class="indexterm" name="d0e17437"></a>
Set the <code class="classname">DataSource</code> into the map passed to <code class="methodname">
Persistence.createEntityManagerFactory</code> under the
<a href="ref_guide_conf_openjpa.html#openjpa.ConnectionFactory" title="5.9.&nbsp; openjpa.ConnectionFactory"><code class="literal">openjpa.ConnectionFactory
</code></a> key.
</p></li><li><p>
<a class="indexterm" name="d0e17454"></a>
Bind the <code class="classname">DataSource</code> into JNDI, and then specify its
location in the <code class="literal">jta-data-source</code> or
<code class="literal">non-jta-data-source</code> element of the
<a href="jpa_overview_persistence.html#jpa_overview_persistence_xml" title="1.&nbsp; persistence.xml">JPA XML format</a> (depending on
whether the <code class="classname">DataSource</code> is managed by JTA), or in the
<a href="ref_guide_conf_openjpa.html#openjpa.ConnectionFactoryName" title="5.11.&nbsp; openjpa.ConnectionFactoryName">
<code class="literal">openjpa.ConnectionFactoryName</code></a> property.
</p></li><li><p>
<a class="indexterm" name="d0e17481"></a>
Specify the full class name of the <code class="classname">DataSource</code>
implementation in the <a href="ref_guide_conf_openjpa.html#openjpa.ConnectionDriverName" title="5.7.&nbsp; openjpa.ConnectionDriverName">
<code class="literal">openjpa.ConnectionDriverName</code></a> property in place of a JDBC
driver. In this configuration OpenJPA will instantiate an instance of the named
class via reflection. It will then configure the <code class="classname">DataSource
</code> with the properties in the
<a href="ref_guide_conf_openjpa.html#openjpa.ConnectionProperties" title="5.18.&nbsp; openjpa.ConnectionProperties">
<code class="literal">openjpa.ConnectionProperties</code></a> setting.
</p></li></ul></div><p>
The features of OpenJPA's own <code class="classname">DataSource</code> can
also be used with third-party implementations. OpenJPA layers on top of the
third-party <code class="classname">DataSource</code> to provide the extra
functionality. To configure these features use the
<a href="ref_guide_conf_openjpa.html#openjpa.ConnectionFactoryProperties" title="5.14.&nbsp; openjpa.ConnectionFactoryProperties">
<code class="literal">openjpa.ConnectionFactoryProperties</code></a> property described
in the previous section.
</p><div class="example"><a name="ref_guide_dbsetup_thirdparty_ex"></a><p class="title"><b>Example&nbsp;4.2.&nbsp;
Properties File for a Third-Party DataSource
</b></p><div class="example-contents"><pre class="programlisting">
&lt;property name="openjpa.ConnectionDriverName" value="oracle.jdbc.pool.OracleDataSource"/&gt;
&lt;property name="openjpa.ConnectionProperties"
value="PortNumber=1521, ServerName=saturn, DatabaseName=solarsid, DriverType=thin"/&gt;
&lt;property name="openjpa.ConnectionFactoryProperties" value="QueryTimeout=5000"/&gt;
</pre></div></div><br class="example-break"><div class="section" lang="en"><div class="titlepage"><div><div><h3 class="title"><a name="ref_guide_dbsetup_thirdparty_enlist"></a>2.1.&nbsp;
Managed and XA DataSources
</h3></div></div></div><a class="indexterm" name="d0e17522"></a><a class="indexterm" name="d0e17527"></a><p>
<a class="indexterm" name="d0e17534"></a>
Certain application servers automatically enlist their <code class="classname"> DataSource
</code>s in global transactions. When this is the case, OpenJPA should not
attempt to commit the underlying connection, leaving JDBC transaction completion
to the application server. To notify OpenJPA that your third-party <code class="classname">
DataSource</code> is managed by the application server, use the
<code class="literal">jta-data-source</code> element of your <code class="filename">
persistence.xml</code> file or set the
<a href="ref_guide_conf_openjpa.html#openjpa.ConnectionFactoryMode" title="5.13.&nbsp; openjpa.ConnectionFactoryMode">
<code class="literal">openjpa.ConnectionFactoryMode</code></a> property to
<code class="literal">managed</code>.
</p><p>
Note that OpenJPA can only use managed <code class="classname">DataSource</code>s when
it is also integrating with the application server's managed transactions. Also
note that all XA <code class="classname">DataSource</code>s are enlisted, and you must
set this property when using any XA <code class="classname"> DataSource</code>.
</p><p>
When using a managed <code class="classname">DataSource</code>, you should also
configure a second unmanaged <code class="classname">DataSource</code> that OpenJPA can
use to perform tasks that are independent of the global transaction. The most
common of these tasks is updating the sequence table OpenJPA uses to generate
unique primary key values for your datastore identity objects. Configure the
second <code class="classname">DataSource</code> using the <code class="literal">non-jta-data-source
</code> <code class="filename">persistence.xml</code> element, or OpenJPA's various
"2" connection properties, such as <code class="literal">openjpa.ConnectionFactory2Name
</code> or <code class="literal">openjpa.Connection2DriverName</code>. These
properties are outlined in <a href="ref_guide_conf.html" title="Chapter&nbsp;2.&nbsp; Configuration">Chapter&nbsp;2, <i xmlns:xlink="http://www.w3.org/1999/xlink">
Configuration
</i></a>.
</p><div class="example"><a name="ref_guide_enterprise_xa_conf_ex"></a><p class="title"><b>Example&nbsp;4.3.&nbsp;
Managed DataSource Configuration
</b></p><div class="example-contents"><pre class="programlisting">
&lt;!-- managed DataSource --&gt;
&lt;jta-data-source&gt;java:/OracleXASource&lt;/jta-data-source&gt;
&lt;properties&gt;
&lt;!-- use OpenJPA's built-in DataSource for unmanaged connections --&gt;
&lt;property name="openjpa.Connection2UserName" value="scott"/&gt;
&lt;property name="openjpa.Connection2Password" value="tiger"/&gt;
&lt;property name="openjpa.Connection2URL" value="jdbc:oracle:thin:@CROM:1521:OpenJPADB"/&gt;
&lt;property name="openjpa.Connection2DriverName" value="oracle.jdbc.driver.OracleDriver"/&gt;
&lt;/properties&gt;
</pre></div></div><br class="example-break"></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ref_guide_dbsetup.html">Prev</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="ref_guide_dbsetup.html">Up</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="ref_guide_dbsetup_sqlconn.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter&nbsp;4.&nbsp;
JDBC
&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="manual.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;3.&nbsp;
Runtime Access to DataSource
</td></tr></table></div></body></html>