8.13. Connection Access

public JDOConnection getConnection ();

The getConnection method returns the datastore connection in use by the PersistenceManager. If the PersistenceManager does not have a connection already, it obtains one. The returned object implements JDOConnection, which provides a single method:

Object getNativeConnection ();

This method returns the underlying datastore connection, free from all decorators. Using the raw connection is dangerous, and should be avoided when possible. Instead, cast the JDOConnection to the proper connection type. For example, in a relational JDO implementation, the JDOConnection will also implement java.sql.Connection.

If a JDO datastore transaction is in progress, then the returned connection will be transactionally consistent. If an optimistic transaction is in progress, the connection returned is only guaranteed to be transactionally consistent if the PersistenceManager has already flushed during the transaction. See Section 9.1, “Transaction Types” for descriptions of optimistic and pessimistic transaction types.


Kodo's PersistenceManagers expose a a beginStore method you can use to ensure that a datastore transaction is in progress without flushing. See the method Javadoc for details.

Always close the returned connection (but not the underlying native connection!) before attempting any other PersistenceManager operations. The JDO implementation will ensure that the native connection is not released if a transaction is in progress.

Example 8.5. Using the PersistenceManager's Connection

import java.sql.*;


PersistenceManager pm = ...;
Connection conn = (Connection) pm.getConnection ();

// do JDBC stuff

conn.close ();


Skip navigation bar   Back to Top