可以这样写来保证两条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>
算是分布式事务吧?
以上是我自己的一点思考,若有错误还请指点。