use of org.hibernate.exception.GenericJDBCException in project hibernate-orm by hibernate.
the class SequenceValueExtractor method extractSequenceValue.
public long extractSequenceValue(final SessionImplementor sessionImpl) {
class WorkImpl implements Work {
private long value;
public void execute(Connection connection) throws SQLException {
Session session = (Session) sessionImpl;
Transaction transaction = session.beginTransaction();
try {
final PreparedStatement query = sessionImpl.getJdbcCoordinator().getStatementPreparer().prepareStatement(queryString);
ResultSet resultSet = sessionImpl.getJdbcCoordinator().getResultSetReturn().extract(query);
resultSet.next();
value = resultSet.getLong(1);
resultSet.close();
transaction.commit();
} catch (GenericJDBCException e) {
transaction.rollback();
throw e;
}
if (dialect instanceof DerbyDialect) {
value--;
}
}
}
WorkImpl work = new WorkImpl();
((Session) sessionImpl).doWork(work);
return work.value;
}
use of org.hibernate.exception.GenericJDBCException in project stdlib by petergeneric.
the class TransactionMethodInterceptor method invoke.
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
if (sessionProvider.get().getTransaction().getStatus() == TransactionStatus.ACTIVE) {
// allow silent joining of enclosing transactional methods (NOTE: this ignores the current method's txn-al settings)
if (log.isTraceEnabled())
log.trace("Joining existing transaction to call " + invocation.getMethod().toGenericString());
return invocation.proceed();
} else {
Timer.Context callTimer = calls.time();
final String tracingId = Tracing.log("TX:begin", () -> invocation.getMethod().toGenericString());
try {
final Transactional annotation = readAnnotation(invocation);
// After the max attempts for auto retry are exhausted we'll fall back on the non-retrying default behaviour
if (annotation.autoRetry()) {
// Try all but the last attempt
final int retries = Math.max(0, annotation.autoRetryCount() - 1);
// N.B. more aggressive than the @Retry annotation implements
long backoff = 1000;
final double multiplier = 1.5;
for (int attempt = 0; attempt < retries; attempt++) {
try {
return createTransactionAndExecuteMethod(invocation, annotation);
} catch (LockAcquisitionException | StaleStateException | GenericJDBCException | OptimisticLockException e) {
if (log.isTraceEnabled())
log.warn("@Transactional caught exception " + e.getClass().getSimpleName() + "; retrying...", e);
else
log.warn("@Transactional caught exception " + e.getClass().getSimpleName() + "; retrying...");
Tracing.logOngoing(tracingId, "TX:exception:retryable", () -> e.getClass().getSimpleName());
try {
Thread.sleep(backoff);
} catch (InterruptedException ie) {
throw new RuntimeException("Interrupted while attempting a @Transactional retry!", ie);
}
// Increase backoff for the next exception
backoff *= multiplier;
} catch (PersistenceException e) {
// Handle generic exception (usually one that wraps another exception)
if (e.getCause() != null && (isSqlServerSnapshotConflictError(e) || isDeadlockError(e))) {
if (log.isTraceEnabled())
log.warn("@Transactional caught exception PersistenceException wrapping " + e.getCause().getClass().getSimpleName() + "; retrying...", e);
else
log.warn("@Transactional caught exception PersistenceException wrapping " + e.getCause().getClass().getSimpleName() + "; retrying...");
Tracing.logOngoing(tracingId, "TX:exception:retryable:wrapped", () -> e.getCause().getClass().getSimpleName());
try {
Thread.sleep(backoff);
} catch (InterruptedException ie) {
throw new RuntimeException("Interrupted while attempting a @Transactional retry!", ie);
}
// Increase backoff for the next exception
backoff *= multiplier;
} else {
Tracing.logOngoing(tracingId, "TX:exception:fatal", () -> e.getClass().getSimpleName());
// rethrow because we won't handle this
throw e;
}
}
}
}
Tracing.logOngoing(tracingId, "TX:last-try", null);
// Run without further retries
return createTransactionAndExecuteMethod(invocation, annotation);
} finally {
Tracing.logOngoing(tracingId, "TX:quit", null);
callTimer.stop();
}
}
}
use of org.hibernate.exception.GenericJDBCException in project head by mifos.
the class LegacyGenericDao method execUniqueResultNamedQueryWithResultTransformer.
@SuppressWarnings("unchecked")
public <T extends Object> T execUniqueResultNamedQueryWithResultTransformer(final String queryName, final Map<String, ?> queryParameters, final Class<T> clazz) {
try {
Query query = getSession().getNamedQuery(queryName).setResultTransformer(Transformers.aliasToBean(clazz));
query.setProperties(queryParameters);
query.setResultTransformer(Transformers.aliasToBean(clazz));
return (T) query.uniqueResult();
} catch (GenericJDBCException gje) {
throw new ConnectionNotFoundException(gje);
} catch (Exception e) {
throw new MifosRuntimeException(e);
}
}
use of org.hibernate.exception.GenericJDBCException in project head by mifos.
the class LegacyGenericDao method execUniqueResultNamedQueryWithoutFlush.
public Object execUniqueResultNamedQueryWithoutFlush(final String queryName, final Map<String, ?> queryParameters) throws PersistenceException {
try {
Session sess = getSession();
sess.setFlushMode(FlushMode.MANUAL);
Query query = getSession().getNamedQuery(queryName);
logger.debug("The query object for the query with the name " + queryName + " has been obtained");
query.setProperties(queryParameters);
Object returnObject = query.uniqueResult();
sess.setFlushMode(FlushMode.AUTO);
return returnObject;
} catch (GenericJDBCException gje) {
throw new ConnectionNotFoundException(gje);
} catch (Exception e) {
throw new PersistenceException(e);
}
}
use of org.hibernate.exception.GenericJDBCException in project head by mifos.
the class LegacyGenericDao method execUniqueResultNamedQuery.
public Object execUniqueResultNamedQuery(final String queryName, final Map<String, ?> queryParameters) throws PersistenceException {
try {
Query query = getSession().getNamedQuery(queryName);
logger.debug("The query object for the query with the name " + queryName + " has been obtained");
query.setProperties(queryParameters);
return query.uniqueResult();
} catch (GenericJDBCException gje) {
throw new ConnectionNotFoundException(gje);
} catch (Exception e) {
throw new PersistenceException(e);
}
}
Aggregations