java - Hibernate 中的多个事务

标签 java hibernate

在一个 session 中多次调用 beginTransaction 是否有风险?我的意思是例如:

session.beginTransaction();
session.saveOrUpdate(object1);
// .... some works
session.beginTransaction();
session.delete(object2);
// ... some other works
session.getTransaction.commit();


我这样做了,似乎没有问题(任何异常或警告)。事实上,我想知道当我以这种方式使用事务时会发生什么。

最佳答案

javadocs给个解释

beginTransaction() Begin a unit of work and return the associated Transaction object. If a new underlying transaction is required, begin the transaction. Otherwise continue the new work in the context of the existing underlying transaction. The class of the returned Transaction object is determined by the property hibernate.transaction_factory.

因此 - 它没有任何效果,它继续使用现有事务。所以不要这样做,因为它可能会让读者(包括你)感到困惑。如果您提交第一个事务然后开始第二个事务,则可以有多个事务。

关于java - Hibernate 中的多个事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6992751/

相关文章:

hibernate - 之后添加 JPA @OrderColumn 会导致集合错误的空索引列

java - 在 JPA 实体的基类中定义序列生成器?

java - 将重新启动、关闭和调整功能应用于 JOptionPane 的特定选项

java - 为什么 enum 设置为 null 后该值仍被保留

java - KeyPressed 事件没有发生?

java - 当我离开 Activity 并返回时,Activity 不断重启

java - 线程中的 Hibernate 异常 "main"java.lang.ExceptionInInitializerError

java - Hibernate 标准 - 加入未映射的实体

java - 在类路径资源 [spring/database/DataSource.xml] 中定义了名称为 'dataSource' 的无效 bean 定义

java - 创建动态 JavaBean 或将变量动态添加到现有 JavaBean