java - 链式事务 api 调用 spring

标签 java spring jpa transactional

我有一个问题,我没有得到我的交易提交

我的Service类是@Transactional注解的

@Transactional
class MyService{
   public void find();
   public void return(); //Return will only happen if find is updated in DB
   public void cancel(){
          find();
          return();
   }
}

我的类(class)有 3 个 api,它们在内部调用各自的 Dao,而 Dao 没有被注释为事务。

现在如果我调用取消,我将首先调用 find,然后调用 return()。但是 return() 需要 find() 需要用 FIND 状态更新数据库。

但是由于在返回取消之前所有操作都发生在同一个事务上,因此事务不会被提交。有人可以帮助我了解这种情况并解决它。

我不知道在这种情况下如何使用传播。

提前致谢..

最佳答案

摘自 Spring Transcation 文档 (Spring Doc)

In proxy mode (which is the default), only external method calls coming in through the proxy are intercepted. This means that self-invocation, in effect, a method within the target object calling another method of the target object, will not lead to an actual transaction at runtime even if the invoked method is marked with @Transactional.

在 cancel() 方法中对 find() 和 return() 的调用不是事务性的。如果您希望它是事务性的,您可以执行类似 applicationContext.getBean(this.getClass()); 的操作并调用您的方法。

关于java - 链式事务 api 调用 spring,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27923326/

相关文章:

sql-server - 数据类型 time 和 datetime 在大于或等于运算符中不兼容

java - 从 HttpServletRequest 中退出请求有效负载时抛出 Java Lang 非法状态异常

java - 如何干净利落地处理stream/forEach中的 "unreported exception IOException"?

java - 用于桌面应用程序的 Apache Felix

java - JSF EJB3 到 Spring 3,BigInteger 始终为 0 而不是 null

java - Spring Tool Suite 找不到 JDBC 驱动程序类

java - 如何使实体只读?

JAVA - 如何在不同的类中使用 Enum 作为公共(public)静态最终字符串?

java - 如何在 IntelliJ IDEA 12 中查看方面受影响的 Java 类?

java - Spring Boot - 使用自定义对象数组 JSON 序列化自定义对象