JPOX
JPOX
 Project  |  Ver 1.1  |  Ver 1.2  |  JDO  |  JPA  |  Guides  |  Tools
1.2 | Persistence | JDO ORM | JPA ORM | Runtime | JDO Runtime | JPA Runtime | Extensions | Developer
JPOX Runtime
RDBMS Datastores
DB4O Datastore
RDBMS Queries

Using an RDBMS datastore JPOX allows you to query the objects in the datastore using the following

  • JDOQL - language based around the objects that are persisted and using Java-type syntax
  • SQL - language found on alomst all RDBMS. This is clearly of little use when using an object datastore for persistence
  • JPOXSQL - language that is based on SQL but allowing some reference to objects
  • JPQL - language defined in the JPA1 specification for JPA persistence which closely mirrors SQL.

When using queries with RDBMS there are some specific situations where it can be useful to benefit from special treatment. These are listed here.

Query Timeouts

JPOX provides a useful extension allowing control over the timeout of the query. So, for example, if you have a query that can cause problems in terms of the time taken, you can set a timeout on the query to retain the usability of your application.

With JDO2 you can set this on a per-Query basis by specifying the query "extension" org.jpox.query.timeout setting it to the value (in seconds) that you want the timeout to be. With JDO/JPA you can apply it to all queries via a persistence property org.jpox.query.timeout



Result Set Type

java.sql.ResultSet defines three possible result set types.

  • forward-only : the result set is navegatble forwards only
  • scroll-sensitive : the result set is scrollable in both directions and is sensitive to changes in the datastore
  • scroll-insensitive : the result set is scrollable in both directions and is insensitive to changes in the datastore

JPOX allows specification of this type as a query extension.

To do this on a per query basis for JDO2 you would do

query.addExtension("org.jpox.rdbms.query.resultSetType", "scroll-insensitive");

To do this on a per query basis for JPA1 you would do

query.setHint("org.jpox.rdbms.query.resultSetType", "scroll-insensitive");

The default is forward-only. The benefit of the other two is that the result set will be scrollable and hence objects will only be read in to memory when accessed. So if you have a large result set you should set this to one of the scrollable values.



Result Set Caching of Results

When using a "scrollable" result set, see above for org.jpox.rdbms.query.resultSetType by default the query result will cache the rows that have been read. You can control this caching to optimise it for your memory requirements. You can set the query extension org.jpox.query.resultCacheType and it has the following possible values

  • weak : use a weak hashmap for caching (default)
  • soft : use a soft reference map for caching
  • hard : use a HashMap for caching (objects not garbage collected)
  • none : no caching (hence uses least memory)

To set this on a per query basis for JDO2 you would do

query.addExtension("org.jpox.query.resultCacheType", "weak");

To do this on a per query basis for JPA1 you would do

query.setHint("org.jpox.query.resultCacheType", "weak");

This is present in JPOX after 1.2.0



Result Set Control

JPOX provides a useful extension allowing control over the ResultSet's that are created by queries. You have at your convenience some properties that give you the power to control whether the result set is read only, whether it can be read forward only, the direction of fetching etc.

To do this on a per query basis for JDO2 you would do

query.addExtension("org.jpox.rdbms.query.fetchDirection", "forward");
query.addExtension("org.jpox.rdbms.query.resultSetConcurrency", "read-only");

To do this on a per query basis for JPA1 you would do

query.setHint("org.jpox.rdbms.query.fetchDirection", "forward");
query.setHint("org.jpox.rdbms.query.resultSetConcurrency", "read-only");

Alternatively you can specify these as persistence properties so that they apply to all queries for that PMF/EMF. Again, the properties are

  • org.jpox.rdbms.query.fetchDirection - controls the direction that the ResultSet is navigated. By default this is forwards only. Use this property to change that.
  • org.jpox.rdbms.query.resultSetConcurrency - controls whether the ResultSet is read only or updateable.

Bear in mind that not all RDBMS support all of the possible values for these options. That said, they do add a degree of control that is often useful.



Flush changes before execution

When using optimistic transactions all updates to data are held until flush()/commit(). This means that executing a query may not take into account changes made during that transaction in some objects. JPOX allows a convenience of calling flush() just before execution of queries so that all updates are taken into account. The property name is org.jpox.query.flushBeforeExecution and defaults to "false".

To do this on a per query basis for JDO2 you would do

query.addExtension("org.jpox.query.flushBeforeExecution","true");

To do this on a per query basis for JPA1 you would do

query.setHint("org.jpox.query.flushBeforeExecution","true");

You can also specify this for all queries using a persistence property org.jpox.query.flushBeforeExecution which would then apply to ALL queries for that PMF/EMF.