Search code examples
javatransactionscassandrakundera

Transactions in kundera-cassandra


I want to use the transaction management from Kundera (V3.2) for my Cassandra database. Referring to https://github.com/impetus-opensource/Kundera/wiki/Transaction-Management it should be possible to use this functionality. I wrote following code:

public void update(Account entity){
    EntityManager manager = this.entityManagerFactory.createEntityManager(getProperties());
    manager.setFlushMode(FlushModeType.COMMIT);
    manager.getTransaction().begin();
    try{


        String queryStringNative = "UPDATE account SET value = 20 WHERE id = 'xxx' IF value = 10";
        Query query = manager.createNativeQuery(queryStringNative);
        query.executeUpdate();

        String queryStringNative1 = "UPDATE account SET value = 30 WHERE id = 'yyy' IF value = 40";
        Query query1 = manager.createNativeQuery(queryStringNative1);
        query1.executeUpdate();

        //commit
        manager.getTransaction().commit();

    } catch(Exception e){
        manager.getTransaction().rollback();
    }
    manager.clear();
    manager.close();
}

But when i simulate an error in the second query the rollback doesn't work and the account with id 'xxx' is updated.

So my question, is it generally possible to use the transaction implementation from Kundera for kundera-cassandra in my particular way?

EDIT:

I found out that Kundera uses a EventLogQueue to perform a rollback or commit. It reads the Events which are in the queue and rollbacks these events. The problem is, it seams that the EventQueue receives only events which are sent by method call of EntityManager.persist(), EntityManager.remove() or EntityManager.merge(). So there is no entry when executing a native query.


Solution

  • Transactions on native queries are not supported by Kundera.

    Reason:

    Kundera tracks the state of the entity objects to ensure transactions. In native queries the query could be anything.. update, delete, create, metadata queries, aggregation queries, etc where transactions are not usually used (atleast in NoSql world).

    Workaround:

    You can have client side transactions by explicitly checking for errors and have another query for undoing the previous query.