Search in sources :

Example 11 with LockModeType

use of jakarta.persistence.LockModeType in project eclipselink by eclipse-ee4j.

the class PessimisticLockingExtendedScopeTestSuite method testPESSMISTIC_ES8.

// Unidirectional OneToMany Relationship, in which entity does not contain the foreign key will not be locked (Scenario 3.2)
public void testPESSMISTIC_ES8() throws Exception {
    if (getPlatform().isSQLServer()) {
        warning("This test deadlocks on SQL Server");
        return;
    }
    if ((JUnitTestCase.getServerSession()).getPlatform().isHANA()) {
        // feature is under development (see bug 384129), but test should be skipped for the time being
        return;
    }
    // Cannot create parallel entity managers in the server.
    if (!isOnServer() && isSelectForUpateSupported()) {
        EntityManager em = createEntityManager();
        Employee emp = null;
        try {
            beginTransaction(em);
            emp = new Employee();
            emp.getDealers().add(new Dealer("Honda", "Kanata"));
            em.persist(emp);
            commitTransaction(em);
        } catch (RuntimeException ex) {
            throw ex;
        } finally {
            if (isTransactionActive(em)) {
                rollbackTransaction(em);
            }
            closeEntityManager(em);
        }
        Exception lockTimeOutException = null;
        LockModeType lockMode = LockModeType.PESSIMISTIC_WRITE;
        Map<String, Object> properties = new HashMap();
        properties.put(QueryHints.PESSIMISTIC_LOCK_SCOPE, PessimisticLockScope.NORMAL);
        EntityManager em1 = createEntityManager();
        try {
            beginTransaction(em1);
            emp = em1.find(Employee.class, emp.getId());
            em1.lock(emp, lockMode, properties);
            EntityManager em2 = createEntityManager();
            try {
                beginTransaction(em2);
                emp = em1.find(Employee.class, emp.getId());
                emp.setDealers(null);
                commitTransaction(em2);
            } catch (jakarta.persistence.RollbackException ex) {
                fail("it should not throw the exception!!!");
            } finally {
                if (isTransactionActive(em2)) {
                    rollbackTransaction(em2);
                }
                closeEntityManager(em2);
            }
        } catch (Exception ex) {
            fail("it should not throw the exception!!!");
            throw ex;
        } finally {
            if (isTransactionActive(em1)) {
                rollbackTransaction(em1);
            }
            closeEntityManager(em1);
        }
    }
}
Also used : EntityManager(jakarta.persistence.EntityManager) Employee(org.eclipse.persistence.testing.models.jpa.advanced.Employee) HashMap(java.util.HashMap) LockModeType(jakarta.persistence.LockModeType) Dealer(org.eclipse.persistence.testing.models.jpa.advanced.Dealer)

Example 12 with LockModeType

use of jakarta.persistence.LockModeType in project eclipselink by eclipse-ee4j.

the class PessimisticLockingExtendedScopeTestSuite method testNonrepeatableRead.

/*
     * The test should assert that the following phenomenon does not occur
     * after a row has been locked by T1:
     *
     * - P2 (Non-repeatable read): Transaction T1 reads a row. Another
     * transaction T2 then modifies or deletes that row, before T1 has
     * committed or rolled back.
     */
private <X> void testNonrepeatableRead(final Actor<X> actor) throws InterruptedException {
    // Cannot create parallel entity managers in the server.
    if (isOnServer() || !isSelectForUpateSupported()) {
        return;
    }
    EntityManager em = createEntityManager();
    EntyC c = null;
    try {
        beginTransaction(em);
        actor.setup(em);
        commitTransaction(em);
    } catch (RuntimeException ex) {
        throw ex;
    } finally {
        if (isTransactionActive(em)) {
            rollbackTransaction(em);
        }
        closeEntityManager(em);
    }
    Exception lockTimeOutException = null;
    LockModeType lockMode = LockModeType.PESSIMISTIC_WRITE;
    Map<String, Object> properties = new HashMap();
    properties.put(QueryHints.PESSIMISTIC_LOCK_SCOPE, PessimisticLockScope.EXTENDED);
    properties.put(QueryHints.PESSIMISTIC_LOCK_TIMEOUT, 10000);
    EntityManager em1 = createEntityManager();
    try {
        beginTransaction(em1);
        X locked = actor.getEntityToLock(em1);
        em1.lock(locked, lockMode, properties);
        final EntityManager em2 = createEntityManager();
        try {
            // P2 (Non-repeatable read)
            Runnable runnable = new Runnable() {

                @Override
                public void run() {
                    try {
                        beginTransaction(em2);
                        actor.modify(em2);
                        // might wait for lock to be released
                        commitTransaction(em2);
                    } catch (jakarta.persistence.RollbackException ex) {
                        if (ex.getMessage().indexOf("org.eclipse.persistence.exceptions.DatabaseException") == -1) {
                            ex.printStackTrace();
                            fail("it's not the right exception");
                        }
                    }
                }
            };
            Thread t2 = new Thread(runnable);
            t2.start();
            // allow t2 to atempt update
            Thread.sleep(1000);
            // assert repeatable read
            actor.check(em1, locked);
            // release lock
            rollbackTransaction(em1);
            // wait until t2 finished
            t2.join();
        } finally {
            if (isTransactionActive(em2)) {
                rollbackTransaction(em2);
            }
            closeEntityManager(em2);
        }
    } finally {
        if (isTransactionActive(em1)) {
            rollbackTransaction(em1);
        }
        closeEntityManager(em1);
    }
}
Also used : HashMap(java.util.HashMap) EntityManager(jakarta.persistence.EntityManager) EntyC(org.eclipse.persistence.testing.models.jpa.advanced.entities.EntyC) LockModeType(jakarta.persistence.LockModeType)

Aggregations

LockModeType (jakarta.persistence.LockModeType)12 EntityManager (jakarta.persistence.EntityManager)8 HashMap (java.util.HashMap)6 JpaEntityManager (org.eclipse.persistence.jpa.JpaEntityManager)4 Employee (org.eclipse.persistence.testing.models.jpa.advanced.Employee)4 DatabaseQuery (org.eclipse.persistence.queries.DatabaseQuery)3 EntityExistsException (jakarta.persistence.EntityExistsException)2 EntityNotFoundException (jakarta.persistence.EntityNotFoundException)2 OptimisticLockException (jakarta.persistence.OptimisticLockException)2 PersistenceException (jakarta.persistence.PersistenceException)2 Query (jakarta.persistence.Query)2 RollbackException (jakarta.persistence.RollbackException)2 TransactionRequiredException (jakarta.persistence.TransactionRequiredException)2 TypedQuery (jakarta.persistence.TypedQuery)2 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)2 IntegrityException (org.eclipse.persistence.exceptions.IntegrityException)2 QueryException (org.eclipse.persistence.exceptions.QueryException)2 ValidationException (org.eclipse.persistence.exceptions.ValidationException)2 JpaQuery (org.eclipse.persistence.jpa.JpaQuery)2 DataModifyQuery (org.eclipse.persistence.queries.DataModifyQuery)2