Example usage for org.hibernate.engine.spi SessionImplementor sessionWithOptions

List of usage examples for org.hibernate.engine.spi SessionImplementor sessionWithOptions

Introduction

In this page you can find the example usage for org.hibernate.engine.spi SessionImplementor sessionWithOptions.

Prototype

SharedSessionBuilder sessionWithOptions();

Source Link

Document

Obtain a Session builder with the ability to grab certain information from this session.

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 . java  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();
    }
}