Search in sources :

Example 1 with SqlStatementLogger

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

the class JdbcServicesImpl method configure.

@Override
public void configure(Map configValues) {
    this.jdbcEnvironment = serviceRegistry.getService(JdbcEnvironment.class);
    assert jdbcEnvironment != null : "JdbcEnvironment was not found!";
    this.multiTenancyStrategy = MultiTenancyStrategy.determineMultiTenancyStrategy(configValues);
    final boolean showSQL = ConfigurationHelper.getBoolean(Environment.SHOW_SQL, configValues, false);
    final boolean formatSQL = ConfigurationHelper.getBoolean(Environment.FORMAT_SQL, configValues, false);
    this.sqlStatementLogger = new SqlStatementLogger(showSQL, formatSQL);
    resultSetWrapper = new ResultSetWrapperImpl(serviceRegistry);
}
Also used : SqlStatementLogger(org.hibernate.engine.jdbc.spi.SqlStatementLogger) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)

Example 2 with SqlStatementLogger

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

the class Helper method logStatement.

private static String logStatement(SessionFactoryImplementor factory, String sql) {
    final SqlStatementLogger statementLogger = factory.getServiceRegistry().getService(JdbcServices.class).getSqlStatementLogger();
    statementLogger.logStatement(sql, FormatStyle.BASIC.getFormatter());
    return sql;
}
Also used : SqlStatementLogger(org.hibernate.engine.jdbc.spi.SqlStatementLogger) JdbcServices(org.hibernate.engine.jdbc.spi.JdbcServices)

Example 3 with SqlStatementLogger

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

the class TableGenerator method generate.

@Override
public Serializable generate(final SharedSessionContractImplementor session, final Object obj) {
    final SqlStatementLogger statementLogger = session.getFactory().getServiceRegistry().getService(JdbcServices.class).getSqlStatementLogger();
    final SessionEventListenerManager statsCollector = session.getEventListenerManager();
    return optimizer.generate(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 selectPS = prepareStatement(connection, selectQuery, statementLogger, statsCollector)) {
                            selectPS.setString(1, segmentValue);
                            final ResultSet selectRS = executeQuery(selectPS, statsCollector);
                            if (!selectRS.next()) {
                                long initializationValue;
                                if (storeLastUsedValue) {
                                    initializationValue = initialValue - 1;
                                } else {
                                    initializationValue = initialValue;
                                }
                                value.initialize(initializationValue);
                                try (PreparedStatement insertPS = prepareStatement(connection, insertQuery, statementLogger, statsCollector)) {
                                    LOG.tracef("binding parameter [%s] - [%s]", 1, segmentValue);
                                    insertPS.setString(1, segmentValue);
                                    value.bind(insertPS, 2);
                                    executeUpdate(insertPS, statsCollector);
                                }
                            } else {
                                int defaultValue;
                                if (storeLastUsedValue) {
                                    defaultValue = 0;
                                } else {
                                    defaultValue = 1;
                                }
                                value.initialize(selectRS, defaultValue);
                            }
                            selectRS.close();
                        } catch (SQLException e) {
                            LOG.unableToReadOrInitHiValue(e);
                            throw e;
                        }
                        try (PreparedStatement updatePS = prepareStatement(connection, updateQuery, statementLogger, statsCollector)) {
                            final IntegralDataTypeHolder updateValue = value.copy();
                            if (optimizer.applyIncrementSizeToSourceValues()) {
                                updateValue.add(incrementSize);
                            } else {
                                updateValue.increment();
                            }
                            updateValue.bind(updatePS, 1);
                            value.bind(updatePS, 2);
                            updatePS.setString(3, segmentValue);
                            rows = executeUpdate(updatePS, statsCollector);
                        } catch (SQLException e) {
                            LOG.unableToUpdateQueryHiValue(renderedTableName, e);
                            throw e;
                        }
                    } while (rows == 0);
                    accessCount++;
                    if (storeLastUsedValue) {
                        return value.increment();
                    } else {
                        return value;
                    }
                }
            }, true);
        }

        @Override
        public String getTenantIdentifier() {
            return session.getTenantIdentifier();
        }
    });
}
Also used : AbstractReturningWork(org.hibernate.jdbc.AbstractReturningWork) SQLException(java.sql.SQLException) Connection(java.sql.Connection) 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)

