Example usage for javax.transaction Synchronization Synchronization

List of usage examples for javax.transaction Synchronization Synchronization

Introduction

In this page you can find the example usage for javax.transaction Synchronization Synchronization.

Prototype

Synchronization

Source Link

Usage

From source file:org.topazproject.otm.impl.AbstractSession.java

private void ensureTxActive(boolean start, int txTimeout) throws OtmException {
    if (jtaTxn != null)
        return;/*  w w  w  .  j  av a  2 s.co  m*/

    try {
        TransactionManager tm = getSessionFactory().getTransactionManager();

        jtaTxn = tm.getTransaction();
        if (jtaTxn == null) {
            if (!start)
                throw new OtmException("No active transaction");

            tm.setTransactionTimeout(txTimeout > 0 ? txTimeout : 0);
            tm.begin();
            jtaTxn = tm.getTransaction();
        }

        jtaTxn.registerSynchronization(new Synchronization() {
            public void beforeCompletion() {
                if (getFlushMode().implies(FlushMode.commit) && !isRollback())
                    flush();
            }

            public void afterCompletion(int status) {
                endTransaction();
            }

            private boolean isRollback() throws OtmException {
                try {
                    return (jtaTxn.getStatus() == Status.STATUS_MARKED_ROLLBACK);
                } catch (Exception e) {
                    throw new OtmException("Error getting rollback-only status", e);
                }
            }
        });
    } catch (OtmException oe) {
        throw oe;
    } catch (Exception e) {
        throw new OtmException("Error setting up transaction", e);
    }
}