I've been trying to learn more abount the new lock capabilities in JPA2. I'm reading [url=http://weblogs.java.net/blog/2009/07/30/jpa-20-concurrency-and-locking]Carol McDonald's[url] article. Unfortunately I'm unable to run any of the example given by Carol in this article.
Trying to reproduce the example given in figure 2 I've written the following code:
Code:
public class Main {
public static void main(String[] args) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory(
"JPALockingPU");
EntityManager em0 = emf.createEntityManager();
EntityManager em1 = emf.createEntityManager();
System.out.println("em0 = " + em0);
System.out.println("em1 = " + em1);
em0.getTransaction().begin();
em1.getTransaction().begin();
EntityOne p0 = em0.find(EntityOne.class, 1);
EntityTwo e1 = em1.find(EntityTwo.class, 1);
EntityOne p1 = e1.getEntityTwo();
em1.lock(p1, LockModeType.OPTIMISTIC);
p0.setTeste(p0.getTeste() + 1);
e1.setTeste(e1.getTeste() + 1);
em0.getTransaction().commit();
em1.getTransaction().commit();
em0.close();
em1.close();
}
}
This is a desktop app (of course) and I'm running this code with Hibernate 3.5.3 and PostgreSQL 8.1. According Carol's article em1's transaction should not commit as e1 is locked and it was updated by em0's transaction but it simple don't happen. Looking the generated sql, it doesn't even check e1's version atribute. What am I missing?
PS.: Both EntityOne and EntityTwo have a version attribute.