Spring Bean 使用 @Transactional 挂在方法上

Rho*_*jin 5 spring jpa transactional propagation

只是一点背景,我是一个新的开发人员,最近在高级开发人员离开公司之后接手了一个重大项目,然后我才完全了解他是如何构建这个项目的。我会尽力解释我的问题。

此应用程序创建多个 MessageListner 线程以从 JMS 队列读取对象。接收到对象后,数据将根据某些业务逻辑进行操作,然后映射到持久性对象以使用休眠 EntityManager 保存到 oracle 数据库中。

直到几周前,自从我加入这个项目以来,这个配置在过去一年左右的时间里还没有出现任何重大问题。但是对于其中一个队列(问题与此特定队列隔离),处理接收到的对象的 spring 托管 bean 挂在下面的方法中。我的调试使我得出结论,它已完成方法中的所有内容,但在完成时挂起。经过数周的尝试解决这个问题,我对这个问题束手无策。对此的任何帮助将不胜感激。

由于每个 MessageListner 都有自己的处理器,因此这种挂起方法只影响一个队列上的传入数据。

@Transactional(propagation = Propagation.REQUIRES_NEW , timeout = 180)
 public void update(UserRelatedData userData, User user,Company company,...)
 { 
   ...
   ....
   //business logic performed on user object
   ....
   ......
   entityMgr.persist(user);

   //business logic performed on userData object
   ...
   ....
   entityMgr.persist(userData);

   ...
   ....

   entityMgr.flush();

}
Run Code Online (Sandbox Code Playgroud)

我插入调试语句只是为了遍历该方法,它完成了包括 entityMgr.flush.() 在内的所有内容。

p3c*_*ing 6

REQUIRES_NEW 可能会挂在测试上下文中,因为单元测试中使用的事务管理器不支持嵌套事务......来自 JpaTransactionManager 的 Javadoc:

* <p>This transaction manager supports nested transactions via JDBC 3.0 Savepoints.
 * The {@link #setNestedTransactionAllowed "nestedTransactionAllowed"} flag defaults
 * to {@code false} though, since nested transactions will just apply to the JDBC
 * Connection, not to the JPA EntityManager and its cached entity objects and related
 * context. You can manually set the flag to {@code true} if you want to use nested
 * transactions for JDBC access code which participates in JPA transactions (provided
 * that your JDBC driver supports Savepoints). <i>Note that JPA itself does not support
 * nested transactions! Hence, do not expect JPA access code to semantically
 * participate in a nested transaction.</i>
Run Code Online (Sandbox Code Playgroud)

很明显,如果您不调用 (@Java config) 或在 XML 配置中设置等效标志:

txManager.setNestedTransactionAllowed(true);
Run Code Online (Sandbox Code Playgroud)

或者如果您的驱动程序不支持保存点,则出现 REQUIRES_NEW 问题是“正常的”......(有些人可能更喜欢例外“不支持嵌套事务”)


mre*_*isz 4

当底层数据库因未提交的更改而锁定时,就会出现此类问题。

我怀疑的是一些其他代码在事务外部或在事务中对 userData 表进行插入/删除,由于它是批处理作业或类似作业,因此需要很长时间才能执行。您应该分析引用这些表的所有代码并查找丢失的@Transactional。