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();