Search in sources :

Example 11 with StaleObjectStateException

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);
    }
}
Also used : JDBCException(org.hibernate.JDBCException) SQLException(java.sql.SQLException) SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) StaleObjectStateException(org.hibernate.StaleObjectStateException)

Example 12 with StaleObjectStateException

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;
}
Also used : Serializable(java.io.Serializable) PersistenceException(javax.persistence.PersistenceException) OptimisticLockException(javax.persistence.OptimisticLockException) EntityNotFoundException(javax.persistence.EntityNotFoundException) StaleObjectStateException(org.hibernate.StaleObjectStateException)

Example 13 with StaleObjectStateException

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();
}
Also used : Item(org.hibernate.test.jpa.Item) SQLServerDialect(org.hibernate.dialect.SQLServerDialect) Transaction(org.hibernate.Transaction) SQLGrammarException(org.hibernate.exception.SQLGrammarException) StaleObjectStateException(org.hibernate.StaleObjectStateException) Session(org.hibernate.Session) Test(org.junit.Test) AbstractJPATest(org.hibernate.test.jpa.AbstractJPATest)

Aggregations

StaleObjectStateException (org.hibernate.StaleObjectStateException)13 SQLException (java.sql.SQLException)8 PreparedStatement (java.sql.PreparedStatement)7 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)6 HibernateException (org.hibernate.HibernateException)5 JDBCException (org.hibernate.JDBCException)4 ResultSet (java.sql.ResultSet)3 Serializable (java.io.Serializable)2 PersistenceException (javax.persistence.PersistenceException)2 AssertionFailure (org.hibernate.AssertionFailure)2 Session (org.hibernate.Session)2 VersionType (org.hibernate.type.VersionType)2 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 OptimisticLockException (javax.persistence.OptimisticLockException)1 Transaction (org.hibernate.Transaction)1 WrongClassException (org.hibernate.WrongClassException)1 SQLServerDialect (org.hibernate.dialect.SQLServerDialect)1 JdbcServices (org.hibernate.engine.jdbc.spi.JdbcServices)1 EventSource (org.hibernate.event.spi.EventSource)1 SQLGrammarException (org.hibernate.exception.SQLGrammarException)1