12.5 Transactions


Early in the chapter, I introduced some basic transactional semantics in the form of a JDO transaction object that enables you to begin, commit, and roll back a transaction. In reality, JDO does not require you to operate in a transactional context. If your environment supports transactions, however, it is a good idea to leverage this functionality since transactions are ultimately the key factor in long- term data integrity.

12.5.1 The Transaction Class

Each PersistenceManager provides exactly one Transaction to manage transactions. In other words, for operations against a given PersistenceManager , you can execute at most one transaction at a time.

JDO supports two transaction types:

  • Data store transaction management

  • Optimistic transaction management

Data store transaction management is where JDO lets the underlying data store manage your transactions. From the time you begin the transaction until the time you commit or roll back, JDO has an open transaction ”and all overhead such as locks associated with it ”in the data store. The data store performs and commits or roll backs.

Under optimistic transaction management, a JDO implementation manages transactional integrity locally until a commit or rollback is issued. For a commit, the implementation verifies the data integrity, opens a transaction with the data store, and sends all changes at once.

Telling a transaction to use optimistic or data store transaction management is a single method call:

 trans.setOptimistic(true); 

Passing in true turns on optimistic transaction management, whereas passing in false turns on data store transaction management. Some JDO implementations will not support optimistic transaction management. In those cases, this method will throw a JDOUnsupportedOptionException .

12.5.2 Managed Versus Nonmanaged Environments

JDO applications can run either in managed or nonmanaged environments. So far in this chapter, the examples have assumed a nonmanaged environment. Managed or nonmanaged simply refers to whether the JDO environment leverages some kind of external transaction management system to manage its transactions. The most common managed environment you will encounter is using JDO in a J2EE application server to provide bean-managed persistence to Enterprise JavaBeans.

In a managed environment, your application cannot make calls to commit and roll back transactions. Any attempt to do so will result in a JDOUserException . Beginning, committing, and rolling back transactions are the job of the application server.



Java Database Best Practices
Java Database Best Practices
ISBN: 0596005229
EAN: 2147483647
Year: 2003
Pages: 102
Authors: George Reese

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net