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