简体   繁体   English

Spring JPA Repository保存未插入数据库

[英]Spring JPA Repository save not Inserting into the database

I'm launching a spring-boot application which contains an @Service class:我正在启动一个包含@Service类的 spring-boot 应用程序:

@Service
@Transactional (value = MatrixModelConfiguration.TRANSACTION_MANAGER_MATRIX)
public class MatrixTubeService implements IMatrixTubeService {

    @Autowired
    MatrixTubeRepository repository;

    @Override
    public void flush() {
        boolean txn = TransactionSynchronizationManager.isActualTransactionActive();
        System.out.println("flush txn exist " + txn);
        repository.flush();
    }

    @Override
    public MatrixTube save(MatrixTube matrixTube) {
        boolean txn = TransactionSynchronizationManager.isActualTransactionActive();       
        String txnName = TransactionSynchronizationManager.getCurrentTransactionName();
        System.out.println("save txn exist " + txn + " as " + txnName);
        return repository.save(matrixTube);
    }
 .
 .
 .

wrapping the calls to this simple @EnableJpaRepositories class:包装对这个简单的@EnableJpaRepositories类的调用:

@EnableJpaRepositories(entityManagerFactoryRef = MatrixModelConfiguration.ENTITY_MANAGER_FACTORY_MATRIX, 
                                   transactionManagerRef = MatrixModelConfiguration.TRANSACTION_MANAGER_MATRIX)
            public interface MatrixTubeRepository extends JpaRepository<MatrixTube, Long> {

    }

This code is in a dependent jar and run perfect in 2 out of three parent projects.此代码位于一个依赖 jar 中,并且在三个父项目中的两个中完美运行。 In the case where it doesn't, INSERTs/UPDATEs do not apply to the database.如果不是,则 INSERT/UPDATE 不适用于数据库。 In fact they don't even show in the debug output.事实上,它们甚至没有显示在调试输出中。

  • I've added the a txn manager check in the save and verified the txn is active and available.我在保存中添加了 txn 管理器检查并验证了 txn 处于活动状态且可用。
  • I am not seeing any Exception while running我在运行时没有看到任何Exception
  • All other JpaRepository implementations are working as expected所有其他JpaRepository实现都按预期工作

I added a call to service.flush() directly after a service.save() of a newly created object and again after a few updates a previously read object (within the same txn) and in both instances, I see the txn is active in the flush method, but when repository.flush() is called, I get a "no transaction is in progress" exception.我在新创建的对象的service.save()之后直接添加了对service.flush()的调用,并在几次更新之前读取的对象(在同一个 txn 内)之后再次添加了对service.flush()的调用,在这两种情况下,我都看到 txn 处于活动状态在flush方法中,但是当repository.flush()被调用时,我得到一个“没有正在进行的事务”异常。

What am I missing?我错过了什么?

Maybe spring-test set rollback default value is true,也许 spring-test 设置回滚默认值是 true,

adding @Rollback(false) soveld my problem.添加@Rollback(false)解决了我的问题。

With springdatajpa you do not need all this magic.使用 springdatajpa,您不需要所有这些魔法。 txn etc. txn 等

Use this annotation in you main application class.在您的主应用程序类中使用此注释。

@EnableJpaRepositories(entityManagerFactoryRef = MatrixModelConfiguration.ENTITY_MANAGER_FACTORY_MATRIX, 
                                   transactionManagerRef = MatrixModelConfiguration.TRANSACTION_MANAGER_MATRIX)

Start with this tutorial.从本教程开始。 Spring data jpa is a little magical comparing to standard Hibernate/jpa.与标准 Hibernate/jpa 相比,Spring 数据 jpa 有点神奇。

https://spring.io/guides/gs/accessing-data-jpa/ https://spring.io/guides/gs/accessing-data-jpa/

The problem had to do with spawning a new thread from within the transaction, which actually performed the SQL inserts well after (in computer time) the transaction had been committed.问题与从事务内部产生一个新线程有关,该线程实际上在事务提交后(在计算机时间)执行 SQL 插入。

Not sure why there were no exceptions thrown, but getting the inserts back into the transactional thread fixed everything.不知道为什么没有抛出异常,但是将插入返回事务线程修复了所有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM