gpt4 book ai didi

java - Kundera cassandra - 事务回滚和实体更新

转载 作者:行者123 更新时间:2023-11-30 10:50:38 25 4
gpt4 key购买 nike

我使用 Kundera-Cassandra 3.2,想使用 Kundera 的事务管理。

我的处理方式是这样的:

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();
}

this.repo.save(account2, manager);发生错误时,manager回滚了事务,但是没有做update语句,他为merge做了delete语句方法。这样做的原因是,在调用 merge 方法时,kundera 创建了一个插入语句而不是更新。但是怎么说 Kundera 做一个更新回滚事务也用一个更新。

日志:

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'.

编辑(我的存储库):

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;
}
}
}

最佳答案

根据 JPA,要更新实体,您必须首先将其置于托管状态(通过获取它)

例子:-

        PersonCassandra p = entityManager.find(PersonCassandra.class, "2");

entityManager.getTransaction().begin();
p.setMonth(Month.JAN);
entityManager.merge(p);
entityManager.persist(p3);
entityManager.getTransaction().commit();

问题不在于 INSERT 和 UPDATE 语句,因为两者对于 Cassandra 来说都是相似的,在幕后。

关于java - Kundera cassandra - 事务回滚和实体更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35013300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com