4.9. Configuring the Use of JDBC Connections

In its default configuration, Kodo obtains JDBC connections on an as-needed basis. Kodo EntityManagers and PersistenceManagers do not retain a connection to the database unless they are in a datastore transaction or there are open Extent iterators or Query results that are using a live JDBC result set. At all other times, including during optimistic transactions, EntityManagers and PersistenceManagers request a connection for each query, then immediately release the connection back to the pool.

In some cases, it may be more efficient to retain connections for longer periods of time. You can configure Kodo's use of JDBC connections through the kodo.ConnectionRetainMode configuration property. The property accepts the following values:

You can also specify the connection retain mode of individual EntityManagers and PersistenceManagers when you retrieve them from their respective factories. See Section 9.2.1, “OpenJPAEntityManagerFactory” and Section 9.3.1, “KodoPersistenceManagerFactory” for details.

The kodo.FlushBeforeQueries configuration property controls another aspect of connection usage: whether to flush transactional changes before executing object queries. This setting only applies to queries that would otherwise have to be executed in-memory because the IgnoreChanges property is set to false and the query may involve objects that have been changed in the current transaction. Legal values are:

The flush mode can also be varied at runtime using the Kodo fetch configuration API, discussed in Chapter 9, Runtime Extensions.

The table below describes the behavior of automatic flushing in various situations. In all cases, flushing will only occur if Kodo detects that you have made modifications in the current transaction that may affect the query's results.

Table 4.2. Kodo Automatic Flush Behavior

  FlushBeforeQueries = false FlushBeforeQueries = true FlushBeforeQueries = with-connection; ConnectionRetainMode = on-demand FlushBeforeQueries = with-connection; ConnectionRetainMode = transaction or always
IgnoreChanges = true no flushno flushno flushno flush
IgnoreChanges = false; no tx active no flushno flushno flushno flush
IgnoreChanges = false; datastore tx active no flushflushflushflush
IgnoreChanges = false; optimistic tx active no flushflush no flush unless flush has already been invoked flush

Example 4.12. Specifying Connection Usage Defaults

JPA XML format:

<property name="kodo.ConnectionRetainMode" value="on-demand"/>
<property name="kodo.FlushBeforeQueries" value="true"/>

JDO properties format:

kodo.ConnectionRetainMode: on-demand
kodo.FlushBeforeQueries: true

Example 4.13. Specifying Connection Usage at Runtime

JPA:

// obtaining an em with a certain connection retain mode
Map props = new HashMap();
props.put("kodo.ConnectionRetainMode", "always");
EntityManager em = emf.createEntityManager(props);

JDO:

import kodo.jdo.*;

... 

// obtaining a pm with a certain transaction and connection retain mode
KodoPersistenceManagerFactory kpmf = KodoJDOHelper.cast (pmf);
PersistenceManager pm = kpmf.getPersistenceManager (false, 
    KodoPersistenceManager.CONN_RETAIN_ALWAYS);

...

// changing the flush mode for an individual PersistenceManager
KodoFetchPlan fetch = (KodoFetchPlan) pm.getFetchPlan ();
fetch.setFlushBeforeQueries (KodoFetchPlan.QUERY_FLUSH_TRUE);

 

Skip navigation bar   Back to Top