3.5.2. Using a Third-Party DataSource
You can use OpenJPA with any third-party javax.sql.DataSource.
There are multiple ways of telling OpenJPA about a DataSource:
-
Set the
DataSourceinto the map passed toPersistence.createEntityManagerFactoryunder theopenjpa.ConnectionFactorykey. -
Bind the
DataSourceinto JNDI, and then specify its location in thejta-data-sourceornon-jta-data-sourceelement of the JPA XML format (depending on whether theDataSourceis managed by JTA), or in theopenjpa.ConnectionFactoryNameproperty. -
Specify the full class name of the
DataSourceimplementation in theopenjpa.ConnectionDriverNameproperty in place of a JDBC driver. In this configuration OpenJPA will instantiate an instance of the named class via reflection. It will then configure theDataSourcewith the properties in theopenjpa.ConnectionPropertiessetting.
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.
<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"/>
You can also force the Apache Commons DBCP BasicDataSource to be used for connection pooling when provided on the classpath by substituting it as the ConnectionDriverName and setting ConnectionProperties=DriverClassName to the actual JDBC driver value -
<property name="openjpa.ConnectionDriverName" value="org.apache.commons.dbcp.BasicDataSource"/>
<property name="openjpa.ConnectionProperties"
value="DriverClassName=oracle.jdbc.pool.OracleDataSource, PortNumber=1521, ServerName=saturn, DatabaseName=solarsid, DriverType=thin, MaxIdle=0"/>
<property name="openjpa.ConnectionFactoryProperties" value="QueryTimeout=5000"/>