Search in sources :

Example 1 with PessimisticLockException

use of org.hibernate.PessimisticLockException in project hibernate-orm by hibernate.

the class InvalidationTest method testConcurrentRemoveAndPutFromLoad.

@Test
@TestForIssue(jiraKey = "HHH-9868")
public void testConcurrentRemoveAndPutFromLoad() throws Exception {
    final Item item = new Item("chris", "Chris's Item");
    withTxSession(s -> {
        s.persist(item);
    });
    Phaser deletePhaser = new Phaser(2);
    Phaser getPhaser = new Phaser(2);
    HookInterceptor hook = new HookInterceptor();
    AdvancedCache pendingPutsCache = getPendingPutsCache(Item.class);
    pendingPutsCache.addInterceptor(hook, 0);
    AtomicBoolean getThreadBlockedInDB = new AtomicBoolean(false);
    Thread deleteThread = new Thread(() -> {
        try {
            withTxSession(s -> {
                Item loadedItem = s.get(Item.class, item.getId());
                assertNotNull(loadedItem);
                arriveAndAwait(deletePhaser, 2000);
                arriveAndAwait(deletePhaser, 2000);
                log.trace("Item loaded");
                s.delete(loadedItem);
                s.flush();
                log.trace("Item deleted");
                arriveAndAwait(deletePhaser, 2000);
                arriveAndAwait(deletePhaser, 4000);
            });
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }, "delete-thread");
    Thread getThread = new Thread(() -> {
        try {
            withTxSession(s -> {
                Item loadedItem = s.get(Item.class, item.getId());
                if (getThreadBlockedInDB.get()) {
                    assertNull(loadedItem);
                } else {
                    assertNotNull(loadedItem);
                }
            });
        } catch (PessimisticLockException e) {
            // (delete-thread has ITEMS table write-locked and we try to acquire read-lock)
            try {
                arriveAndAwait(getPhaser, 2000);
                arriveAndAwait(getPhaser, 2000);
            } catch (Exception e1) {
                throw new RuntimeException(e1);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }, "get-thread");
    deleteThread.start();
    // deleteThread loads the entity
    arriveAndAwait(deletePhaser, 2000);
    withTx(() -> {
        sessionFactory().getCache().evictEntity(Item.class, item.getId());
        assertFalse(sessionFactory().getCache().containsEntity(Item.class, item.getId()));
        return null;
    });
    arriveAndAwait(deletePhaser, 2000);
    // delete thread invalidates PFER
    arriveAndAwait(deletePhaser, 2000);
    // get thread gets the entity from DB
    hook.block(getPhaser, getThread);
    getThread.start();
    try {
        arriveAndAwait(getPhaser, 2000);
    } catch (TimeoutException e) {
        getThreadBlockedInDB.set(true);
    }
    arriveAndAwait(deletePhaser, 2000);
    // delete thread finishes the remove from DB and cache
    deleteThread.join();
    hook.unblock();
    arriveAndAwait(getPhaser, 2000);
    // get thread puts the entry into cache
    getThread.join();
    assertNoInvalidators(pendingPutsCache);
    withTxSession(s -> {
        Item loadedItem = s.get(Item.class, item.getId());
        assertNull(loadedItem);
    });
}
Also used : Item(org.hibernate.test.cache.infinispan.functional.entities.Item) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AdvancedCache(org.infinispan.AdvancedCache) Phaser(java.util.concurrent.Phaser) TimeoutException(java.util.concurrent.TimeoutException) PessimisticLockException(org.hibernate.PessimisticLockException) PessimisticLockException(org.hibernate.PessimisticLockException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with PessimisticLockException

use of org.hibernate.PessimisticLockException in project hibernate-orm by hibernate.

the class SQLStateConversionDelegate method convert.

@Override
public JDBCException convert(SQLException sqlException, String message, String sql) {
    final String sqlState = JdbcExceptionHelper.extractSqlState(sqlException);
    final int errorCode = JdbcExceptionHelper.extractErrorCode(sqlException);
    if (sqlState != null) {
        String sqlStateClassCode = JdbcExceptionHelper.determineSqlStateClassCode(sqlState);
        if (sqlStateClassCode != null) {
            if (SQL_GRAMMAR_CATEGORIES.contains(sqlStateClassCode)) {
                return new SQLGrammarException(message, sqlException, sql);
            } else if (INTEGRITY_VIOLATION_CATEGORIES.contains(sqlStateClassCode)) {
                final String constraintName = getConversionContext().getViolatedConstraintNameExtracter().extractConstraintName(sqlException);
                return new ConstraintViolationException(message, sqlException, sql, constraintName);
            } else if (CONNECTION_CATEGORIES.contains(sqlStateClassCode)) {
                return new JDBCConnectionException(message, sqlException, sql);
            } else if (DATA_CATEGORIES.contains(sqlStateClassCode)) {
                return new DataException(message, sqlException, sql);
            }
        }
        if ("40001".equals(sqlState)) {
            return new LockAcquisitionException(message, sqlException, sql);
        }
        if ("40XL1".equals(sqlState) || "40XL2".equals(sqlState)) {
            // Derby "A lock could not be obtained within the time requested."
            return new PessimisticLockException(message, sqlException, sql);
        }
        // MySQL Query execution was interrupted
        if ("70100".equals(sqlState) || // Oracle user requested cancel of current operation
        ("72000".equals(sqlState) && errorCode == 1013)) {
            throw new QueryTimeoutException(message, sqlException, sql);
        }
    }
    return null;
}
Also used : DataException(org.hibernate.exception.DataException) QueryTimeoutException(org.hibernate.QueryTimeoutException) JDBCConnectionException(org.hibernate.exception.JDBCConnectionException) SQLGrammarException(org.hibernate.exception.SQLGrammarException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) LockAcquisitionException(org.hibernate.exception.LockAcquisitionException) PessimisticLockException(org.hibernate.PessimisticLockException)

Example 3 with PessimisticLockException

use of org.hibernate.PessimisticLockException 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);
}
Also used : SQLExceptionConversionDelegate(org.hibernate.exception.spi.SQLExceptionConversionDelegate) JDBCException(org.hibernate.JDBCException) SQLException(java.sql.SQLException) PessimisticLockException(org.hibernate.PessimisticLockException) Test(org.junit.Test)

Example 4 with PessimisticLockException

use of org.hibernate.PessimisticLockException in project hibernate-orm by hibernate.

the class VersionedTest method testStaleRead.

protected ByRef<Object> testStaleRead(BiConsumer<Session, Item> consumer) throws Exception {
    AtomicReference<Exception> synchronizationException = new AtomicReference<>();
    CountDownLatch syncLatch = new CountDownLatch(1);
    CountDownLatch commitLatch = new CountDownLatch(1);
    Future<Boolean> action = executor.submit(() -> withTxSessionApply(s -> {
        try {
            ((SharedSessionContractImplementor) s).getTransactionCoordinator().getLocalSynchronizations().registerSynchronization(new Synchronization() {

                @Override
                public void beforeCompletion() {
                }

                @Override
                public void afterCompletion(int i) {
                    syncLatch.countDown();
                    try {
                        awaitOrThrow(commitLatch);
                    } catch (Exception e) {
                        synchronizationException.set(e);
                    }
                }
            });
            Item item = s.load(Item.class, itemId);
            consumer.accept(s, item);
            s.flush();
        } catch (StaleStateException e) {
            log.info("Exception thrown: ", e);
            markRollbackOnly(s);
            return false;
        } catch (PessimisticLockException e) {
            log.info("Exception thrown: ", e);
            markRollbackOnly(s);
            return false;
        }
        return true;
    }));
    awaitOrThrow(syncLatch);
    ByRef<Object> entryRef = new ByRef<>(null);
    try {
        withTxSession(s -> {
            Item item = s.load(Item.class, itemId);
            assertEquals("Original item", item.getDescription());
            entryRef.set(assertSingleCacheEntry());
        });
    } finally {
        commitLatch.countDown();
    }
    assertTrue(action.get(WAIT_TIMEOUT, TimeUnit.SECONDS));
    assertNull(synchronizationException.get());
    return entryRef;
}
Also used : BaseTransactionalDataRegion(org.hibernate.cache.infinispan.impl.BaseTransactionalDataRegion) Arrays(java.util.Arrays) VersionedEntry(org.hibernate.cache.infinispan.util.VersionedEntry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Session(org.hibernate.Session) Caches(org.hibernate.cache.infinispan.util.Caches) AtomicReference(java.util.concurrent.atomic.AtomicReference) Future(java.util.concurrent.Future) PessimisticLockException(org.hibernate.PessimisticLockException) InvocationContext(org.infinispan.context.InvocationContext) AdvancedCache(org.infinispan.AdvancedCache) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) Synchronization(javax.transaction.Synchronization) StaleStateException(org.hibernate.StaleStateException) CyclicBarrier(java.util.concurrent.CyclicBarrier) ByRef(org.infinispan.commons.util.ByRef) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Assert.assertNotEquals(org.junit.Assert.assertNotEquals) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) BaseCustomInterceptor(org.infinispan.interceptors.base.BaseCustomInterceptor) Assert.assertNull(org.junit.Assert.assertNull) Flag(org.infinispan.context.Flag) Assert.assertFalse(org.junit.Assert.assertFalse) PutKeyValueCommand(org.infinispan.commands.write.PutKeyValueCommand) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) SharedSessionContractImplementor(org.hibernate.engine.spi.SharedSessionContractImplementor) ByRef(org.infinispan.commons.util.ByRef) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Synchronization(javax.transaction.Synchronization) PessimisticLockException(org.hibernate.PessimisticLockException) StaleStateException(org.hibernate.StaleStateException) PessimisticLockException(org.hibernate.PessimisticLockException) Item(org.hibernate.test.cache.infinispan.functional.entities.Item) OtherItem(org.hibernate.test.cache.infinispan.functional.entities.OtherItem) StaleStateException(org.hibernate.StaleStateException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Aggregations

PessimisticLockException (org.hibernate.PessimisticLockException)4 Test (org.junit.Test)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 Item (org.hibernate.test.cache.infinispan.functional.entities.Item)2 AdvancedCache (org.infinispan.AdvancedCache)2 SQLException (java.sql.SQLException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 Future (java.util.concurrent.Future)1 Phaser (java.util.concurrent.Phaser)1 TimeUnit (java.util.concurrent.TimeUnit)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 BiConsumer (java.util.function.BiConsumer)1 Synchronization (javax.transaction.Synchronization)1