Java Code Examples for javax.persistence.EntityManager#lock()

The following examples show how to use javax.persistence.EntityManager#lock() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: OptimisticLockingIntegrationTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test(expected = OptimisticLockException.class)
public void givenVersionedEntitiesWithLockByLockMethod_whenConcurrentUpdate_thenOptimisticLockException() throws IOException {
    EntityManager em = getEntityManagerWithOpenTransaction();
    OptimisticLockingStudent student = em.find(OptimisticLockingStudent.class, 1L);
    em.lock(student, LockModeType.OPTIMISTIC);

    EntityManager em2 = getEntityManagerWithOpenTransaction();
    OptimisticLockingStudent student2 = em2.find(OptimisticLockingStudent.class, 1L);
    em.lock(student, LockModeType.OPTIMISTIC_FORCE_INCREMENT);
    student2.setName("RICHARD");
    em2.persist(student2);
    em2.getTransaction()
        .commit();
    em2.close();

    student.setName("JOHN");
    em.persist(student);
    em.getTransaction()
        .commit();
    em.close();
}
 
Example 2
Source File: InvoiceEntityListener.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Finds the current invoice number, checks for uniqueness, retrieves it and increments by one
 * 
 * @return
 */
private int findAndIncrementInvoiceNumber(){
	int ret = 0;
	EntityManager em = (EntityManager) ElexisEntityManagerServiceHolder.getEntityManager()
		.getEntityManager(false);
	try {
		em.getTransaction().begin();
		Config invoiceNr = em.find(Config.class, "RechnungsNr");
		if (invoiceNr == null) {
			Config invoiceNrConfig = new Config();
			invoiceNrConfig.setParam("RechnungsNr");
			invoiceNrConfig.setWert("1");
			em.persist(invoiceNrConfig);
			ret = 1;
		} else {
			em.lock(invoiceNr, LockModeType.PESSIMISTIC_WRITE);
			ret = Integer.parseInt(invoiceNr.getWert());
			ret += 1;
			
			while (true) {
				TypedQuery<Invoice> query =
					em.createNamedQuery("Invoice.number", Invoice.class);
				query.setParameter("number", Integer.toString(ret));
				List<Invoice> results = query.getResultList();
				if (results.isEmpty()) {
					break;
				} else {
					ret += 1;
				}
			}
			invoiceNr.setWert(Integer.toString(ret));
		}
		em.getTransaction().commit();
		return ret;
	} finally {
		ElexisEntityManagerServiceHolder.getEntityManager().closeEntityManager(em);
	}
}
 
Example 3
Source File: LabOrderEntityListener.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Finds the current lab order number, checks for uniqueness, retrieves it and increments by one
 * 
 * @return
 */
private int findAndIncrementLabOrderId(){
	int ret = 0;
	EntityManager em =
		(EntityManager) ElexisEntityManagerServiceHolder.getEntityManager()
			.getEntityManager(false);
	try {
		em.getTransaction().begin();
		LabOrder version = em.find(LabOrder.class, "VERSION");
		if (version == null) {
			version = new LabOrder();
			version.setId("VERSION");
			version.setOrderid("1");
			version.setDeleted(true);
			em.persist(version);
			ret = 1;
		} else {
			em.lock(version, LockModeType.PESSIMISTIC_WRITE);
			ret = Integer.parseInt(version.getOrderid());
			ret += 1;
			
			while (true) {
				TypedQuery<LabOrder> query =
					em.createNamedQuery("LabOrder.orderid", LabOrder.class);
				query.setParameter("orderid", Integer.toString(ret));
				List<LabOrder> results = query.getResultList();
				if (results.isEmpty()) {
					break;
				} else {
					ret += 1;
				}
			}
			version.setOrderid(Integer.toString(ret));
		}
		em.getTransaction().commit();
		return ret;
	} finally {
		ElexisEntityManagerServiceHolder.getEntityManager().closeEntityManager(em);
	}
}
 
Example 4
Source File: KontaktEntityListener.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Finds the current patient number, checks for uniqueness, retrieves it and
 * increments by one
 * 
 * @return
 */
private int findAndIncrementPatientNr(){
	int ret = 0;
	EntityManager em = (EntityManager) ElexisEntityManagerServiceHolder.getEntityManager()
		.getEntityManager(false);
	try {
		em.getTransaction().begin();
		Config patNr = em.find(Config.class, "PatientNummer");
		if (patNr == null) {
			Config patNrConfig = new Config();
			patNrConfig.setParam("PatientNummer");
			patNrConfig.setWert("1");
			em.persist(patNrConfig);
			ret = 1;
		} else {
			em.lock(patNr, LockModeType.PESSIMISTIC_WRITE);
			ret = Integer.parseInt(patNr.getWert());
			ret += 1;

			while (true) {
				TypedQuery<Kontakt> query =
					em.createNamedQuery("Kontakt.code", Kontakt.class);
				query.setParameter("code", Integer.toString(ret));
				List<Kontakt> results = query.getResultList();
				if (results.isEmpty()) {
					break;
				} else {
					ret += 1;
				}
			}
			patNr.setWert(Integer.toString(ret));
		}
		em.getTransaction().commit();
		return ret;
	} finally {
		ElexisEntityManagerServiceHolder.getEntityManager().closeEntityManager(em);
	}
}
 
Example 5
Source File: LockModeOptimisticWithPessimisticLockUpgradeTest.java    From high-performance-java-persistence with Apache License 2.0 4 votes vote down vote up
@Override
protected void lockUpgrade(EntityManager entityManager, Post post) {
    entityManager.lock(post, LockModeType.PESSIMISTIC_READ);
}