Example 4 with SqlStatementLogger

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

the class MultipleHiLoPerTableGenerator method generate.

public synchronized Serializable generate(final SharedSessionContractImplementor session, Object obj) {
    DeprecationLogger.DEPRECATION_LOGGER.deprecatedTableGenerator(getClass().getName());
    final SqlStatementLogger statementLogger = session.getFactory().getServiceRegistry().getService(JdbcServices.class).getSqlStatementLogger();
    final SessionEventListenerManager statsCollector = session.getEventListenerManager();
    final WorkExecutorVisitable<IntegralDataTypeHolder> work = new AbstractReturningWork<IntegralDataTypeHolder>() {

        @Override
        public IntegralDataTypeHolder execute(Connection connection) throws SQLException {
            IntegralDataTypeHolder value = IdentifierGeneratorHelper.getIntegralDataTypeHolder(returnClass);
            int rows;
            do {
                final PreparedStatement queryPreparedStatement = prepareStatement(connection, query, statementLogger, statsCollector);
                try {
                    final ResultSet rs = executeQuery(queryPreparedStatement, statsCollector);
                    boolean isInitialized = rs.next();
                    if (!isInitialized) {
                        value.initialize(0);
                        final PreparedStatement insertPreparedStatement = prepareStatement(connection, insert, statementLogger, statsCollector);
                        try {
                            value.bind(insertPreparedStatement, 1);
                            executeUpdate(insertPreparedStatement, statsCollector);
                        } finally {
                            insertPreparedStatement.close();
                        }
                    } else {
                        value.initialize(rs, 0);
                    }
                    rs.close();
                } catch (SQLException sqle) {
                    LOG.unableToReadOrInitHiValue(sqle);
                    throw sqle;
                } finally {
                    queryPreparedStatement.close();
                }
                final PreparedStatement updatePreparedStatement = prepareStatement(connection, update, statementLogger, statsCollector);
                try {
                    value.copy().increment().bind(updatePreparedStatement, 1);
                    value.bind(updatePreparedStatement, 2);
                    rows = executeUpdate(updatePreparedStatement, statsCollector);
                } catch (SQLException sqle) {
                    LOG.error(LOG.unableToUpdateHiValue(tableName), sqle);
                    throw sqle;
                } finally {
                    updatePreparedStatement.close();
                }
            } while (rows == 0);
            return value;
        }
    };
    // maxLo < 1 indicates a hilo generator with no hilo :?
    if (maxLo < 1) {
        // keep the behavior consistent even for boundary usages
        IntegralDataTypeHolder value = null;
        while (value == null || value.lt(1)) {
            value = session.getTransactionCoordinator().createIsolationDelegate().delegateWork(work, true);
        }
        return value.makeValue();
    }
    return hiloOptimizer.generate(new AccessCallback() {

        public IntegralDataTypeHolder getNextValue() {
            return session.getTransactionCoordinator().createIsolationDelegate().delegateWork(work, true);
        }

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

Example 5 with SqlStatementLogger

use of org.hibernate.engine.jdbc.spi.SqlStatementLogger 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

SqlStatementLogger (org.hibernate.engine.jdbc.spi.SqlStatementLogger)6 Connection (java.sql.Connection)4 SQLException (java.sql.SQLException)4 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)4 PreparedStatement (java.sql.PreparedStatement)3 ResultSet (java.sql.ResultSet)3 SessionEventListenerManager (org.hibernate.engine.spi.SessionEventListenerManager)3 IntegralDataTypeHolder (org.hibernate.id.IntegralDataTypeHolder)2 AbstractReturningWork (org.hibernate.jdbc.AbstractReturningWork)2 AssertionFailure (org.hibernate.AssertionFailure)1 JdbcEnvironmentImpl (org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentImpl)1 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)1 IdentifierGenerationException (org.hibernate.id.IdentifierGenerationException)1 AccessCallback (org.hibernate.id.enhanced.AccessCallback)1