JPOX
JPOX
JPOX  |  Version 1.0  |  Version 1.1  |  Version 1.2  |  JDO  |  JPA 
1.2 | Persistence | JDO ORM | JPA ORM | Query | Runtime | Extensions | Tutorials and Examples
JDO Runtime
JPA Runtime
JPOX Runtime
RDBMS Datastores
DB4O Datastore
JDO Transactions

A Transaction forms a unit of work. The Transaction manages what happens within that unit of work, and when an error occurs the Transaction can roll back any changes performed. Transactions can be managed by the users application, or can be managed by a framework (such as Spring), or can be managed by a J2EE container. These are described below.

See also :-

Locally Managed Transactions

When using a JDO implementation such as JPOX in a normal Java environment (with no J2EE container), the Transactions are known as Locally Managed Transactions. The users code (or a framework such as Spring) will manage the transactions by starting, and commiting the transaction itself. With these transactions you would do something like

PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try
{
    tx.begin();
    
    {users code to persist objects}
    
    tx.commit();
}
finally
{
    if (tx.isActive())
    {
        tx.rollback();
    }
}
pm.close();

When you use a framework like Spring you would not need to specify the tx.begin(), tx.commit(), tx.rollback() since that would be done for you. The basic idea with Locally Managed transactions is that you are managing the transaction start and end.



Container Managed Transactions

When using a J2EE container, and using the JCA adapter for JPOX, you are giving over control of the transactions to the container. Here you have Container Managed Transactions. In terms of your code, you would do like the previous example except that you would OMIT the tx.begin(), tx.commit(), tx.rollback() since the J2EE container will be doing this for you.



No Transactions

JDO allows the ability to operate without transactions. This can be enabled by setting the 2 properties javax.jdo.option.NontransactionalRead, javax.jdo.option.NontransactionalWrite to true. JPOX supports the first of these properties allowing the ability to read fields of persisted objects outside of transactions. The important thing is that to use this mode of operation, you must enable it by setting the property.



Notification on Transaction Commit or Rollback

There are situations where you may want to get notified that a transaction is in course of being committed or rolling back. To make that happen, you would do something like

PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try
{
    tx.begin();

    tx.setSynchronization(new javax.transaction.Synchronization()
    {
        public void beforeCompletion()
        {
             // before commit or rollback
        }

        public void afterCompletion(int status)
        {
            if (status == javax.transaction.Status.STATUS_ROLLEDBACK)
            {
                // rollback
            }
            else if (status == javax.transaction.Status.STATUS_COMMITTED)
            {
                // commit
            }
        }
    });
    
    tx.commit();
}
finally
{
    if (tx.isActive())
    {
        tx.rollback();
    }
}
pm.close();