Hi
I have tested pesimistic lock and the result was for me unexpected. In the first transaction i modified one object and in the second transaction i wanted to the same and I got an exceptopn that lock could not be granted, quite nice but in that moment I wanted commit the first transaction and I also got lock exception. this the test:
public void testPesimisticLock() { PersistenceManager pm1 = null; PersistenceManager pm2 = null; try { pm1 = pmf.getPersistenceManager(); pm1.currentTransaction().setOptimistic(false); pm1.currentTransaction().begin(); long numOfPictures = countObjects(pm1, Picture.class); Picture picture1 = (Picture) readRandomObject(pm1, numOfPictures, Picture.class); picture1.setCanvas(new Square(picture1, 10)); pm2 = pmf.getPersistenceManager(); pm2.currentTransaction().setOptimistic(false); pm2.currentTransaction().begin(); Picture picture2 = null; try { picture2 = (Picture) pm2.getObjectById(pm1.getObjectId(picture1)); picture2.setCanvas(new Square(picture2, 20)); pm2.currentTransaction().commit(); } catch (JDOUserException e1) { pm1.currentTransaction().commit(); pm2.currentTransaction().rollback(); System.out.println(Thread.currentThread().getName() + " --" + "Pesimistic lock succed"); return; } pm1.currentTransaction().commit(); System.out.println(Thread.currentThread().getName() + " --Pesimistic lock failed"); } catch (JDOUserException e1){ System.out.println(Thread.currentThread().getName() + " --Pesimistic lock failed " + e1.getMessage()); } finally { if (pm1 != null) { if (pm1.currentTransaction().isActive()) pm1.currentTransaction().rollback(); pm1.close(); } if (pm2 != null) { if (pm2.currentTransaction().isActive()) pm2.currentTransaction().rollback(); pm2.close(); } } }
br
Tomasz