Example usage for org.hibernate.resource.jdbc.spi PhysicalConnectionHandlingMode DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION

List of usage examples for org.hibernate.resource.jdbc.spi PhysicalConnectionHandlingMode DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION

Introduction

In this page you can find the example usage for org.hibernate.resource.jdbc.spi PhysicalConnectionHandlingMode DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION.

Prototype

PhysicalConnectionHandlingMode DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION

To view the source code for org.hibernate.resource.jdbc.spi PhysicalConnectionHandlingMode DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION.

Click Source Link

Document

The Connection will be acquired as soon as it is needed; it will be released after each transaction is completed.

Usage

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

License:Open Source License

public void doBeforeTransactionCompletion(SessionImplementor session) {
    if (workUnits.size() == 0) {
        return;/*from ww  w.  ja  v a  2 s .c o m*/
    }

    if (!session.getTransactionCoordinator().isActive()) {
        log.debug(
                "Skipping hibernate-audit transaction hook due to non-active (most likely marked as rollback) transaction");
        return;
    }

    if (FlushMode.MANUAL == session.getHibernateFlushMode() || session.isClosed()) {
        Session temporarySession = null;
        try {
            temporarySession = session.sessionWithOptions().connection().autoClose(false)
                    .connectionHandlingMode(
                            PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_AFTER_TRANSACTION)
                    .openSession();
            executeInSession(temporarySession);
            temporarySession.flush();
        } finally {
            if (temporarySession != null) {
                temporarySession.close();
            }
        }
    } else {
        executeInSession(session);

        // explicitly flushing the session, as the auto-flush may have already happened.
        session.flush();
    }
}