【发布时间】:2008-11-15 16:06:29
【问题描述】:
我有一张如下表:
transaction_id
user_id
other_user_id
trans_type
amount
此表用于维护金融类应用的账户交易。
它的复式记账,所以从用户 A 到 B 的转移会在表中插入两行。
1, A, B, Sent, -100
1, B, A, Received, 100
任何帐户的余额都是通过汇总该帐户的交易来计算的。
例如:
select sum(amount) from transactions where user_id=A
锁定资金转移的最佳方式是什么?我当前的代码如下所示:
Start Transaction
Debit the sender's account
check the balance of the sender's account
if new balance is negative then the sender didn't have enough money and rollback
if the balance is positive then credit the receiver and commit
这似乎没有完全按预期工作。我在网上看到很多关于交易的例子,基本上都是:开始、借方、贷方、提交。但是检查发件人之间余额的最佳方法是什么?
我有一些不应该通过的交易。假设一个用户有 3K 的余额,并且两笔交易同时进入 3K,这两个交易都在只有一个应该通过的时候通过。
谢谢
【问题讨论】:
标签: mysql transactions