Search in sources :

Example 81 with SessionImplementor

use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.

the class EvictAuditDataAfterCommitTest method checkEmptyAuditSessionCache.

private void checkEmptyAuditSessionCache(Session session, String... auditEntityNames) {
    List<String> entityNames = Arrays.asList(auditEntityNames);
    PersistenceContext persistenceContext = ((SessionImplementor) session).getPersistenceContext();
    for (Map.Entry<Object, EntityEntry> entrySet : persistenceContext.reentrantSafeEntityEntries()) {
        final EntityEntry entityEntry = entrySet.getValue();
        if (entityNames.contains(entityEntry.getEntityName())) {
            assert false : "Audit data shall not be stored in the session level cache. This causes performance issues.";
        }
        Assert.assertFalse("Revision entity shall not be stored in the session level cache. This causes performance issues.", SequenceIdRevisionEntity.class.getName().equals(entityEntry.getEntityName()));
    }
}
Also used : EntityEntry(org.hibernate.engine.spi.EntityEntry) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Map(java.util.Map)

Example 82 with SessionImplementor

use of org.hibernate.engine.spi.SessionImplementor in project spring-framework by spring-projects.

the class HibernateTransactionManager method doCleanupAfterCompletion.

@Override
@SuppressWarnings("deprecation")
protected void doCleanupAfterCompletion(Object transaction) {
    HibernateTransactionObject txObject = (HibernateTransactionObject) transaction;
    // Remove the session holder from the thread.
    if (txObject.isNewSessionHolder()) {
        TransactionSynchronizationManager.unbindResource(getSessionFactory());
    }
    // Remove the JDBC connection holder from the thread, if exposed.
    if (getDataSource() != null) {
        TransactionSynchronizationManager.unbindResource(getDataSource());
    }
    Session session = txObject.getSessionHolder().getSession();
    if (this.prepareConnection && isPhysicallyConnected(session)) {
        // Else, we need to rely on the connection pool to perform proper cleanup.
        try {
            Connection con = ((SessionImplementor) session).connection();
            Integer previousHoldability = txObject.getPreviousHoldability();
            if (previousHoldability != null) {
                con.setHoldability(previousHoldability);
            }
            DataSourceUtils.resetConnectionAfterTransaction(con, txObject.getPreviousIsolationLevel());
        } catch (HibernateException ex) {
            logger.debug("Could not access JDBC Connection of Hibernate Session", ex);
        } catch (Throwable ex) {
            logger.debug("Could not reset JDBC Connection after transaction", ex);
        }
    }
    if (txObject.isNewSession()) {
        if (logger.isDebugEnabled()) {
            logger.debug("Closing Hibernate Session [" + session + "] after transaction");
        }
        SessionFactoryUtils.closeSession(session);
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Not closing pre-bound Hibernate Session [" + session + "] after transaction");
        }
        if (txObject.getSessionHolder().getPreviousFlushMode() != null) {
            session.setFlushMode(txObject.getSessionHolder().getPreviousFlushMode());
        }
        if (!this.allowResultAccessAfterCompletion && !this.hibernateManagedSession) {
            disconnectOnCompletion(session);
        }
    }
    txObject.getSessionHolder().clear();
}
Also used : HibernateException(org.hibernate.HibernateException) Connection(java.sql.Connection) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Session(org.hibernate.Session)

Aggregations

SessionImplementor (org.hibernate.engine.spi.SessionImplementor)82 Session (org.hibernate.Session)59 Test (org.junit.Test)54 Connection (java.sql.Connection)20 TestForIssue (org.hibernate.testing.TestForIssue)18 PreparedStatement (java.sql.PreparedStatement)17 Work (org.hibernate.jdbc.Work)13 Statement (java.sql.Statement)12 List (java.util.List)12 Transaction (org.hibernate.Transaction)12 EntityPersister (org.hibernate.persister.entity.EntityPersister)12 ResultSet (java.sql.ResultSet)11 SQLException (java.sql.SQLException)11 ArrayList (java.util.ArrayList)7 JDBCException (org.hibernate.JDBCException)6 CollectionEntry (org.hibernate.engine.spi.CollectionEntry)6 EntityEntry (org.hibernate.engine.spi.EntityEntry)6 QueryParameters (org.hibernate.engine.spi.QueryParameters)6 ResultSetProcessor (org.hibernate.loader.plan.exec.process.spi.ResultSetProcessor)6 NamedParameterContext (org.hibernate.loader.plan.exec.query.spi.NamedParameterContext)6