use of org.hibernate.StaleObjectStateException in project hibernate-orm by hibernate.
the class PessimisticReadSelectLockingStrategy 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.StaleObjectStateException in project hibernate-orm by hibernate.
the class ExceptionConverterImpl method wrapStaleStateException.
protected PersistenceException wrapStaleStateException(StaleStateException e) {
PersistenceException pe;
if (e instanceof StaleObjectStateException) {
final StaleObjectStateException sose = (StaleObjectStateException) e;
final Serializable identifier = sose.getIdentifier();
if (identifier != null) {
try {
final Object entity = sharedSessionContract.internalLoad(sose.getEntityName(), identifier, false, true);
if (entity instanceof Serializable) {
//avoid some user errors regarding boundary crossing
pe = new OptimisticLockException(e.getMessage(), e, entity);
} else {
pe = new OptimisticLockException(e.getMessage(), e);
}
} catch (EntityNotFoundException enfe) {
pe = new OptimisticLockException(e.getMessage(), e);
}
} else {
pe = new OptimisticLockException(e.getMessage(), e);
}
} else {
pe = new OptimisticLockException(e.getMessage(), e);
}
return pe;
}
use of org.hibernate.StaleObjectStateException in project hibernate-orm by hibernate.
the class RepeatableReadTest method testStaleVersionedInstanceFoundOnLock.
@Test
public void testStaleVersionedInstanceFoundOnLock() {
if (!readCommittedIsolationMaintained("repeatable read tests")) {
return;
}
String check = "EJB3 Specification";
Session s1 = sessionFactory().openSession();
Transaction t1 = s1.beginTransaction();
Item item = new Item(check);
s1.save(item);
t1.commit();
s1.close();
Long itemId = item.getId();
long initialVersion = item.getVersion();
// Now, open a new Session and re-load the item...
s1 = sessionFactory().openSession();
t1 = s1.beginTransaction();
item = (Item) s1.get(Item.class, itemId);
// now that the item is associated with the persistence-context of that session,
// open a new session and modify it "behind the back" of the first session
Session s2 = sessionFactory().openSession();
Transaction t2 = s2.beginTransaction();
Item item2 = (Item) s2.get(Item.class, itemId);
item2.setName("EJB3 Persistence Spec");
t2.commit();
s2.close();
// at this point, s1 now contains stale data, so acquire a READ lock
// and make sure we get the already associated state (i.e., the old
// name and the old version)
s1.lock(item, LockMode.READ);
item2 = (Item) s1.get(Item.class, itemId);
assertTrue(item == item2);
assertEquals("encountered non-repeatable read", check, item2.getName());
assertEquals("encountered non-repeatable read", initialVersion, item2.getVersion());
// attempt to acquire an UPGRADE lock; this should fail
try {
s1.lock(item, LockMode.UPGRADE);
fail("expected UPGRADE lock failure");
} catch (StaleObjectStateException expected) {
// this is the expected behavior
} catch (SQLGrammarException t) {
if (getDialect() instanceof SQLServerDialect) {
// sql-server (using snapshot isolation) reports this as a grammar exception /:)
//
// not to mention that it seems to "lose track" of the transaction in this scenario...
t1.rollback();
t1 = s1.beginTransaction();
} else {
throw t;
}
}
t1.commit();
s1.close();
// clean up
s1 = sessionFactory().openSession();
t1 = s1.beginTransaction();
s1.createQuery("delete Item").executeUpdate();
t1.commit();
s1.close();
}
Aggregations