用Spring boot jpa update modify delete 数据和事务管理的那些坑

有好多文章介绍了Spring boot 博客已经很多了比如说:
http://blog.csdn.net/linzhiqiang0316/article/details/52639265
http://blog.csdn.net/linzhiqiang0316/article/details/52638039
http://blog.didispace.com/springbootmultidatasource/等等
上面几个博客都是有关查询的一些例子,但是今天我们的是jpa删除和事务的一些坑
使用的spring boot版本:1.3.7.RELEASE

一.业务场景(在线考试系统)和代码:

业务逻辑:根据问题的id删除答案

repository层:

  int deleteByQuestionId(Integer questionId);

service 层:

public void deleteChoiceAnswerByQuestionId(Integer questionId) {
    choiceAnswerRepository.deleteByQuestionId(questionId);

test(测试)层:

   @Test
    public void testDeleteByQuestionId() {

        choiceAnswerService.deleteChoiceAnswerByQuestionId(5);
        System.out.println("hehehhe");
        System.out.println("hehehhe");

        System.out.println("hehehhe");

        System.out.println("hehehhe");
        System.out.println("hehehhe");
        System.out.println("hehehhe");
        System.out.println("hehehhe");


    }

二.问题

问题1:对于modify和delete时如果各层都不加事务管理的话会报这个错误

org.springframework.dao.InvalidDataAccessApiUsageException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call; nested exception is javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'remove' call 

当我们除了query外的modiy和delete如果各层的方法中没有进行事务管理的话(没加@Transactional)话会报错

问题2:只在test层加@Transactional

没有错误但是数据并没有被删除,在用IDEA的调试是,在执行这个测试方法的过程时还可以在choiceanswer表中进行操作并没有加锁事务并没有起作用

问题3:只在 Repository层加@Transactional

 public void deleteChoiceAnswerByQuestionId(Integer questionId) {
        choiceAnswerRepository.deleteByQuestionId(questionId);
        System.out.println("hehehhe");

        System.out.println("hehehhe");
//        questionRepository.delete(5);
        System.out.println("hehehhe");

        System.out.println("hehehhe");
        System.out.println("hehehhe");
        System.out.println("hehehhe");
        System.out.println("hehehhe");
    }

这时当执行完
choiceAnswerRepository.deleteByQuestionId(questionId);
数据里面被修改,修改成功

问题4:只在 service层加@Transactional

当只有执行完service内的对应方法时数据才会被删除 (只有在这一层加事务管理才是真正的事务管理)

问题5:在service 层和repository层都加上@transactional

当只有执行完service内的对应方法时数据才会被删除

问题6:只有在test层加上@Transactional,不管service层和repository层加不加@Transactional(可以加可以不加)

数据都不会被删除

问题7:repository层不同注解的执行结果

@Modifying
@Query("delete from ChoiceAnswer c where c.question.id=?1 ")
@Transactional
int deleteByQuestionId(Integer questionId);
与
@Transactional
int deleteByQuestionId(Integer questionId);

有什么区别,上面的会直接执行delete语句
下面的会先执行select 再执行delete
#总结:
事务管理只有在service加上事务管理才起作用,query不需要事务管理但是delete update modify都需要事务管理。为了不在service层不加事务管理可以在repository层的delete update modify加上@transactional 但这样不能真正保持事务的完整性

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用Spring BootJPA实现多数据源时,需要进行以下步骤: 1. 配置数据源:在application.properties文件中,配置多个数据源的连接信息。每个数据源都需要设置独特的前缀。 2. 创建多个数据源Bean:在代码中,使用@Configuration和@Bean注解创建多个数据源的实例。每个数据源的实例需要设置对应的连接信息。 3. 创建EntityManagerFactory:使用LocalContainerEntityManagerFactoryBean创建多个EntityManagerFactory实例。每个实例需要设置对应的数据源和持久化单元。 4. 创建JpaTransactionManager:使用PlatformTransactionManager的实现类JpaTransactionManager创建多个事务管理器实例。每个实例都需要设置对应的EntityManagerFactory。 5. 定义Repository:创建多个Repository接口,并分别使用@PersistenceContext注解注入不同的EntityManager实例。 6. 配置事务:使用@EnableTransactionManagement注解启用事务管理,并使用@Transactional注解定义事务的范围。 7. 使用不同的数据源:在代码中使用@Qualifier注解指定需要使用的数据源。在进行相关数据库操作时,使用相应的Repository来访问对应的数据源。 通过以上步骤,就能够使用Spring BootJPA实现多数据源的配置和使用。每个数据源都可以连接到不同的数据库,对应的Repository可以访问并操作各自的数据源。这样就可以实现在同一个应用程序中对多个数据源的管理和操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值