JPOX
JPOX
JPOX  |  Ver 1.0  |  Ver 1.1  |  Ver 1.2  |  JDO  |  JPA  |  Project
Java Data Objects
JDO Transactions

As was seen in the JDO States guide, objects can have many different states during their lifecycle. In reality the user only sees a small subset of these. The state changes relative to the state of the transaction controlling the unit of work.

Making objects accessible outside a transaction

The most common thing that a system would want to do once it had persisted an object is be able to access it again outside of a persistence transaction. There are 3 ways that this can be achieved with JDO, and the selection of which one depends on what the system wants to do with the object.

  • Make the object transient. This is achieved by calling makeTransient on the object and it will migrate to the state transient. This doesn't affect the object that is in the datastore. What it does do however is it removes the identity from the object. If an attempt is made to persist this (transient) object later, a new entry will be added to the datastore. That is, the transient object is unconnected to the object that it was originally when first persisted.
  • Use the JDO 2 detach facility. This is achieved by calling detachCopy on the object and it will migrate to the state detached. This doesn't affect the object that is in the datastore. The difference to the first option is that this will retain the identity of the object. This is useful in that the object can then be modified, and later "attached" to the persistence graph and its changes will make their way into the datastore.
  • Use nontransactionalRead. The nontransactionalRead facility allows objects to be read outside a transaction. You need nontransactionalWrite to be enabled for any changes to these objects to then make their way to the datastore

As you can see from the 3 options, they all have their uses. The most useful is the second, because it retains the linkage to the original object and so the propagation of changes into the datastore is automatic when re-attaching. That said, there are situations where you may favour the first or third. It's important to understand the consequences of your choices.