Search code examples
javacassandrakundera

Kundera cassandra - Transaction rollback and Entity update


I use Kundera-Cassandra 3.2 and want to use the transaction management from Kundera.

My handling looks like this:

EntityManager manager = repo.getEntityManagerFactory().createEntityManager(CassandraRepository.getProperties());

try{
    manager.getTransaction().begin();

    this.repo.update(account1, manager); //calls the merge method of the Entitymanager
    this.repo.save(account2, manager); //calls the persist method of the Entitymanager

    manager.getTransaction().commit();

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

When an error in the this.repo.save(account2, manager); occurs, the manager rollbacks the transaction, but does not do a update statement, he makes a delete statement for the merge method. The reason for this is, when calling the merge methode, kundera creates an insert statement and not an update. But how to say Kundera to make an update to rollback the transaction also with an update.

Logs:

12:42:41.185 [http-bio-8080-exec-3] INFO com.impetus.client.cassandra.CassandraClientBase - Returning delete query DELETE FROM "account" WHERE "id" = 'MCSP-000000000004'.
12:42:41.211 [http-bio-8080-exec-3] INFO com.impetus.client.cassandra.CassandraClientBase - Returning delete query DELETE FROM "account" WHERE "id" = 'MCSP-000000000005'.

EDIT (my repository):

public class CassandraRepository<T> {

    @PersistenceUnit
    private EntityManagerFactory entityManagerFactory;

    public static Map<String, String> getProperties() {
        final Map<String, String> properties = new HashMap<String, String>();
        properties.put(CassandraConstants.CQL_VERSION, CassandraConstants.CQL_VERSION_3_0);
        return properties;
    }



    public void update(T entity, EntityManager manager) throws Exception{
        try {
            manager.merge(entity);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

    public void save(T entity, EntityManager manager) throws Exception{
        try {
            manager.persist(entity);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }
}

Solution

  • According to JPA, to update an entity you have to first bring it into managed state (by fetching it)

    Example:-

            PersonCassandra p = entityManager.find(PersonCassandra.class, "2");
    
            entityManager.getTransaction().begin();
            p.setMonth(Month.JAN);
            entityManager.merge(p);
            entityManager.persist(p3);
            entityManager.getTransaction().commit();
    

    Issue is not with INSERT and UPDATE statements since both are similar for Cassandra, under the hood.