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