Kundera transactions should fail on backing store entity change
I understand that transactions cannot be implemented in NoSQL the same as they are in SQL. However, there is more to transactions than simply deferring writes until the transaction completes, as the docs here seem to imply:
https://github.com/impetus-opensource/Kundera/wiki/Transaction-Management
For a single entity (cassandra row/mongo document), you should be able to perform transactions. For example, in the usual sequence:
- begin transaction
- read entity
- modify entity
- merge/persist entity
- commit transaction
Based on my tests of Kundera/Mongo, no locking occurs (because no locking is available in Mongo) and the sequence always procedes. If the entity on the datastore is modified by a third party durring the transaction, that change is simply wiped out, meaning no transaction has occurred. The appears to have been differed, but the important part of the transaction (atomicity) is lost. Atomicity is possible, and transactions can be implemented, as I will explain below.
What should happen is that the transaction should either lock (when locking is available, such as on SQL), or the commit should fail. The commit should always fail if the entity has been removed or differs from the original value. Imagine a case where two users are adding elements to a list in the entity. If they are woking at the same time, under your scheme, they will both think they have succeeded, when in fact only one of them has. When a commit fails, they can simply try again. If that's not the behavior the user wants, they shouldn't use transactions.
Here is some of my test code:
em.getTransaction().begin();
XonamiUser xu;
String docId;
try {
xu = new XonamiUser(em, null, "bjorn", "roche", null, "[email protected]", true, "abcdefg12345678" );
em.persist(xu);
docId = xu.getUserKey();
} catch (AddressException e1) {
throw new RuntimeException(e1);
} catch (IllegalArgumentException e1) {
throw new RuntimeException(e1);
} catch (BadPasswordException e1) {
throw new RuntimeException(e1);
}
em.getTransaction().commit();
// at this stage, the document is correctly stored in the database
em.getTransaction().begin();
xu = em.find(XonamiUser.class, docId );
xu.setFirstname("john");
em.merge(xu);
// we have modified it, but not transacted the change. The change resides in memory only
// outside the transaction, we modify the element. This should cause the transaction to fail.
DB db = EMFactory.createMongoClientDB();
db.setWriteConcern(WriteConcern.ACKNOWLEDGED);
DBCollection coll = db.getCollection("XonamiUser");
BasicDBObject query = new BasicDBObject("_id", docId);
BasicDBObject update = new BasicDBObject("all", "new");
coll.findAndModify(query, update);
// the transaction succeeds, wiping out the change we did above.
// Atomicity is lost.
em.getTransaction().commit();
Transactions can and should be implemented in Mongo by using an update with a query that contains the complete document that was read. You can also use a version number that gets incremented each time. That way, if the document has changed, the data will not be found, and the transaction will fail. If more than one documents have been modified by the transaction, the transaction should also fail since the system cannot guarantee atomicity in this case. This is how other JPA/NoSQL solutions work (eg. google app engine), it's called optimistic locking. Hibernate can also be used to do similar things.
http://gae-java-persistence.blogspot.com/2009/10/optimistic-locking-with-version.html
In Cassandra, I understand transactions are available with zookeeper.
Thanks for the explanation and the time. Of late we have been tied up with Kundera 2.6 Release. Marking for discussion and further action.
Rereading what I wrote, I may have conflated atomicity and transactions a bit. I also forgot to mention there's a whole series of functions in JPA specifically for optimistic locking, but that's a start anyway. Good luck.