ObjectOptimisticLockingFailureException和RollbackException

时间:2018-04-25 07:00:28

标签: java hibernate jpa

我遇到了hibernate和jpa的问题。

我有一个定期调用的方法,用于从数据库中的表中检索数据并将其发送到另一个服务器。

以下是该方法的代码:

protected List<MessagingMessage> getNewMessages() throws NoMessageAvailableException {
    return messagingPublisher.getNewMessages(application);
}

以下是发布商的代码:

@Override
@Transactional(noRollbackFor = {ObjectOptimisticLockingFailureException.class})
public List<MessagingMessage> getNewMessages(String application) throws NoMessageAvailableException {
    List<GpsDataToSend> allGpsDatas = gpsDataToSendService.findByapplicationIgnoreCase(application);
    List<MessagingMessage> allMessages = new ArrayList<>();
    for (GpsDataToSend gpsDataToSend : allGpsDatas) {
        MessagingMessage message = convertToMessagingMessage(gpsDataToSend);
        // Ajout dans les messages à envoyer au TMS
        allMessages.add(message);
        try {
            // Suppression des données de la base
            gpsDataToSendService.deleteGpsDataToSend(gpsDataToSend.getId());
        } catch (ObjectOptimisticLockingFailureException oolfa) {
            // Si l'exception est levée, c'est que la donnée a été supprimée, donc déjà été envoyée au TMS
            allMessages.remove(message);
            Long idGpsDataToSend = ((GpsDataToSend) message.getObject()).getId();
            log.info("The GPS message was already sent because the data doesn't exists anymore in database (id : {})", idGpsDataToSend);
        }
    }
    return allMessages;
}

发布者检索数据,将其转换为消息,并从数据库中删除数据,以确保它只发送一次。

问题是我在负载均衡中有2台服务器执行相同的代码,因此2台服务器可能同时检索相同的数据。有时,在删除时,我发生了一个ObjectOptimisticLockingFailureException,告诉我没有要删除的数据。 我刚刚处理了这个例外,以确保它没有执行剩余的执行。

但现在,当发生ObjectOptimisticLockingFailureException时,在下次执行时:

protected List<MessagingMessage> getNewMessages() throws NoMessageAvailableException {
    return messagingPublisher.getNewMessages(application);
}

我得到了这个例外:

2018-04-24 15:58:09 ERROR [pool-3-thread-1] c.g.m.m.w.WepSphereMqQueuePusher [WepSphereMqQueuePusher.java:76] Error getting new message to send org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:526) ~[spring-orm-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:761) ~[spring-tx-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:730) ~[spring-tx-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:518) ~[spring-tx-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:292) ~[spring-tx-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) ~[spring-aop-4.3.14.RELEASE.jar:4.3.14.RELEASE]
at com.sun.proxy.$Proxy75.getNewMessages(Unknown Source) ~[na:na]
at com.geodisbm.mobility.messaging.websphere.WepSphereMqQueuePusher.getNewMessages(WepSphereMqQueuePusher.java:101) [messaging-1.0-SNAPSHOT.jar:na]
at com.geodisbm.mobility.messaging.websphere.WepSphereMqQueuePusher.sendAllMessages(WepSphereMqQueuePusher.java:71) [messaging-1.0-SNAPSHOT.jar:na]
at com.geodisbm.mobility.messaging.websphere.WepSphereMqQueuePusher.access$000(WepSphereMqQueuePusher.java:16) [messaging-1.0-SNAPSHOT.jar:na]
at com.geodisbm.mobility.messaging.websphere.WepSphereMqQueuePusher$1.run(WepSphereMqQueuePusher.java:63) [messaging-1.0-SNAPSHOT.jar:na]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_45]
at java.util.concurrent.FutureTask.runAndReset$$$capture(FutureTask.java:308) [na:1.8.0_45]
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java) [na:1.8.0_45]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_45]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_45]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_45]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_45]
at java.lang.Thread.run(Thread.java:745) [na:1.8.0_45]
Caused by: javax.persistence.RollbackException: Transaction marked as rollbackOnly
at org.hibernate.jpa.internal.TransactionImpl.commit(TransactionImpl.java:58) ~[hibernate-entitymanager-5.1.12.Final.jar:5.1.12.Final]
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:517) ~[spring-orm-4.3.14.RELEASE.jar:4.3.14.RELEASE]
... 20 common frames omitted

如果发生ObjectOptimisticLockingFailureException,则事务将标记为rollbackonly。所以我试图说这个事务没有回滚这个异常(通过注释),但它没有工作。

你知道它为什么不起作用吗?

感谢。

1 个答案:

答案 0 :(得分:2)

最有可能发生此问题是因为getNewMessages调用了一些事务性方法,而事务管理器的属性globalRollbackOnParticipationFailure设置为true(这是默认值)。

以下是javadoc的相关内容:

 Set whether to globally mark an existing transaction as rollback-only
 after a participating transaction failed.
 <p>Default is "true": If a participating transaction (e.g. with
 PROPAGATION_REQUIRES or PROPAGATION_SUPPORTS encountering an existing
 transaction) fails, the transaction will be globally marked as rollback-only.
 The only possible outcome of such a transaction is a rollback: The
 transaction originator <i>cannot</i> make the transaction commit anymore.
 <p>Switch this to "false" to let the transaction originator make the rollback
 decision. If a participating transaction fails with an exception, the caller
 can still decide to continue with a different path within the transaction.
 However, note that this will only work as long as all participating resources
 are capable of continuing towards a transaction commit even after a data access
 failure: This is generally not the case for a Hibernate Session, for example;
 neither is it for a sequence of JDBC insert/update/delete operations.

您已在外部交易中设置noRollbackFor,但

  1. getNewMessages永远不会抛出ObjectOptimisticLockingFailureException
  2. 更重要的是,当某个其他方法(我猜deleteGpsDataToSend注释为@Transactional)抛出该异常时,事务被标记为rollbackOnly。