关于MySQL事务的一些记录

可以这样写来保证两条SQL的原子性,也可以说是事务。

start TRANSACTION
update test01.goods set num = num-20 where id = 1
insert into chat.buy_record(good_id,create_time)values(1,now())
COMMIT

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test01?useUnicode=true&characterEncoding=utf-8&useSSL=false
#&useAffectedRows=true
spring.datasource.username = root
spring.datasource.password = root

#spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#spring.datasource.url = jdbc:mysql://rm-2ze6805lhmr49t263co.mysql.rds.aliyuncs.com:3306/redwineapp?useUnicode=true&characterEncoding=utf-8&useSSL=false
#spring.datasource.username = jingyongchun
#spring.datasource.password = jingyongchun@1

像这样配置两个数据源,但是必须是同一个连接,可以不同库。SQL可以通过库名.表名的形式表示,如下图


	<update id="updateGoodsNum">
		update test01.goods set num = num-#{num} where id = #{goodsId}
	</update>
	
	
	<insert id="addBuyRecord">
		insert into test02.buyback_record(goodsId,create_time)values(#{goodsId},now())
	</insert>
	

算是分布式事务吧?
以上是我自己的一点思考,若有错误还请指点。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值