JPOX
JPOX
 JPOX Version 1.0
Configuration | Tutorials | Worked Examples | Developer
Obtaining JPOX
JPOX Preparation
JPOX Runtime
JDO Transactions

JDO supports 2 forms of transactions. JPOX supports both of these options. These are described below.

Pessimistic Transactions

Pessimistic transactions are the default in JDO. They are suitable for short lived operations where no user interaction is taking place and so it is possible to block access to datastore entities for the duration of the transaction.

To give a framework for the typical pessimistic transaction

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

    // Do some work persisting objects and updating objects.
    // Typically short lived operation

    tx.commit();
}
catch (Exception e)
{
    // Handle the exception
}
finally
{
    if (tx.isActive())
    {
        tx.rollback();
    }
}
                
Optimistic Transactions

Optimistic transactions are the other option in JDO. They are suitable for longer lived operations maybe where user interaction is taking place and where it would be undesirable to block access to datastore entities for the duration of the transaction. The assumption is that data altered in this transaction will not be updated by other transactions during the duration of this transaction. The data is checked just before commit to ensure the integrity in this respect.

Rather than placing version/timestamp columns on all user datastore tables, JPOX allows the user to notate particular classes as requiring optimistic treatment. This is performed by adding JPOX extension Meta-Data tags to the Meta-Data of the classes concerned, for example

<extension vendor-name="jpox" key="optimistic-mode" value="Timestamp"/>
<extension vendor-name="jpox" key="optimistic-column-name" value="TIMESTAMP"/>
                

The second is optional and allows the user to define the name of the column to use for the optimistic column. The first tells JPOX to allow optimistic transaction capability on this class and to use "Timestamp" as the means of checking for updates.

To give a framework for the typical optimistic transaction

Transaction tx=pm.currentTransaction();
tx.setOptimistic(true);
try
{
    tx.begin();

    // Do some work that may take some time
    // e.g waiting for user input etc.

    tx.commit();
}
catch (Exception e)
{
    // Handle the exception
}
finally
{
    if (tx.isActive())
    {
        tx.rollback();
    }
}
                
Operation without 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.