use of org.hibernate.id.IntegralDataTypeHolder 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.id.IntegralDataTypeHolder in project uPortal by Jasig.
the class HibernateStyleCounterStore method getNextIdInternal.
private int getNextIdInternal(final Optimizer optimizer, final String counterName) {
return (Integer) optimizer.generate(new AccessCallback() {
@Override
public IntegralDataTypeHolder getNextValue() {
IntegralDataTypeHolder rslt = null;
for (int i = 0; rslt == null && i < MAX_ATTEMPTS; i++) {
rslt = transactionOperations.execute(new TransactionCallback<IntegralDataTypeHolder>() {
@Override
public IntegralDataTypeHolder doInTransaction(TransactionStatus status) {
final IntegralDataTypeHolder value = IdentifierGeneratorHelper.getIntegralDataTypeHolder(identifierType.getReturnedClass());
// Try and load the current value,
// returns true if the expected row
// exists, null otherwise
final boolean selected = jdbcOperations.query(SELECT_QUERY, new ResultSetExtractor<Boolean>() {
@Override
public Boolean extractData(ResultSet rs) throws SQLException, DataAccessException {
if (rs.next()) {
value.initialize(rs, 1);
return true;
}
return false;
}
}, counterName);
// it
if (!selected) {
value.initialize(initialValue);
jdbcOperations.update(INSERT_QUERY, new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
ps.setString(1, counterName);
value.bind(ps, 2);
}
});
}
// Increment the counter row value
final IntegralDataTypeHolder updateValue = value.copy();
if (optimizer.applyIncrementSizeToSourceValues()) {
updateValue.add(incrementSize);
} else {
updateValue.increment();
}
// Update the counter row, if rows
// returns 0 the update failed due to a
// race condition, it will be retried
int rowsAltered = jdbcOperations.update(UPDATE_QUERY, new PreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps) throws SQLException {
updateValue.bind(ps, 1);
ps.setString(2, counterName);
value.bind(ps, 3);
}
});
return rowsAltered > 0 ? // Success
value : // Failed; try again...
null;
}
});
}
if (rslt == null) {
throw new RuntimeException("Failed to fetch a new batch of sequence values after " + MAX_ATTEMPTS + " tries");
}
return rslt;
}
});
}
use of org.hibernate.id.IntegralDataTypeHolder in project hibernate-orm by hibernate.
the class SequenceStructure method buildCallback.
@Override
public AccessCallback buildCallback(final SharedSessionContractImplementor session) {
if (sql == null) {
throw new AssertionFailure("SequenceStyleGenerator's SequenceStructure was not properly initialized");
}
return new AccessCallback() {
@Override
public IntegralDataTypeHolder getNextValue() {
accessCounter++;
try {
final PreparedStatement st = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(sql);
try {
final ResultSet rs = session.getJdbcCoordinator().getResultSetReturn().extract(st);
try {
rs.next();
final IntegralDataTypeHolder value = IdentifierGeneratorHelper.getIntegralDataTypeHolder(numberType);
value.initialize(rs, 1);
if (LOG.isDebugEnabled()) {
LOG.debugf("Sequence value obtained: %s", value.makeValue());
}
return value;
} finally {
try {
session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(rs, st);
} catch (Throwable ignore) {
// intentionally empty
}
}
} finally {
session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(st);
session.getJdbcCoordinator().afterStatementExecution();
}
} catch (SQLException sqle) {
throw session.getJdbcServices().getSqlExceptionHelper().convert(sqle, "could not get next sequence value", sql);
}
}
@Override
public String getTenantIdentifier() {
return session.getTenantIdentifier();
}
};
}
use of org.hibernate.id.IntegralDataTypeHolder 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();
}
};
}
use of org.hibernate.id.IntegralDataTypeHolder in project hibernate-orm by hibernate.
the class NoopOptimizer method generate.
@Override
public Serializable generate(AccessCallback callback) {
// IMPL NOTE : this method is called concurrently and is
// not synchronized. It is very important to work on the
// local variable: the field lastSourceValue is not
// reliable as it might be mutated by multipled threads.
// The lastSourceValue field is only accessed by tests,
// so this is not a concern.
IntegralDataTypeHolder value = callback.getNextValue();
lastSourceValue = value;
return value.makeValue();
}
Aggregations