use of org.hibernate.JDBCException in project hibernate-orm by hibernate.
the class PessimisticWriteSelectLockingStrategy method lock.
@Override
public void lock(Serializable id, Object version, Object object, int timeout, SharedSessionContractImplementor session) {
final String sql = determineSql(timeout);
final SessionFactoryImplementor factory = session.getFactory();
try {
try {
final PreparedStatement st = session.getJdbcCoordinator().getStatementPreparer().prepareStatement(sql);
try {
getLockable().getIdentifierType().nullSafeSet(st, id, 1, session);
if (getLockable().isVersioned()) {
getLockable().getVersionType().nullSafeSet(st, version, getLockable().getIdentifierType().getColumnSpan(factory) + 1, session);
}
final ResultSet rs = session.getJdbcCoordinator().getResultSetReturn().extract(st);
try {
if (!rs.next()) {
if (factory.getStatistics().isStatisticsEnabled()) {
factory.getStatistics().optimisticFailure(getLockable().getEntityName());
}
throw new StaleObjectStateException(getLockable().getEntityName(), id);
}
} finally {
session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(rs, st);
}
} finally {
session.getJdbcCoordinator().getLogicalConnection().getResourceRegistry().release(st);
session.getJdbcCoordinator().afterStatementExecution();
}
} catch (SQLException e) {
throw session.getJdbcServices().getSqlExceptionHelper().convert(e, "could not lock: " + MessageHelper.infoString(getLockable(), id, session.getFactory()), sql);
}
} catch (JDBCException e) {
throw new PessimisticEntityLockException(object, "could not obtain pessimistic lock", e);
}
}
use of org.hibernate.JDBCException in project hibernate-orm by hibernate.
the class PessimisticWriteUpdateLockingStrategy 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);
}
}
use of org.hibernate.JDBCException in project hibernate-orm by hibernate.
the class ContextualLobCreator method createNClob.
@Override
public NClob createNClob(String string) {
try {
final NClob nclob = createNClob();
nclob.setString(1, string);
return nclob;
} catch (SQLException e) {
throw new JDBCException("Unable to set NCLOB string after creation", e);
}
}
use of org.hibernate.JDBCException in project hibernate-orm by hibernate.
the class ContextualLobCreator method createBlob.
@Override
public Blob createBlob(byte[] bytes) {
try {
final Blob blob = createBlob();
blob.setBytes(1, bytes);
return blob;
} catch (SQLException e) {
throw new JDBCException("Unable to set BLOB bytes after creation", e);
}
}
use of org.hibernate.JDBCException in project hibernate-orm by hibernate.
the class EmbeddableIntegratorTest method testWithoutIntegrator.
/**
* Throws a mapping exception because DollarValue is not mapped
*/
@Test
public void testWithoutIntegrator() {
SessionFactory sf = new Configuration().addAnnotatedClass(Investor.class).setProperty("hibernate.hbm2ddl.auto", "create-drop").buildSessionFactory();
try {
Session sess = sf.openSession();
try {
sess.getTransaction().begin();
Investor myInv = getInvestor();
myInv.setId(1L);
sess.save(myInv);
sess.flush();
fail("A JDBCException expected");
sess.clear();
Investor inv = (Investor) sess.get(Investor.class, 1L);
assertEquals(new BigDecimal("100"), inv.getInvestments().get(0).getAmount().getAmount());
} catch (PersistenceException e) {
assertTyping(JDBCException.class, e.getCause());
sess.getTransaction().rollback();
}
sess.close();
} finally {
sf.close();
}
}
Aggregations