Transactions in SQL and @Transaction annotations in Spring

Transactions in SQL and @Transaction annotations in Spring

concept:

官方概念:是访问并可能更新各种数据项的一个程序执行单元(unit)

characteristic:

事务四个特性(ACID)
(1)原子性(事务的步骤集合必须作为一个单一的,不可分割的单元出现。)
(2)一致性(因为这些事务是不可分割的,所以要么执行其全部内容,要么都不执行。)
(3)隔离性(不考虑隔离性问题会产生三个读的问题。可设置为:读已提交;读未提交;可重复读;串行化)
(4)持久性(一旦提交就永久生效,不可回滚。)

Example/Scenario:

1:转账: 一个减少一个增加,要么都完成,要么都不完成。
2:多表查询统计:可以通过事务控制将时间拉起到同一时间节点,保证数据的一致性。
3:一个方法中有多个update、delete、insert操作: 可以通过添加事务保证原子性,操作要么同时成功,要么同时失败。

Business FAQ:

不考虑隔离性问题会产生三个读的问题。可设置为:读已提交;读未提交;可重复读;串行化
1:脏读:一个未提交事务读取到另一个未提交的数据。
2:不可重复读:一个未提交事务读取到另一提交事务修改数据
3:幻读(虚读):一个未提交事务读取到另一提交事务添加数据。

可通过设置事务的隔离级别赖解决读的问题:

insert image description here

About spring's @Transaction annotation

@Transaction注解时spring中基于声明式事务管理基于注解的方式。除了可以基于@Transaction注解还可以
基于基于 xml 配置文件方式
即:
															spring事务管理
				声明式事务管理																	   编程式事务管理
xml配置文件方式			注解方式

Spring中的声明式事务管理,底层使用AOP,所以在某些情况下会失效,需要注意!!!

insert image description hereParameters:
1: propagation transaction propagation behavior (manage multi-transaction method calls. There are propagation behaviors)
REQUIRED default propagation mechanism in spring, applicable to most scenarios
2: ioslation: transaction isolation level
3: timeout: timeout time
(1) transaction It needs to be submitted within a certain period of time. If it is not submitted, it will be rolled back
(2) The default value is -1, and the setting time is calculated in seconds
4: readOnly: whether to read only
(1) Read: query operation, write: add, modify, delete Operation
(2) The default value of readOnly is false, which means that it can be queried, and can be added, modified, and deleted.
(3) Set the value of readOnly to true. After setting it to true, you can only query
5: rollbackFor: Rollback
(1) Set which exceptions occur to perform transactions Rollback
6: noRollbackFor: no rollback
(1) Set which exceptions occur and do not perform transaction rollback

smuggled goods:

	嘿嘿,看看他的本质。spring的事务,也是基于数据库的事务。如果数据库学好了。那么spring的事务这里
就可以直接抓住本质,很好理解,不过是把数据库提供的接口以面向对象的形式展示出来,做了封装和功能增强。
就像是熟悉了http以后,再看springMVC一样。也不过是将http的协议内容封装成了对象来进行操作。如果清楚
了这点,那么只需要熟悉了http,不管是django,flask,springMVC还是别的web框架,都可以很好的去进行
理解学习。
日光底下无新事,
一切都是包装和重演。
嘎嘎嘎。



Guess you like

Origin blog.csdn.net/weixin_43441262/article/details/126824849