对于两张表,需要顺序操作,必须全部表均操作成功才可,否则两张表不操作。


例如,现在有device,collectionpoint两张表,向两张表顺序执行insert操作
SQL如下
  1. INSERT INTO device (dev_id, tenant_id, user_id)
  2. VALUES
  3. ('bangjile', 909, 909);
  4. INSERT INTO collectionpoint (
  5. device_id,
  6. tenant_id,
  7. point_name
  8. )
  9. VALUES
  10. (808,808,'ceshihahah');
这种方式下,必须两条语句均成功才可,否则均不成功

使用JPA进行操作
现有device的repository
  1. @Repository
  2. public interface DeviceEntityRepository extends JpaRepository<DeviceEntity,Integer> {}
collectionpoint的repository
  1. @Repository
  2. public interface CollectionPointRepository extends JpaRepository<CollectionpointEntity,Integer> {}

新建一个测试repository,用来测试事务操作
  1. @Repository
  2. public class TestRepository {
  3. @Autowired
  4. DeviceEntityRepository deviceEntityRepository;
  5. @Autowired
  6. CollectionPointRepository repository;
  7. public void testMethod(){
  8. DeviceEntity deviceEntity=new DeviceEntity();
  9. deviceEntity.setDevId("gogogo12");
  10. deviceEntity.setTenantId(88);
  11. deviceEntity.setUserId(88);
  12. deviceEntityRepository.save(deviceEntity);
  13. CollectionpointEntity collectionpointEntity=new CollectionpointEntity();
  14. collectionpointEntity.setDeviceId(8);
  15. collectionpointEntity.setTenantId(88);
  16. collectionpointEntity.setPointName("chongtu11");
  17. repository.save(collectionpointEntity);
  18. }
  19. }

执行该测试方法,两条数据均写入成功。
由于测试方法中的实体类赋值了非空键和唯一键。下面来测试device写入成功,collectionpoint写入冲突的情况
  1. public void testMethod(){
  2. DeviceEntity deviceEntity=new DeviceEntity();
  3. deviceEntity.setDevId("gogogo122");
  4. ...
  5. CollectionpointEntity collectionpointEntity=new CollectionpointEntity();
  6. collectionpointEntity.setDeviceId(8);
  7. collectionpointEntity.setTenantId(88);
  8. collectionpointEntity.setPointName("chongtu11");
  9. repository.save(collectionpointEntity);
  10. }
执行方法,调试窗口显示collectionpoint冲突,但查看数据库显示device表中新增了gogogo122的数据,说明整个testMethod方法不具有事务性

事务实现:
只需要在testMethod上方添加一句注解即可
  1. @Transactional(readOnly = false, rollbackFor = Throwable.class)
  2. public void testMethod(){
  3. ...
  4. }
