9.2. The JDO Transaction Interface
Prev | Chapter 9. Transaction | Next

## 9.2. The JDO Transaction Interface

The `Transaction` interface controls transactions in JDO. This interface consists of "getter" and "setter" methods for several Java bean-style properties, standard transaction demarcation methods, and a method to test whether there is a transaction in progress.

    public boolean getNontransactionalRead ();
    public void setNontransactionalRead (boolean read);
    public boolean getNontransactionalWrite ();
    public void setNontransactionalWrite (boolean write);
    public boolean getRetainValues ();
    public void setRetainValues (boolean retain);
    public boolean getRestoreValues ();
    public void setRestoreValues (boolean restore);
    public boolean getOptimistic ();
    public void setOptimistic (boolean optimistic);
    public Synchronization getSynchronization ();
    public void setSynchronization (Synchronization synch);

The `Transaction`'s `NonTransactionalRead, NonTransactionalWrite, RetainValues, RestoreValues,` and `Optimistic` properties mirror those presented in the section on `PersistenceManagerFactory` settings. The final `Transaction` property, `Synchronization`, has not been covered yet. This property enables you to associate a `javax.transaction.Synchronization` instance with the `Transaction`. The `Transaction` will notify your `Synchronization` instance on transaction completion events, so that you can implement custom behavior on commit or rollback. See the `javax.transaction.Synchronization` Javadoc for details.

    public void begin ();
    public void commit ();
    public void rollback ();

The `begin`, `commit`, and `rollback` methods demarcate transaction boundaries. The methods should be self-explanatory: `begin` starts a transaction, `commit` attempts to commit the transaction's changes to the data store, and `rollback` aborts the transaction, in which case the data store is "rolled back" to its previous state. JDO implementations will automatically roll back transactions if any fatal exception is thrown during the commit process. Otherwise, it is up to you to roll back the transaction to free its resources.

    public boolean isActive ();

Finally, the `isActive` method returns `true` if the transaction is in progress (`begin` has been called more recently than `commit` or `rollback`), and `false` otherwise.

Example 9.1. Grouping Operations with Transactions

    public void transferFunds (User from, User to, double amnt)
    {
        PersistenceManager pm = JDOHelper.getPersistenceManager (from);
        Transaction trans = pm.currentTransaction ();
        trans.begin ();
        try
        {
            from.decrementAccount (amnt);
            to.incrementAccount (amnt);
            trans.commit ();
        }
        catch (JDOFatalException jfe)  // trans is already rolled back
        {
            throw jfe;
        }
        catch (RuntimeException re)    // includes non-fatal JDO exceptions
        {
            trans.rollback ();         // or could attempt to fix error and retry
            throw re;
        }
    }

Prev | Up | Next
Chapter 9. Transaction | Home | Chapter 10. Extent