春季 @Transactional 提交失败;Deby+Eclipselink

7
以下是Spring配置文件:
数据源
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
    <property name="driverClassName" value="${rwos.dataSource.driverClassName}" />
    <property name="url" value="${rwos.dataSource.url}" />
    <property name="username" value="${rwos.dataSource.user}" />
    <property name="password" value="${rwos.dataSource.password}" />
 </bean>

实体管理器配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd">

  <bean id="persistenceUnitManager" class="org.springframework.data.jpa.support.MergingPersistenceUnitManager">
        <property name="defaultDataSource" ref="dataSource"/>
  </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitManager" ref="persistenceUnitManager"/>
        <property name="persistenceUnitName" value="com.retailwave.rwos_rwos-data-pojo_jar_4.0.0PU"/>
  </bean>

  <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"  lazy-init="true">
                <property name="entityManagerFactory" ref="entityManagerFactory"/>
                <property name="dataSource" ref="dataSource" />
  </bean>
    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
</beans>

以下是用于持久化实体的代码片段。
@Singleton
@Component
public class RWTransactionDao {

@PersistenceContext(type = PersistenceContextType.EXTENDED)
private EntityManager em;

 @Override
protected EntityManager getEntityManager() {
    return em;
}

@Transactional
public void createOrderTxns(RWRetailTransaction peTxn, RWRetailTransaction fcTxn) {
    create(peTxn);
    create(fcTxn);
    log.debug("Committed Transaction : {} ", peTxn.displayString());
    log.debug("Committed Transaction : {} ", fcTxn.displayString());
 }

 @Transactional
 public void create(T entity) {
   getEntityManager().persist(entity);
 }

}

类:

@Component
@Path("/")
public class RWTransactionREST {

   @Inject
   private RWTransactionDao rWTransactionDao;

  @POST
  @Produces(MediaType.APPLICATION_JSON)
  @Path("txns/sales")
  public RWResponse createPurchaseTransaction(RWRetailTransaction peTxn, RWRetailTransaction fcTxn) {
    rWTransactionDao.createOrderTxns(peTxn, fcTxn);
    builder.status(RWStatus.OK);
    RWResponse response = builder.build();
    log.info("Returning.. {}", response);
    return response; 
   }

  }

日志信息:

2017-06-14 10:49:51,453 DEBUG [qtp592179046-13] - Committed Transaction : RWRetailTransaction[609, CU_ORD, RTCO-609-17-11193, 2017-06-14 10:49:51.431] 
2017-06-14 10:49:51,453 DEBUG [qtp592179046-13]  - Committed Transaction : RWRetailTransaction[509, CU_ORD, RTCO-509-17-11193, 2017-06-14 10:49:51.444]
2017-06-14 10:49:51,463 INFO  [qtp592179046-13] RWTransactionREST - Returning.. Response[1000:Order has been created successfully, Transaction Id : RTCO-609-17-11193]

在一段时间后,在同一个RWTransactionDao中,其他一些REST实现方法也出现了以下错误:

2017-06-14 10:51:24,199 ERROR [qtp592179046-16] com.retailwave.rwos.compartment.rest.exception.RWCompartmentRestExceptionMapper - Exception caught at Mapper : Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.4.v20160829-44060b6): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: java.sql.SQLTransactionRollbackException: A lock could not be obtained within the time requested 

由于这个错误,之前的提交被回滚了,但其实不应该回滚。
不确定是什么原因导致了这个回滚。

数据库表上观察到行锁定? - harshavmb
@harshavmb 表锁 - vels4j
1
表锁,理想情况下我们不应该看到应用程序持有表锁,因为它们不应该运行DDL操作。是否有在数据库上运行备份、索引创建等DDL操作并持有表锁? - harshavmb
1
你在这个事务中到底在做什么都没有展示出来。在EclipseLink中打开日志记录,以查看在锁定超时之前它发出的SQL,并检查你的方法,因为如果你不展示在createNewRWRetailTransaction和使用JPA的任何其他调用中正在做什么,我们无法提供帮助。 - Chris
1
Spring 应该重用相同的 EntityManager 和数据库连接,因此不应在自身上进行锁定 - 但我不熟悉 Spring,并且您没有显示任何日志,所以我无法确定。即使错误堆栈跟踪也可能告诉您错误来自哪里,并指向为什么在方法认为已成功返回后仍然回滚。顺便问一下,RWCompartmentRestExceptionMapper 类捕获此异常有什么作用? - Chris
显示剩余7条评论
2个回答

3
Derby对于INSERT语句锁定单个行,将每行保留直到事务提交。(如果表关联有索引,则也会锁定前一个键。)
所以对于您的问题,我的理解是您尝试在一个事务中向Derby插入两条记录,当您执行create(peTxn);时,Derby将锁定单个行并保留每行直到事务提交[lockA],然后您尝试执行create(fcTxn),它将尝试获取另一个单行锁以插入新记录,但实际上事务尚未提交,因此锁仍然保持[lockA]。
因此解决方案是:
在第一步完成后调用getEntityManager().refresh来提交事务,这将使SQL INSERT集中到数据库中。
建议在服务层中使用@Transactional

peTxn和FcTxn必须一起提交。持久化期间不使用刷新。 - vels4j
@vels4j 你可以试一试,如果这个解决方案真的修复了你的问题,那么接下来,你可以添加额外的代码来保持一致性 - Liping Huang
这里的“transaction”似乎是一个实体,代码试图在同一数据库事务中插入两个实体。如果正确连接,这应该没问题。 - Chris

1
问题出在PersistenceContextType.EXTENDED上。我进行了两个更改,问题得到了解决。
  1. PersistenceContextType.EXTENDED changed to PersistenceContextType.TRANSACTION
  2. @Singleton changed to @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)

    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    @Component
    public class RWTransactionDao {
    
        @PersistenceContext(type = PersistenceContextType.TRANSACTION)
        private EntityManager em;
    
        @Override
        protected EntityManager getEntityManager() {
            return em;
        }
    
        @Transactional
        public void createOrderTxns(RWRetailTransaction peTxn, RWRetailTransaction fcTxn) {
            create(peTxn);
            create(fcTxn);
            log.debug("Committed Transaction : {} ", peTxn.displayString());
            log.debug("Committed Transaction : {} ", fcTxn.displayString());
        }
    
        @Transactional
        public void create(T entity) {
            getEntityManager().persist(entity);
        }
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接