首页 文章

Spring数据jpa嵌套事务回滚没有删除插入?

提问于
浏览
1

我尝试在事务中的数据库中插入2条记录 . 第二个插入失败,但在回滚中,第一个插入不会被删除:

@Resource
      private WebMessageRep rep; //<-- extends JpaRepository

@Transactional
  public void test() {
    WebMessage wm = new WebMessage(.valid params.);
    wm = rep.save(wm);//<-- save is crud save which is transactional by default
    WebMessage wm2 = new WebMessage(.invalid params.);
    rep.save(wm2);
  }

(我也尝试用以下方法替换save方法:jpaContext.getEntityManagerByManagedType(WebMessage.class).persist(wm);这样我就不会使用crud save,但问题仍然存在)

我启用了事务日志记录以查看发生了什么,我发现:

在调用test()之前,会创建一个新事务,因为@transactional annotaion:

Creating new transaction with name [com..data.webmessage.WebMessageServiceImpl.test]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
 Opened new EntityManager [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])] for JPA transaction

调用第一个保存,查看第一个事务:

Found thread-bound EntityManager [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])] for JPA transaction

第二次保存也看到第一笔交易:

Found thread-bound EntityManager [SessionImpl(PersistenceContext[entityKeys=[EntityKey[com..shared.WebMessage#107]],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=1} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])] for JPA transaction
2017-02-22 14:07:22,000 [           main] DEBUG orm.jpa.JpaTransactionManager             - Participating in existing transaction

退出test()时,提交完成:

Committing JPA transaction on EntityManager [SessionImpl(PersistenceContext[entityKeys=[EntityKey[com..shared.WebMessage#108], EntityKey[com..shared.WebMessage#107]],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=2} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])]

它失败:

Column 'text' cannot be null
 HHH000010: On release of batch it still contained JDBC statements
HHH000346: Error during managed flush [org.hibernate.exception.ConstraintViolationException: could not execute statement]
    Initiating transaction rollback after commit exception

滚回来:

Rolling back JPA transaction on EntityManager [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=ExecutableList{size=0} updates=ExecutableList{size=0} deletions=ExecutableList{size=0} orphanRemovals=ExecutableList{size=0} collectionCreations=ExecutableList{size=0} collectionRemovals=ExecutableList{size=0} collectionUpdates=ExecutableList{size=0} collectionQueuedOps=ExecutableList{size=0} unresolvedInsertDependencies=null])]

奇怪的是,第一个插入的记录仍然在我的数据库(mysql)中 .

不确定它是否意味着什么,但在提交时我们有:insertions = ExecutableList 但在回滚时它是:insertions = ExecutableList

有人知道它为什么不回滚第一个插入物吗?

我的事务配置非常简单:

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="emf" />
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />

解雇我的调试器后发现,当尝试回滚时,我的事务不再处于活动状态 . 让我解释:

[JpaTransactionManager.java]

@Override
    protected void doRollback(DefaultTransactionStatus status) {
        JpaTransactionObject txObject = (JpaTransactionObject) status.getTransaction();
        if (status.isDebug()) {
            logger.debug("Rolling back JPA transaction on EntityManager [" +
                    txObject.getEntityManagerHolder().getEntityManager() + "]");
        }
        try {
            EntityTransaction tx = txObject.getEntityManagerHolder().getEntityManager().getTransaction();
            if (tx.isActive()) {
                tx.rollback();
            }
        }
        catch (PersistenceException ex) {
            throw new TransactionSystemException("Could not roll back JPA transaction", ex);
        }
        finally {
            if (!txObject.isNewEntityManagerHolder()) {
                // Clear all pending inserts/updates/deletes in the EntityManager.
                // Necessary for pre-bound EntityManagers, to avoid inconsistent state.
                txObject.getEntityManagerHolder().getEntityManager().clear();
            }
        }
    }

上面代码中的tx.isActive()返回false,这意味着不执行回滚 .

现在最大的问题是为什么我的交易不再活跃了?

1 回答

  • 1

    好吧,似乎问题是mysql,spring数据jpa生成的表是myisam类型 .

    在使用某些类型的交易时,似乎myisam变得不稳定 .

    我将我的表转换为innodb,现在它可以正常工作:当事务失败并且事务被回滚时,所有插入的行都被删除 . 当表是myisam类型时,不会发生这种情况 .

相关问题