use of org.hibernate.JDBCException in project hibernate-orm by hibernate.
the class BeforeCompletionFailureTest method testUniqueConstraintViolationDuringManagedFlush.
@Test
@TestForIssue(jiraKey = "HHH-9888")
public void testUniqueConstraintViolationDuringManagedFlush() throws Exception {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// set up test data
Session session = openSession();
session.getTransaction().begin();
session.save(newEntity(1));
session.getTransaction().commit();
session.close();
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// do the test
final TransactionManager tm = JtaPlatformStandardTestingImpl.INSTANCE.transactionManager();
assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
// begin the transaction ("CMT" style)
tm.begin();
session = openSession();
session.save(newEntity(2));
// which should lead to the UK violation
try {
tm.commit();
fail("Expecting a failure from JTA commit");
} catch (RollbackException expected) {
log.info("Test encountered expected JTA RollbackException; looking for nested JDBCException", expected);
boolean violationExceptionFound = false;
Throwable cause = expected;
while (cause != null) {
if (cause instanceof JDBCException) {
log.info("Found JDBCException, assuming related to UK violation", cause);
violationExceptionFound = true;
break;
}
cause = cause.getCause();
}
if (!violationExceptionFound) {
fail("Did not find JDBCException in JTA RollbackException chain");
}
} finally {
if (!((SessionImplementor) session).isClosed()) {
session.close();
}
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// clean up test data
session = openSession();
session.getTransaction().begin();
session.createQuery("delete SimpleEntity").executeUpdate();
session.getTransaction().commit();
session.close();
}
use of org.hibernate.JDBCException in project hibernate-orm by hibernate.
the class PostgreSQL81DialectTestCase method testTimeoutException.
@Test
public void testTimeoutException() {
PostgreSQL81Dialect dialect = new PostgreSQL81Dialect();
SQLExceptionConversionDelegate delegate = dialect.buildSQLExceptionConversionDelegate();
assertNotNull(delegate);
JDBCException exception = delegate.convert(new SQLException("Lock Not Available", "55P03"), "", "");
assertTrue(exception instanceof PessimisticLockException);
}
use of org.hibernate.JDBCException in project hibernate-orm by hibernate.
the class GeneralWorkTest method testSQLExceptionThrowing.
@Test
public void testSQLExceptionThrowing() {
final Session session = openSession();
session.beginTransaction();
try {
session.doWork(new Work() {
public void execute(Connection connection) throws SQLException {
Statement statement = null;
try {
statement = ((SessionImplementor) session).getJdbcCoordinator().getStatementPreparer().createStatement();
((SessionImplementor) session).getJdbcCoordinator().getResultSetReturn().extract(statement, "select * from non_existent");
} finally {
releaseQuietly(((SessionImplementor) session), statement);
}
}
});
fail("expecting exception");
} catch (JDBCException expected) {
// expected outcome
}
session.getTransaction().commit();
session.close();
}
use of org.hibernate.JDBCException in project hibernate-orm by hibernate.
the class NonBatchingBatch method addToBatch.
@Override
public void addToBatch() {
notifyObserversImplicitExecution();
for (Map.Entry<String, PreparedStatement> entry : getStatements().entrySet()) {
try {
final PreparedStatement statement = entry.getValue();
final int rowCount = jdbcCoordinator.getResultSetReturn().executeUpdate(statement);
getKey().getExpectation().verifyOutcome(rowCount, statement, 0);
jdbcCoordinator.getResourceRegistry().release(statement);
jdbcCoordinator.afterStatementExecution();
} catch (SQLException e) {
abortBatch();
throw sqlExceptionHelper().convert(e, "could not execute non-batched batch statement", entry.getKey());
} catch (JDBCException e) {
abortBatch();
throw e;
}
}
getStatements().clear();
}
use of org.hibernate.JDBCException in project hibernate-orm by hibernate.
the class PessimisticReadUpdateLockingStrategy method lock.
@Override
public void lock(Serializable id, Object version, Object object, int timeout, SharedSessionContractImplementor session) {
if (!lockable.isVersioned()) {
throw new HibernateException("write locks via update not supported for non-versioned entities [" + lockable.getEntityName() + "]");
}
final SessionFactoryImplementor factory = session.getFactory();
try {
try {
final PreparedStatement st = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(sql);
try {
lockable.getVersionType().nullSafeSet(st, version, 1, session);
int offset = 2;
lockable.getIdentifierType().nullSafeSet(st, id, offset, session);
offset += lockable.getIdentifierType().getColumnSpan(factory);
if (lockable.isVersioned()) {
lockable.getVersionType().nullSafeSet(st, version, offset, session);
}
final int affected = session.getJdbcCoordinator().getResultSetReturn().executeUpdate(st);
// todo: should this instead check for exactly one row modified?
if (affected < 0) {
if (factory.getStatistics().isStatisticsEnabled()) {
factory.getStatistics().optimisticFailure(lockable.getEntityName());
}
throw new StaleObjectStateException(lockable.getEntityName(), id);
}
} finally {
session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(st);
session.getJdbcCoordinator().afterStatementExecution();
}
} catch (SQLException e) {
throw session.getJdbcServices().getSqlExceptionHelper().convert(e, "could not lock: " + MessageHelper.infoString(lockable, id, session.getFactory()), sql);
}
} catch (JDBCException e) {
throw new PessimisticEntityLockException(object, "could not obtain pessimistic lock", e);
}
}
Aggregations