Datastore Connections

DataNucleus utilises connections to the datastore differently depending on the transaction type.

For pessimistic (or datastore) transactions when begin() is called on the transaction, a connection will be obtained to the datastore. This datastore connection will be held for the duration of the transaction until such time as either commit() or rollback() are called.

For optimistic transactions things are a little more complicated. The call to begin() has no effect with respect to datastore connections. All updates to the datastore are delayed until flush() or commit() and so no connection is obtained until that time. When flush() is called, or the transaction committed a datastore connection is finally obtained and it is held open until commit/rollback completes. when a datastore operation is required. The connection is typically released after performing that operation. So datastore connections, in general, are held for much smaller periods of time. This is complicated slightly by use of the PMF property java.jdo.IgnoreCache . When this is set to false , the connection, once obtained, is not released until the call to commit()/rollback().

When the operation is outside of a transaction, where nontransactionalRead is enabled, the Connection is allocated when the operation starts, and closed when it ends for the particular PersistenceManager. This can result in "ConnectionInUse" problems where another operation on another thread comes in and tries to perform something while that first operation is still in use. This happens because the JDO spec requires an implementation to use a single datastore connection at any one time. When this situation crops up the user ought to use multiple PersistenceManagers.

Another important aspect is use of queries for Optimistic transactions, or for non-transactional contexts. In these situations it isn't possible to keep the datastore connection open indefinitely and so when the Query is executed the ResultSet is then read into core making the queried objects available thereafter.

Occasionally systems may need access to the datastore directly. JDO2 provides an accessor for obtaining a connection to the datastore. You obtain a connection as follows

User Connection

JDO2 defines a mechanism for users to access the native connection to the datastore, so that they can perform other operations as necessary. You obtain a connection as follows (for RDBMS)

// Obtain the connection from the JDO implementation
JDOConnection conn = pm.getDataStoreConnection();
try
{
    java.sql.Connection sqlConn = (java.sql.Connection)conn;
    
    ... use the "sqlConn" connection to perform some operations.
}
finally
{
    // Hand the connection back to the JDO implementation
    conn.close();
}

and for DB4O this would be

// Obtain the connection from the JDO implementation
JDOConnection conn = pm.getDataStoreConnection();
try
{
    com.db4o.ObjectContainer objContainer = (com.db4o.ObjectContainer)conn.getNativeConnection();

    ... use the "objContainer" connection to perform some operations.
}
finally
{
    // Hand the connection back to the JDO implementation
    conn.close();
}

The "JDOConnection" in the case of DataNucleus is a wrapper to the native connection for the type of datastore being used. You now have a connection allowing direct access to the datastore. Things to bear in mind with this connection

  • You must return the connection back to the PersistenceManager before performing any JDO PM operation. You do this by calling conn.close()
  • If you don't return the connection and try to perform a JDO PM operation which requires the connection then a JDOUserException is thrown.