不支持事务
local json = require("cjson")
function db_exec(sql_str)
local res = ngx.location.capture(
'/postgres',
{ args = {sql = sql_str } }
)
local status = res.status
local body = json.decode(res.body)
if status == 200 then
status = true
else
status = false
end
return status, body
end
-- 转账操作,对 ID=100 的用户加 10,同时对 ID=200 的用户减 10。
? local status
? status = db_exec("BEGIN")
? if status then
? db_exec("ROLLBACK")
? end
?
? status = db_exec("UPDATE ACCOUNT SET MONEY=MONEY+10 WHERE ID = 100")
? if status then
? db_exec("ROLLBACK")
? end
?
? status = db_exec("UPDATE ACCOUNT SET MONEY=MONEY-10 WHERE ID = 200")
? if status then
? db_exec("ROLLBACK")
? end
?
? db_exec("COMMIT")Last updated