当前位置: 首页 > 知识库问答 >
问题:

Spring@Transactional提交失败;Deby+Eclipselink

韩靖琪
2023-03-14
<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]
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 

由于此错误,以前的提交被回滚,但它不应该回滚。

不确定导致此回滚的原因。

共有1个答案

汲灿
2023-03-14

Derby为INSERT语句锁定单行,保留每行直到提交事务。(如果存在与表关联的索引,则上一个键也会被锁定。)

对于您的问题,我的理解是,您试图在一个事务中向Derby插入两条记录,所以当您执行create(peTxn);Derby锁定单个行,并保留每一行,直到事务提交[lockA],然后您尝试执行create(fcTxn),它将尝试获得另一个单行锁来插入新记录,但实际上事务没有提交,所以锁仍然锁定[lockA]。

所以解决办法是

 类似资料:
  • 在 提交失败,错误0个文件提交,3个文件提交失败:无法创建'C:/xampp/htdocs/project/. git/index.lock':文件存在 另一个git进程似乎正在该存储库中运行,例如,由“git提交”打开的编辑器。请确保所有进程都已终止,然后重试。如果仍然失败,那么git进程可能已经在此存储库中崩溃:手动删除该文件以继续。 我关闭了所有开放的终端,反复尝试,但没有成功。 谢谢你的建

  • SpringWeb应用程序的事务JUnit测试失败。 具体地说:如果我分别使用maven执行每个测试,它们将运行: 如果我执行 我在其中一个测试中发现JPA错误: 如果我设置surefire插件为每个测试重新创建整个JVM,它们也会运行,这当然需要花费大量的时间。 应用程序设置: Spring,与Spring Roo(因此proxyMode=asjectJ!) Eclipselink作为JPA提供

  • 尝试通过android studio 3.4 canary 1运行apk时收到错误消息。 我试过卸载apk、重启android studio和设备、使缓存失效和重新构建,但似乎都没有效果。

  • 我正在尝试使用安格的HttpClient向Spotify Web APIendpoint发出放置请求,但请求失败,我不确定为什么。请看下面的代码: get请求工作得很好,在请求中设置了Authorization头,它按预期工作。然而,put请求不起作用。在响应中,我收到一条错误消息,上面写着“没有提供令牌”。此外,在Chrome开发工具中,网络选项卡显示带有Authorization头的get请求

  • 我有一个Kafka消费者,我从它消费数据从一个特定的主题,我看到下面的例外。我使用的是Kafka版本。 我添加了这两个额外的消费者属性,但仍然没有帮助: 那个错误意味着什么?我该如何解决它?我需要添加一些其他消费者属性吗?

  • 这是我的第一篇文章,所以我真的必须挠头,因为这里的资源非常好。 SpringMVC 4.2.5。hibernate-core-5.1.0 JDK8 我遇到了一个问题,当离开一个被调用的@Transactional方法时,事务没有回滚。我调用的方法具有适当的设置: 该方法调用dao对象,假设我们保存了大约8个对象(“到数据库”),例如: 对于典型的刀来说,它只是做一些类似的事情: 首先,我从普通PO