public IntegralDataTypeHolder getNextValue() {
return session.getTransactionCoordinator().getTransaction().createIsolationDelegate().delegateWork(
new AbstractReturningWork<IntegralDataTypeHolder>() {
@Override
public IntegralDataTypeHolder execute(Connection connection) throws SQLException {
final IntegralDataTypeHolder value = IdentifierGeneratorHelper.getIntegralDataTypeHolder(
identifierType.getReturnedClass()
);
int rows;
do {
statementLogger.logStatement(
selectQuery,
FormatStyle.BASIC.getFormatter()
);
PreparedStatement selectPS = connection.prepareStatement( selectQuery );
try {
selectPS.setString( 1, segmentValue );
final ResultSet selectRS = selectPS.executeQuery();
if ( !selectRS.next() ) {
value.initialize( initialValue );
PreparedStatement insertPS = null;
try {
statementLogger.logStatement(
insertQuery,
FormatStyle.BASIC.getFormatter()
);
insertPS = connection.prepareStatement( insertQuery );
insertPS.setString( 1, segmentValue );
value.bind( insertPS, 2 );
insertPS.execute();
}
finally {
if ( insertPS != null ) {
insertPS.close();
}
}
}
else {
value.initialize( selectRS, 1 );
}
selectRS.close();
}
catch (SQLException e) {
LOG.unableToReadOrInitHiValue( e );
throw e;
}
finally {
selectPS.close();
}
statementLogger.logStatement(
updateQuery,
FormatStyle.BASIC.getFormatter()
);
final PreparedStatement updatePS = connection.prepareStatement( updateQuery );
try {
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 = updatePS.executeUpdate();
}
catch (SQLException e) {