现在再次修改device实体数据,使写入非冲突,collectionpoint实体保持冲突。
执行方法,调试窗口显示collectionpoint冲突,但数据库中device表中为增加新数据。
调试信息如下,显示了jpa的整个过程
  1. 06-02 18:03:51.386 DEBUG 11624 --- [nio-8089-exec-1] org.hibernate.SQL : insert into public.device (dev_desc, dev_id, dev_manufacture, dev_note, dev_position1, dev_position2, dev_sn, dev_type, emp_id, tenant_id, user_id, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  2. Hibernate: insert into public.device (dev_desc, dev_id, dev_manufacture, dev_note, dev_position1, dev_position2, dev_sn, dev_type, emp_id, tenant_id, user_id, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  3. 2017-06-02 18:03:51.392 DEBUG 11624 --- [nio-8089-exec-1] org.hibernate.SQL : insert into public.collectionpoint (device_id, point_alarm_contacts, point_alarm_flag, point_desc, point_downlimit, point_downlimit2, point_name, point_scale, point_type, point_unit, point_uplimit, point_uplimit2, tenant_id, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  4. Hibernate: insert into public.collectionpoint (device_id, point_alarm_contacts, point_alarm_flag, point_desc, point_downlimit, point_downlimit2, point_name, point_scale, point_type, point_unit, point_uplimit, point_uplimit2, tenant_id, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  5. 2017-06-02 18:03:51.404 ERROR 11624 --- [nio-8089-exec-1] druid.sql.Statement : {conn-10005, pstmt-20004} execute error. insert into public.collectionpoint (device_id, point_alarm_contacts, point_alarm_flag, point_desc, point_downlimit, point_downlimit2, point_name, point_scale, point_type, point_unit, point_uplimit, point_uplimit2, tenant_id, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
  6. org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "collectionpoint_point_name_device_id_tenant_id_key"
  7. 详细:Key (point_name, device_id, tenant_id)=(chongtu11, 8, 88) already exists.
  8. ................................此处为异常信息...................
  9. 2017-06-02 18:03:51.409 DEBUG 11624 --- [nio-8089-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : could not execute statement [n/a]
  10. ................................此处为异常信息...................
  11. 2017-06-02 18:03:51.410 WARN 11624 --- [nio-8089-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 23505
  12. 2017-06-02 18:03:51.410 ERROR 11624 --- [nio-8089-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: duplicate key value violates unique constraint "collectionpoint_point_name_device_id_tenant_id_key"
  13. 详细:Key (point_name, device_id, tenant_id)=(chongtu11, 8, 88) already exists.
  14. 2017-06-02 18:03:51.412 INFO 11624 --- [nio-8089-exec-1] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements
  15. 2017-06-02 18:03:51.413 DEBUG 11624 --- [nio-8089-exec-1] o.h.jpa.spi.AbstractEntityManagerImpl : Mark transaction for rollback
  16. 2017-06-02 18:03:51.415 DEBUG 11624 --- [nio-8089-exec-1] cResourceLocalTransactionCoordinatorImpl : JDBC transaction marked for rollback-only (exception provided for stack trace)
  17. ................................此处为异常信息...................
  18. 2017-06-02 18:03:51.421 DEBUG 11624 --- [nio-8089-exec-1] o.h.jpa.spi.AbstractEntityManagerImpl : Mark transaction for rollback
  19. 2017-06-02 18:03:51.421 DEBUG 11624 --- [nio-8089-exec-1] o.h.e.t.internal.TransactionImpl : rollback() called on an inactive transaction
  20. 2017-06-02 18:03:51.422 DEBUG 11624 --- [nio-8089-exec-1] .m.m.a.ExceptionHandlerExceptionResolver : Resolving exception from handler [public void com.yginsight.adminservice.app.api.TestController.test()]: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [collectionpoint_point_name_device_id_tenant_id_key]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
  21. 2017-06-02 18:03:51.424 DEBUG 11624 --- [nio-8089-exec-1] .w.s.m.a.ResponseStatusExceptionResolver : Resolving exception from handler [public void com.yginsight.adminservice.app.api.TestController.test()]: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [collectionpoint_point_name_device_id_tenant_id_key]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
  22. 2017-06-02 18:03:51.424 DEBUG 11624 --- [nio-8089-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolving exception from handler [public void com.yginsight.adminservice.app.api.TestController.test()]: org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [collectionpoint_point_name_device_id_tenant_id_key]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
  23. 2017-06-02 18:03:51.424 DEBUG 11624 --- [nio-8089-exec-1] o.h.e.jdbc.internal.JdbcCoordinatorImpl : HHH000420: Closing un-released batch
  24. 2017-06-02 18:03:51.428 DEBUG 11624 --- [nio-8089-exec-1] o.s.web.servlet.DispatcherServlet : Could not complete request

Spring Data Jpa 简单使用事务的更多相关文章

  1. spring spring data jpa save操作事务

    整合spring spring data jpa的时候,在save方法上加了@Transactional注解.此时调用springdatajpa save方法并不会真的把数据提交给数据库,而是缓存起来 ...

  2. Spring Data JPA 简单查询--接口方法

    一.接口方法整理速查 下表针对于简单查询,即JpaRepository接口(继承了CrudRepository接口.PagingAndSortingRepository接口)中的可访问方法进行整理.( ...

  3. JAVA入门[20]-Spring Data JPA简单示例

    Spring 对 JPA 的支持已经非常强大,开发者只需关心核心业务逻辑的实现代码,无需过多关注 EntityManager 的创建.事务处理等 JPA 相关的处理.Spring Data JPA更是 ...

  4. Spring Data JPA简单使用

    用Spring Data JPA操作数据库 这份教程教你用Spring Data JPA从关系数据库mysql中存储和提取数据.总结来自https://spring.io/guides/gs/acce ...

  5. spring data jpa 简单使用

    通过解析方法名创建查询 通过前面的例子,读者基本上对解析方法名创建查询的方式有了一个大致的了解,这也是 Spring Data JPA 吸引开发者的一个很重要的因素.该功能其实并非 Spring Da ...

  6. Spring Data JPA简单查询接口方法速查

    下表针对于简单查询,即JpaRepository接口(继承了CrudRepository接口.PagingAndSortingRepository接口)中的可访问方法进行整理.(1)先按照功能进行分类 ...

  7. Spring Data Jpa简单了解

    原文来源:http://www.cnblogs.com/xuyuanjia/p/5707681.html 以下是自己简单整理原有文章,其实就是在原来文章基础上化重点以及可能会有所删减的方式进行整理,需 ...

  8. Spring Data JPA 简单查询--方法定义规则

    一.常用规则速查 1  And 并且2  Or  或3  Is,Equals 等于4  Between  两者之间5  LessThan 小于6  LessThanEqual   小于等于7  Gre ...

  9. Spring Data JPA 简单查询

    一.常用规则速查 1  And 并且2  Or  或3  Is,Equals 等于4  Between  两者之间5  LessThan 小于6  LessThanEqual   小于等于7  Gre ...

随机推荐

  1. iOS - CAReplicatorLayer 的运用

    http://www.cocoachina.com/ios/20151230/14822.html 序 CAReplicatorLayer可以复制自己子层的layer,并且复制的出来的layer和原来 ...

  2. git gc干了啥

    前几天在写升级项目的时候发现./git/objects/pack/下的idx和pack文件是只读的,用java在windows下删除会抛异常,然后把只读属性改掉就好了. 于是就想弄清楚这两个文件的作用 ...

  3. day39-Spring 19-今天的内容总结

  4. 【风马一族_mysql】mysql基本指令

    船停在港湾是很安全的,但那不是造船的目的! 用户 创建用户 mysql>grant 权限(select,insert,update,delete) on  数据库.数据表  to  用户名@电脑 ...

  5. codechef Heavy-light Decompositions

    Heavy-light Decompositions Problem Code: HLDOTSSubmit All submissions for this problem are available ...

  6. oracle回滚机制深入研究

    这篇文章主要描写叙述oracle的回滚机制,篇幅可能较长,由于对于oracle的回滚机制来说,要讨论和描写叙述的实在太多,仅仅能刷选自己觉得最有意义的一部分进行深入研究和分享 一.我们来看一个DML语 ...

  7. python 异常处理技巧

  8. Java练习 SDUT-2787_加密术

    加密术 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 加密技术是一种常用的安全保密手段,利用加密技术可以把重要的数据变 ...

  9. tensorflow兼容处理 tensorflow.compat.v1

    https://www.wandouip.com/t5i183316/ 引言 原来引用过一个段子,这里还要再引用一次.是关于苹果的.大意是,苹果发布了新的开发语言Swift,有非常多优秀的特征,于是很 ...

  10. 逗号分隔的字符串转换为Python中的列表 split

    将逗号分隔的字符串转换为Python中的列表   给定一个字符串: 它是由逗号分隔的几个值的序列: mStr = '192.168.1.1,192.168.1.2,192.168.1.3' 如何将字符 ...