您当前的位置:首页 > 互联网教程

Spring支持的常用数据库事务传播属性和隔离级别

发布时间:2025-05-22 22:05:17    发布人:远客网络

Spring支持的常用数据库事务传播属性和隔离级别

一、Spring支持的常用数据库事务传播属性和隔离级别

那么不知道你对于Spring支持的常用数据库事务传播属性和隔离级别了解得怎么样呢?要不要一起复习复习了:grin:

很喜欢一句话:“八小时内谋生活,八小时外谋发展”

描述:进来先看看风景啦,要相信会有光的哦

对于数据库事务ACID(原子性、一致性、隔离性、持久性)性质我想大家都是知道的,这里就不写了:grin:

我们都知道用事务是为了保证数据库的完整性,保证成批的 SQL语句要么全部执行,要么全部不执行。

但是如果一个方法嵌套关联着其他方法了,这该怎么算呢?当前方法及关联方法都有事务呢,或者只是其中某几个有事务,该用谁的呢?

事务的传播行为:一个方法运行在一个开启了事务的方法上时,当前方法是使用原来的事务还是开启一个新的事务。

通过@Transaction注解中 propagation来设置事务传播行为。其中

下面写了一个小demo来让理解更加快捷一些哈。

注意:account表中 balance字段是设置为无符号的(即不能为负数)。

模拟的是买书的一个过程,账户余额不足,但是一次买多本的情况,一起付款。

在其中再测试事务传播行为的不同,来看数据的变化。

我们在 void checkout(int userId, List isbns)和 void purchase(int userId, int isbn)上都加了@Transactional

目前账户为 100元,两本书的价格分别为 60和50,因为我们的付款过程是使用循环购买的,你说我们会买到一本还是一本都买不到呢?

答案当然是一本都买不到,因为@Transactional注解,默认事务的传播属性是:REQUIRED,即业务方法需要在一个事务中运行。如果方法运行时,已经处在一个事务中,那么加入到该事务,否则为自己创建一个新的事务。所以实际上 void purchase(int userId, int isbn)其实和调用它的方法用的同一个事务。简单画个图:

测试二:测试-->REQUIRES_NEW属性

其他代码未改变,仅在 purchase上的注解上加了点东西@Transactional(propagation= Propagation.REQUIRES_NEW).

REQUIRES_NEW:不管是否存在事务,业务方法总会为自己发起一个新的事务。如果方法已经运行在一个事务中,则原有事务会被挂起,新的事务会被创建,直到方法执行结束,新事务才算结束,原先的事务才会恢复执行。

你说说答案和上面是一样的么?:grinning:

答案是不一样的,测试一我们实际上用的就是checkout上的事务,并没有用到 purchase的事务,从图上也能看出来。

测试二它的事务传播属性用图来讲是这样的啦:

还有很多,意思都解释过了,没有一一测完了。

假设现在有A和B两个事务并发执行。

1)脏读:一个事务读取到另一事务未提交的更新新据

2)不可重复读:同一事务中,多次读取同一数据返回的结果有所不同(针对的update操作)

3)幻读:一个事务读取到另一事务已提交的insert数据(针对的insert操作)

数据库事务的隔离性:数据库系统必须具有隔离并发运行各个事务的能力,使它们不会相互影响,避免各种并发问题.

一个事务与其他事务隔离的程度称为隔离级别.数据库规定了多种事务隔离级别,不同隔离级别对应不同的干扰程度,隔离级别越高,数据一致性就越好,但并发性越弱

1)测试一下 mysql的默认隔离级别:

测试代码特别简单,但因为我是手动模拟,得打断点、debug启动,

当执行完第一个 double bookPrice= bookShopMapper.getBookPriceByIsbn(isbn)语句时,应该去mysql修改一下书的价格,这样看一下结果。

这个时候再接着执行。看输出什么。

最后的结果仍然是50、50。因为mysql的默认事务隔级别是可重复读,意思在这同一个事务中,可以重复读。

注:因为这是直接修改数据库,其操作行为并不可取,此处只是为了模拟。其结果有时也非一定准确。

二、spring 该怎么理解

裂缝,裂开;【航海】倒缆,(甲板的)上翘,转向锚索;【建】起拱点[面]

the springs of one's conduct

Pierian spring学问,知识;诗的源泉,灵感

silent spring沉寂的春天(指因大量使用化学毒剂造成没有飞鸟的春天)

set every spring in motion(=set all springs going)开动所有发条,尽全力

take its springs from[out of]发源于...,始于,来自

with a spring一跳,一骨碌(起来等)

spring forth [out]跳出,冲出,突出,涌出,喷出;突然长出来

spring from突然从...冒出;起源于,出身于;来自

spring on [upon]扑向,袭击,突然提出

spring up跳上来,发生,萌芽,生长,出现

She sprang out of her chair to greet her father.

I have sprung my tennis racket.

