Hi,
I've tried to implement a counter with ObjectDB, but the following code doesn't work as expected:
final EntityManagerFactory emf = Persistence.createEntityManagerFactory("test.odb"); EntityManager em = emf.createEntityManager(); Point p = new Point(0, 0); em.getTransaction().begin(); em.persist(p); em.getTransaction().commit(); em.close(); final CountDownLatch l = new CountDownLatch(100); for (int i = 0; i < 100; i++) { Thread t = new Thread(new Runnable() { @Override public void run() { EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); //Query q = em.createQuery("UPDATE Point SET x = x + 1"); Query query = em.createQuery("UPDATE Point SET x = x + 1"); query.executeUpdate(); // Point p = new Point(1, 1); // em.persist(p); em.getTransaction().commit(); em.close(); l.countDown(); //To change body of implemented methods use File | Settings | File Templates. } }); t.start(); } l.await(); em = emf.createEntityManager(); TypedQuery<Point> myquery = em.createQuery("SELECT p from Point p", Point.class); List<Point> results = myquery.getResultList(); System.out.println("X coordinate is: " + results.get(0).getX()); em.close();
The result I expect has to be X coordinate is 100 but in reality, the code prints out different result: 2
What's wrong with my code?