Example usage for org.hibernate.engine.transaction.jta.platform.spi JtaPlatform retrieveTransactionManager

List of usage examples for org.hibernate.engine.transaction.jta.platform.spi JtaPlatform retrieveTransactionManager

Introduction

In this page you can find the example usage for org.hibernate.engine.transaction.jta.platform.spi JtaPlatform retrieveTransactionManager.

Prototype

TransactionManager retrieveTransactionManager();

Source Link

Document

Locate the TransactionManager

Usage

From source file:com.googlecode.hibernate.audit.synchronization.AuditSynchronization.java

License:Open Source License

private boolean isMarkedForRollback(Session session) {
    JtaPlatform jtaPlatform = ((SessionFactoryImplementor) auditedSession.getSessionFactory()).getSettings()
            .getJtaPlatform();//w ww . j  a v  a  2  s .com
    TransactionManager manager = null;
    if (jtaPlatform != null) {
        manager = jtaPlatform.retrieveTransactionManager();
    }
    if (manager != null) {
        try {
            int status = manager.getStatus();
            if (status == Status.STATUS_MARKED_ROLLBACK || status == Status.STATUS_ROLLING_BACK
                    || status == Status.STATUS_ROLLEDBACK) {
                return true;
            }
        } catch (SystemException e) {
            throw new TransactionException("Unable to get the transaction status.", e);
        }
    } else {
        return session.getTransaction().wasRolledBack();
    }

    return false;
}

From source file:com.googlecode.hibernate.audit.synchronization.work.AbstractAuditWorkUnit.java

License:Open Source License

private HibernateException createAuditLogicalGroup(Session session, AuditLogicalGroup logicalGroup,
        AuditType auditType) {//from  ww  w .  j a va  2  s  .c o m
    Session newSession = null;

    TransactionManager txManager = null;
    javax.transaction.Transaction suspendedTransaction = null;

    try {
        JtaPlatform jtaPlatform = ((SessionFactoryImplementor) session.getSessionFactory()).getSettings()
                .getJtaPlatform();
        if (jtaPlatform != null) {
            txManager = jtaPlatform.retrieveTransactionManager();
        }

        if (txManager != null) {
            try {
                suspendedTransaction = txManager.suspend();
            } catch (SystemException e) {
                throw new HibernateException(e);
            }
        }
        Transaction tx = null;
        try {
            newSession = session.getSessionFactory().openSession();
            tx = newSession.beginTransaction();
            logicalGroup.setAuditType(auditType);
            // when we are creating the audit logical group for the first time we set it to 0, this is before even we may have transaction record - this is executed in separate transaction
            logicalGroup.setLastUpdatedAuditTransactionId(Long.valueOf(0));
            newSession.save(logicalGroup);
            tx.commit();

            return null;
        } catch (HibernateException e) {
            if (log.isDebugEnabled()) {
                // log the exception is debug level because this most likely
                // will indicate that there was a concurrent insert and we
                // are prepared to handle such calls. If this is not the
                // case and we want to troubleshoot where is the problem
                // then at least log the exception is DEBUG level so we can
                // see it.
                log.debug(
                        "HibernateException occured while creating a new AuditLogicalGroup in new transaction",
                        e);
            }
            if (tx != null) {
                try {
                    tx.rollback();
                } catch (HibernateException ignored) {
                }
            }

            return e;
        } finally {
            if (newSession != null && newSession.isOpen()) {
                try {
                    newSession.close();
                } catch (HibernateException ignored) {
                }
            }
        }
    } finally {
        if (txManager != null && suspendedTransaction != null) {
            try {
                txManager.resume(suspendedTransaction);
            } catch (SystemException e) {
                throw new HibernateException(e);
            } catch (InvalidTransactionException e) {
                throw new HibernateException(e);
            }
        }
    }
}

From source file:org.babyfish.springframework.orm.hibernate.SpringXSessionContext.java

License:Open Source License

public SpringXSessionContext(XSessionFactoryImplementor sessionFactory) {
    this.sessionFactory = sessionFactory;
    JtaPlatform jtaPlatform = sessionFactory.getServiceRegistry().getService(JtaPlatform.class);
    TransactionManager transactionManager = jtaPlatform.retrieveTransactionManager();
    this.jtaSessionContext = (transactionManager != null ? new SpringJtaXSessionContext(sessionFactory) : null);
}

From source file:org.codehaus.groovy.grails.orm.hibernate.GrailsSessionContext.java

License:Apache License

public void initJta() {
    JtaPlatform jtaPlatform = sessionFactory.getServiceRegistry().getService(JtaPlatform.class);
    TransactionManager transactionManager = jtaPlatform.retrieveTransactionManager();
    jtaSessionContext = transactionManager == null ? null : new SpringJtaSessionContext(sessionFactory);
}

From source file:org.springframework.orm.hibernate4.fix.SpringSessionContext.java

License:Apache License

/**
 * Create a new SpringSessionContext for the given Hibernate SessionFactory.
 * @param sessionFactory the SessionFactory to provide current Sessions for
 *//*from  w w w  .  j a  v  a 2s .c  o m*/
public SpringSessionContext(SessionFactoryImplementor sessionFactory) {
    this.sessionFactory = sessionFactory;
    JtaPlatform jtaPlatform = sessionFactory.getServiceRegistry().getService(JtaPlatform.class);
    TransactionManager transactionManager = jtaPlatform.retrieveTransactionManager();
    this.jtaSessionContext = (transactionManager != null ? new SpringJtaSessionContext(sessionFactory) : null);
}

From source file:org.springframework.orm.hibernate5.SpringSessionContext.java

License:Apache License

/**
 * Create a new SpringSessionContext for the given Hibernate SessionFactory.
 * @param sessionFactory the SessionFactory to provide current Sessions for
 *//*from w ww .  java2  s  .  c om*/
public SpringSessionContext(SessionFactoryImplementor sessionFactory) {
    this.sessionFactory = sessionFactory;
    try {
        JtaPlatform jtaPlatform = sessionFactory.getServiceRegistry().getService(JtaPlatform.class);
        this.transactionManager = jtaPlatform.retrieveTransactionManager();
        if (this.transactionManager != null) {
            this.jtaSessionContext = new SpringJtaSessionContext(sessionFactory);
        }
    } catch (Exception ex) {
        LogFactory.getLog(SpringSessionContext.class)
                .warn("Could not introspect Hibernate JtaPlatform for SpringJtaSessionContext", ex);
    }
}