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);
}
}
}
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);
}
}
Aggregations