Search in sources :

Example 6 with SessionEventListenerManager

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

the class CacheHelper method fromSharedCache.

public static Serializable fromSharedCache(SharedSessionContractImplementor session, Object cacheKey, RegionAccessStrategy cacheAccessStrategy) {
    final SessionEventListenerManager eventListenerManager = session.getEventListenerManager();
    Serializable cachedValue = null;
    eventListenerManager.cacheGetStart();
    try {
        cachedValue = (Serializable) cacheAccessStrategy.get(session, cacheKey, session.getTimestamp());
    } finally {
        eventListenerManager.cacheGetEnd(cachedValue != null);
    }
    return cachedValue;
}
Also used : Serializable(java.io.Serializable) SessionEventListenerManager(org.hibernate.engine.spi.SessionEventListenerManager)

Example 7 with SessionEventListenerManager

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

the class TableStructure method buildCallback.

@Override
public AccessCallback buildCallback(final SharedSessionContractImplementor session) {
    final SqlStatementLogger statementLogger = session.getFactory().getServiceRegistry().getService(JdbcServices.class).getSqlStatementLogger();
    if (selectQuery == null || updateQuery == null) {
        throw new AssertionFailure("SequenceStyleGenerator's TableStructure was not properly initialized");
    }
    final SessionEventListenerManager statsCollector = session.getEventListenerManager();
    return new AccessCallback() {

        @Override
        public IntegralDataTypeHolder getNextValue() {
            return session.getTransactionCoordinator().createIsolationDelegate().delegateWork(new AbstractReturningWork<IntegralDataTypeHolder>() {

                @Override
                public IntegralDataTypeHolder execute(Connection connection) throws SQLException {
                    final IntegralDataTypeHolder value = makeValue();
                    int rows;
                    do {
                        try (PreparedStatement selectStatement = prepareStatement(connection, selectQuery, statementLogger, statsCollector)) {
                            final ResultSet selectRS = executeQuery(selectStatement, statsCollector);
                            if (!selectRS.next()) {
                                final String err = "could not read a hi value - you need to populate the table: " + tableNameText;
                                LOG.error(err);
                                throw new IdentifierGenerationException(err);
                            }
                            value.initialize(selectRS, 1);
                            selectRS.close();
                        } catch (SQLException sqle) {
                            LOG.error("could not read a hi value", sqle);
                            throw sqle;
                        }
                        try (PreparedStatement updatePS = prepareStatement(connection, updateQuery, statementLogger, statsCollector)) {
                            final int increment = applyIncrementSizeToSourceValues ? incrementSize : 1;
                            final IntegralDataTypeHolder updateValue = value.copy().add(increment);
                            updateValue.bind(updatePS, 1);
                            value.bind(updatePS, 2);
                            rows = executeUpdate(updatePS, statsCollector);
                        } catch (SQLException e) {
                            LOG.unableToUpdateQueryHiValue(tableNameText, e);
                            throw e;
                        }
                    } while (rows == 0);
                    accessCounter++;
                    return value;
                }
            }, true);
        }

        @Override
        public String getTenantIdentifier() {
            return session.getTenantIdentifier();
        }
    };
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) SQLException(java.sql.SQLException) Connection(java.sql.Connection) IdentifierGenerationException(org.hibernate.id.IdentifierGenerationException) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) SqlStatementLogger(org.hibernate.engine.jdbc.spi.SqlStatementLogger) SessionEventListenerManager(org.hibernate.engine.spi.SessionEventListenerManager) IntegralDataTypeHolder(org.hibernate.id.IntegralDataTypeHolder)

Aggregations

SessionEventListenerManager (org.hibernate.engine.spi.SessionEventListenerManager)7 Connection (java.sql.Connection)3 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SQLException (java.sql.SQLException)3 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)3 SqlStatementLogger (org.hibernate.engine.jdbc.spi.SqlStatementLogger)3 Serializable (java.io.Serializable)2 SharedSessionContractImplementor (org.hibernate.engine.spi.SharedSessionContractImplementor)2 IntegralDataTypeHolder (org.hibernate.id.IntegralDataTypeHolder)2 AbstractReturningWork (org.hibernate.jdbc.AbstractReturningWork)2 AssertionFailure (org.hibernate.AssertionFailure)1 EntityRegionAccessStrategy (org.hibernate.cache.spi.access.EntityRegionAccessStrategy)1 CacheEntry (org.hibernate.cache.spi.entry.CacheEntry)1 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)1 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)1 EventListenerRegistry (org.hibernate.event.service.spi.EventListenerRegistry)1 EventType (org.hibernate.event.spi.EventType)1 PreLoadEventListener (org.hibernate.event.spi.PreLoadEventListener)1 IdentifierGenerationException (org.hibernate.id.IdentifierGenerationException)1