mysql模擬事務轉賬

2021-10-12 17:16:56 字數 2395 閱讀 5511

測試事務

建立表

create

table

`account`

(`id`

int(3)

notnull

auto_increment

,`name`

varchar(30

)not

null

,`money`

decimal(9

,2)not

null

,primary

key(

`id`))

engine

=innodb

default

charset

= utf8

插入資料:

insert

into

`account`

(`name`

,`money`

)values

('a'

,2000.00),

('b'

,100000.00

)

模擬轉賬事務:

set autocommit =

0-- 關閉自動提交

start

transaction

-- 開啟乙個事務(一組事務)

update

`account`

set money=money-

500where

`name`

='a'

-- a減500

update

`account`

set money = money+

500where

`name`

='b'

-- b加500

commit

;-- 提交事務就被持久化了

rollback

;-- 回滾

0-- 關閉自動提交

start

transaction

-- 開啟乙個事務(一組事務)

update

`account`

set money=money-

500where

`name`

='a'

-- a減500

update

`account`

set money = money+

500where

`name`

='b'

-- b加500

commit

;-- 提交事務就被持久化了

rollback

;-- 回滾

set autocommit =

1;

Mysql模擬事務隔離級別步驟

1 查詢當前會話事務隔離級別 select session.tx isolation 2 設定事務隔離級別 set session transaction isolation level 事務隔離級別 3 開啟事務 set autocommit 0 begin 4 執行sql,檢視結果 5 提交事務...

事務例子 模擬轉賬

需求 需求 有乙個轉賬功能 service transfer to,from,money reducebalance to,money 模擬錯誤i 5 0 increasebalance from,money dao reducebalance userid,money increasebalanc...

事務(轉賬功能)

轉賬時必須雙方同時成功,要麼同時失敗,就是把雙方組成單元放到事務中 1 顯示的開啟乙個事務 start transaction 2 事務提交 commit代表從開啟事務到事務提交 中間的所有的sql都認為有效 真正的更新資料庫 3 事務的回滾 rollback 代表事務的回滾 從開啟事務到事務回滾 ...