我有一个方法addEvent()
,它正在从webhook REST控制器添加事件记录:
public void addEvent(String subscriptionToken, DateTime eventTimestamp, SubscriptionEventType subscriptionEventType) {
LOGGER.debug("Adding event: " + subscriptionEventType.name());
this.ctx.transaction(configuration -> {
// Transaction code ..
});
}
我面临的问题是,如果同时有两个webhook进入DataAccessException
:
[http-bio-8080-exec-6] DEBUG com.mz.server.rest.braintree.webhooks.BraintreeWebhooksRestController - Webhook kind: SUBSCRIPTION_WENT_ACTIVE
[http-bio-8080-exec-6] DEBUG com.mz.server.repository.jooq.payment.PaymentGatewaySubscriptionEventRepository - Adding event: WENT_ACTIVE
[http-bio-8080-exec-6] DEBUG com.mz.server.spring.SpringTransactionProvider - ##### begin #####
[http-bio-8080-exec-6] INFO com.mz.server.spring.SpringTransaction - SpringTransaction Ctor()
[http-bio-8080-exec-3] DEBUG com.mz.server.rest.braintree.webhooks.BraintreeWebhooksRestController - Webhook kind: SUBSCRIPTION_CHARGED_SUCCESSFULLY
[http-bio-8080-exec-3] DEBUG com.mz.server.repository.jooq.payment.PaymentGatewaySubscriptionEventRepository - Adding event: CHARGED_SUCCESSFULLY
[http-bio-8080-exec-3] DEBUG com.mz.server.spring.SpringTransactionProvider - ##### begin #####
[http-bio-8080-exec-3] INFO com.mz.server.spring.SpringTransaction - SpringTransaction Ctor()
[http-bio-8080-exec-6] DEBUG com.mz.server.spring.SpringTransactionProvider - ##### commit #####
[http-bio-8080-exec-6] DEBUG com.mz.server.spring.auth.CustomHttpSessionListener - Unhandled event
[http-bio-8080-exec-3] DEBUG com.mz.server.spring.SpringTransactionProvider - ##### rollback #####
[http-bio-8080-exec-3] ERROR com.mz.server.rest.braintree.webhooks.BraintreeWebhooksRestController -
org.jooq.exception.DataAccessException: SQL [update `mz_db`.`payment_gateway_subscription` set `mz_db`.`payment_gateway_subscription`.`subscription_event_type_id` = ? where `mz_db`.`payment_gateway_subscription`.`id` = ?]; Deadlock found when trying to get lock; try restarting transaction
at org.jooq.impl.Utils.translate(Utils.java:1690)
at org.jooq.impl.DefaultExecuteContext.sqlException(DefaultExecuteContext.java:660)
at org.jooq.impl.AbstractQuery.execute(AbstractQuery.java:356)
at org.jooq.impl.AbstractDelegatingQuery.execute(AbstractDelegatingQuery.java:133)
at com.mz.server.repository.jooq.payment.PaymentGatewaySubscriptionEventRepository.lambda$0(PaymentGatewaySubscriptionEventRepository.java:73)
at org.jooq.impl.DefaultDSLContext$1.run(DefaultDSLContext.java:370)
at org.jooq.impl.DefaultDSLContext$1.run(DefaultDSLContext.java:367)
at org.jooq.impl.DefaultDSLContext.transactionResult(DefaultDSLContext.java:339)
at org.jooq.impl.DefaultDSLContext.transaction(DefaultDSLContext.java:367)
我该怎么办?
我不明白为什么我会这样做。我正在访问的行似乎被锁定,而下一个线程就是扼流圈。有没有理智的方法来处理这个问题?这是我第一次看到这个例外。由于这是一个更大的Web应用程序的后端,我真的不希望在生产中看到类似的东西......虽然目前我担心如果我什么都不做就会发生这种情况。