spring a leak开始漏水;出现漏洞

春天,跃起,泉,弹簧,发条,弹性,弹力,根源

柯林斯英语大词典查词历史In a fast food storetwo chairs next to the bed动物去公园spring

to move or cause to move suddenly upwards or forwards in a single motion

to release or be released from a forced position by elastic force

to come, issue, or arise suddenly

(of a part of a mechanism, etc) to jump out of place

to make(wood, etc) warped or split or(of wood, etc) to become warped or split

to happen or cause to happen unexpectedly

the idea sprang from a chance meeting

to come into being or appear suddenly

(of a gun dog) to rouse(game) from cover

(of game or quarry) to start or rise suddenly from cover

to appear to have a strong upward movement

the beam springs away from the pillar

to explode(a mine) or(of a mine) to explode

to provide with a spring or springs

to arrange the escape of(someone) from prison

(of daylight or dawn) to begin to appear

the act or an instance of springing

the quality of resilience; elasticity.(as modifier)

the act or an instance of moving rapidly back from a position of tension

a natural outflow of ground water, as forming the source of a stream.(as modifier)

a device, such as a coil or strip of steel, that stores potential energy when it is compressed, stretched, or bent and releases it when the restraining force is removed.(as modifier)

a structural defect such as a warp or bend

the season of the year between winter and summer, astronomically from the March equinox to the June solstice in the N hemisphere and from the September equinox to the December solstice in the S hemisphere.(as modifier).

the earliest or freshest time of something

one of a set of strips of rubber, steel, etc, running down the inside of the handle of a cricket bat, hockey stick, etc

Also called: spring line a mooring line, usually one of a pair that cross amidships

springs sprang sprung springing

1. the season of growth;"the emerging buds were a sure sign of spring";"he will hold office until the spring of next year"

(hypernym) season, time of year

2. a natural flow of ground water

(synonym) fountain, outflow, outpouring, natural spring

(hypernym) geological formation, formation

3. a metal elastic device that returns to its shape or position when pushed or pulled or pressed;"the spring was broken"

4. a light springing movement upwards or forwards

(synonym) leap, leaping, saltation, bound, bounce

(derivation) bounce, resile, take a hop, bound, rebound, recoil, reverberate, ricochet

5. the elasticity of something that can be stretched and returns to its original length

6. a point at which water issues forth

(hypernym) beginning, origin, root, rootage, source

1. move forward by leaps and bounds;"The horse bounded across the meadow";"The child leapt across the puddle";"Can you jump over the fence?"

(derivation) leap, leaping, saltation, bound, bounce

2. develop into a distinctive entity;"our plans began to take shape"

(synonym) form, take form, take shape

3. spring back; spring away from an impact;"The rubber ball bounced";"These particles do not resile but they unite after they collide"

(synonym) bounce, resile, take a hop, bound, rebound, recoil, reverberate, ricochet

(hyponym) kick back, recoil, kick

(derivation) leap, leaping, saltation, bound, bounce

4. produce or disclose suddenly or unexpectedly;"He sprang a new haircut on his wife"

(hypernym) produce, bring on, bring out

5. develop suddenly;"The tire sprang a leak"

(hypernym) grow, develop, produce, get, acquire

6. produce or disclose suddenly or unexpectedly;"He sprang these news on me just as I was leaving"

(hypernym) disclose, let on, bring out, reveal, discover, expose, divulge, impart, break, give away, let out

We agree to comply with the copyright notice and statements mentioned in WordNet license.

同义词: bounce bound hop hurdle jump leap vault

n.春天,弹簧,跳跃,弹性,活力,泉,源泉

vi.跳,弹跳,涌出,生长,裂开,高耸

actuating spring dome spring spring from spring board stabilizer spring multiple spring spring hinge spring gun spring pawl locking spring running spring spring flood packing spring spring control spring housing spring barley oil spring gland spring spring seat spring washer cantilever spring spring fastening

该解释由词友提供,仅供参考。免责声明

网络词典(beta)与 spring相关的例句calendar spring festival picture

Spring Festival Entertainment Programme

submarine hot spring community

spring back; spring away from an impact.

resembling a spring or the action of a spring.

三、SpringBoot+mybatis-plus+人大金仓数据库配置

配置数据源时,首先设定默认数据源或者数据源组为“master”,并启用严格匹配数据源模式,即当未匹配到指定数据源时,系统将抛出异常。具体配置如下:

primary: master#设置默认的数据源或者数据源组,默认值即为master

strict: true#严格匹配数据源,默认false。true未匹配到指定数据源时抛异常,false使用默认数据源

url: jdbc:kingbase8://127.0.0.1:54321/数据库名称?currentSchema=模式&characterEncoding=UTF-8

driver-class-name: com.kingbase8.Driver

另外,需要注意的是,使用dynamic-datasource-spring-boot-starter时,版本至少需要升级到3.5.0。