You can use OpenJPA with any third-party javax.sql.DataSource
. There are multiple ways of telling OpenJPA about a
DataSource
:
Set the DataSource
into the map passed to
Persistence.createEntityManagerFactory
under the
openjpa.ConnectionFactory
key.
Bind the DataSource
into JNDI, and then specify its
location in the jta-data-source
or
non-jta-data-source
element of the
JPA XML format (depending on
whether the DataSource
is managed by JTA), or in the
openjpa.ConnectionFactoryName
property.
Specify the full class name of the DataSource
implementation in the
openjpa.ConnectionDriverName
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 DataSource
with the properties in the
openjpa.ConnectionProperties
setting.
The features of OpenJPA's own DataSource
can
also be used with third-party implementations. OpenJPA layers on top of the
third-party DataSource
to provide the extra
functionality. To configure these features use the
openjpa.ConnectionFactoryProperties
property described
in the previous section.
Example 4.2. Properties File for a Third-Party DataSource
<property name="openjpa.ConnectionDriverName" value="oracle.jdbc.pool.OracleDataSource"/> <property name="openjpa.ConnectionProperties" value="PortNumber=1521, ServerName=saturn, DatabaseName=solarsid, DriverType=thin"/> <property name="openjpa.ConnectionFactoryProperties" value="QueryTimeout=5000"/>
Certain application servers automatically enlist their DataSource
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
DataSource
is managed by the application server, use the
jta-data-source
element of your
persistence.xml
file or set the
openjpa.ConnectionFactoryMode
property to
managed
.
Note that OpenJPA can only use managed DataSource
s when
it is also integrating with the application server's managed transactions. Also
note that all XA DataSource
s are enlisted, and you must
set this property when using any XA DataSource
.
When using a managed DataSource
, you should also
configure a second unmanaged DataSource
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 DataSource
using the non-jta-data-source
persistence.xml
element, or OpenJPA's various
"2" connection properties, such as openjpa.ConnectionFactory2Name
or openjpa.Connection2DriverName
. These
properties are outlined in Chapter 2,
Configuration
.
Example 4.3. Managed DataSource Configuration
<!-- managed DataSource --> <jta-data-source>java:/OracleXASource</jta-data-source> <properties> <!-- use OpenJPA's built-in DataSource for unmanaged connections --> <property name="openjpa.Connection2UserName" value="scott"/> <property name="openjpa.Connection2Password" value="tiger"/> <property name="openjpa.Connection2URL" value="jdbc:oracle:thin:@CROM:1521:OpenJPADB"/> <property name="openjpa.Connection2DriverName" value="oracle.jdbc.driver.OracleDriver"/> </properties>