Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自动冲销解冻的触发器的并发问题 #2

Open
easonzhu-zyx opened this issue May 10, 2021 · 1 comment
Open

自动冲销解冻的触发器的并发问题 #2

easonzhu-zyx opened this issue May 10, 2021 · 1 comment

Comments

@easonzhu-zyx
Copy link

timer.schedule(new TimerTask() { public void run() { synchronized (payment.getPayId().intern()) { // 使用2分钟之前的Payment到数据库中查出当前的Payment Payment currentPayment = paymentRepository.findById(payment.getId()).orElseThrow(() -> new EntityNotFoundException(payment.getId().toString())); if (currentPayment.getPayState() == Payment.State.WAITING) { log.info("支付单{}当前状态为:WAITING,转变为:TIMEOUT", payment.getId()); accomplishSettlement(Payment.State.TIMEOUT, payment.getPayId()); } } } }, payment.getExpires());

1.先findById,2.再判断状态,3,再执行accomplishSettlement
如果产生并发可能产生在accomplishSettlement之前payment已经被支付了,是否可能产生业务问题。

@JJPeng00
Copy link

synchronized (payment.getPayId().intern())
对payId加锁之后,findById得到的数据是数据库中最新的数据,不会存在findById之后被其他线程修改payment状态的情况

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants