em = emf.createEntityManager();
getLog().info(" *Begin a transaction.");
em.getTransaction().begin();
resetSQL();
int beforeReadLevel = getConfiguration(em).getReadLockLevelConstant();
LockModeType beforeReadMode = MixedLockLevelsHelper.fromLockLevel(beforeReadLevel);
getLog().info(" *Save ReadLockLevel before Query:" + beforeReadMode);
getLog().info(" *Query " + entityName + "(" + id0 + ") with PESSIMISTIC_READ lock");
Query q = em.createQuery(queryString);
if (extended) {
q = q.setHint("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);
}
q = q.setLockMode(LockModeType.PESSIMISTIC_READ);
q = q.setParameter("firstName", "firstName%" + id0);
List<T> es = q.getResultList();
getLog().info(" *Found " + es.size() + " entity");
assertEquals(" *Should find 1 entity", es.size(), 1);
verify.queryPessimisticReadDbSQL(em);
e0 = es.get(0);
getLog().info(" *Found entity:" + e0);
assertNotNull(" *Found " + entityName + "(" + id0 + ")", e0);
assertEquals("Assert pessimistic read lock applied", LockModeType.PESSIMISTIC_READ, em.getLockMode(e0));
assertEquals(" *Read lock should still be " + beforeReadMode + "after query set lock mode",
beforeReadLevel, getConfiguration(em).getReadLockLevelConstant());
getLog().info(
" *Find " + entityName + "(" + id1
+ ") with no lock to verify query lock set does not affect em lock mode.");
resetSQL();
e1 = em.find(type, id1);
getLog().info(" *" + (e1 != null ? "F" : "Can not f") + "ind entity");
verify.findNoLockAfterQueryPessimisticReadDbSQL(em);
getLog().info(" *Found entity:" + e1);
assertNotNull(" *Found " + entityName + "(" + id1 + ")", e1);
assertEquals(" *Assert default lock applied", LockModeType.NONE, em.getLockMode(e1));
getLog().info("Committing transaction.");
em.getTransaction().commit();
} finally {
em = null;
e0 = e1 = null;
if (em != null && em.isOpen()) {
em.close();
}
}
try {
getLog().info("-- Test name query with pessimistic write lock in " + scope + " scope");
em = emf.createEntityManager();
getLog().info(" *Begin a transaction.");
em.getTransaction().begin();
resetSQL();
int beforeReadLevel = getConfiguration(em).getReadLockLevelConstant();
LockModeType beforeReadMode = MixedLockLevelsHelper.fromLockLevel(beforeReadLevel);
getLog().info(" *Save ReadLockLevel before Query:" + beforeReadMode);
getLog().info(" *Query " + entityName + "(" + id0 + ") with PESSIMISTIC_WRITE lock");
Query q = em.createNamedQuery(namedQueryString);
if (extended) {
q = q.setHint("javax.persistence.lock.scope", PessimisticLockScope.EXTENDED);