4.2. Using a Third-Party DataSource

You can use Kodo with any third-party
javax.sql.DataSource. There are multiple ways
of telling Kodo about a DataSource:

Set the DataSource into the map passed
to Persistence.createEntityManagerFactory
under the kodo.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
kodo.ConnectionFactoryName property.

Specify the full class name of the
DataSource implementation in the
kodo.ConnectionDriverName property
in place of a JDBC driver. In this configuration Kodo will
instantiate an instance of the named class via reflection. It
will then configure the DataSource with
the properties in the kodo.ConnectionProperties setting.

Some advanced features of Kodo's own DataSource
can also be used with third-party implementations. Kodo layers on top
of the third-party DataSource to provide the
extra functionality. To configure these advanced features, including
prepared statement caching, use the
kodo.ConnectionFactoryProperties property
described in the previous section.

4.2.1. Managed and XA DataSources

Certain application servers automatically enlist their
DataSources in global transactions. When this is the
case, Kodo should not attempt to commit the underlying connection,
leaving JDBC transaction completion to the application server. To
notify Kodo 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 kodo.ConnectionFactoryMode property to
managed.

When using a managed DataSource, you
should also configure a second unmanaged
DataSource that Kodo can use to perform
tasks that are independent of the global transaction.
The most common of these tasks is updating the sequence table Kodo
uses to generate unique primary key values for your
datastore identity objects. Configure the second
DataSource just as the first, but use the
non-jta-data-source element of your
persistence.xml or the
various "2" connection properties, such as
kodo.ConnectionFactory2Name
or kodo.Connection2DriverName.
These properties are outlined in Chapter 2, Configuration.