简体   繁体   English

在Java EE中重新运行失败的容器管理的事务

[英]Rerunning failed Container-Managed transactions in Java EE

I have a situation with a legacy system which uses Java EE Bean Managed Transactions. 我在使用Java EE Bean托管事务的旧系统中遇到了问题。 It's getting LockAcquisitionException thrown when it's trying to retrieve something it just created. 尝试检索刚刚创建的内容时,将引发LockAcquisitionException

My initial thoughts were this: 我最初的想法是:

@TransactionAttribute(SUPPORTS)
public Item retrieveItem(int id) {
 Item i;
 try {
   i = em.find(Item.class, id);
 } catch (PersistenceException e) {
   if (e.getCause() instanceof LockAcquisitionException) {
     i = retrieveItem(id);
   }
 }
 return i;
}

However - when the recursive call is made, the transaction has already died - and it doesn't seem to create a new one. 但是-进行递归调用时,事务已经终止-并且似乎没有创建新事务。 I've tried different TransactionAttributes , but it doesn't seem to make a difference. 我尝试了不同的TransactionAttributes ,但似乎没有什么不同。 Also tried managing the transaction myself ( em.getTransaction() ), but that's illegal in CMT. 还尝试自己管理事务( em.getTransaction() ),但这在CMT中是非法的。

I'm not looking for an elegant fix - as I said, this is legacy, I just need something that will triage it until the whole thing gets replaced in a couple of months! 我并不是要寻找一个优雅的解决方案-正如我所说的,这是旧的,我只需要将其分类的东西,直到几个月后整个东西都被替换!

Cheers. 干杯。

Try to annotate retrieveItem with @TransactionAttribute(REQUIRES_NEW) : it will then be executed in a new transaction. 尝试注释retrieveItem@TransactionAttribute(REQUIRES_NEW)它会接着在一个新的事务执行。

Note that: 注意:

  1. the the first was has been invalidated (set for rollback) and will never complete 第一个无效(设置为回滚),并且永远不会完成
  2. during the second transaction, changes done in the first transaction are not visible anyway 在第二笔交易期间,在第一笔交易中所做的更改始终不可见

So I don't know if it fits your scenario. 所以我不知道它是否适合您的情况。 But it's the only way I know to do an operation and commit it successfully if the original transaction has been invalidated. 但这是我知道的唯一操作,如果原始事务已失效,则成功进行操作。

Unfortunately the only way I can find to do this is to fix the cause of the transaction - so now I'm doing a em.flush() at the beginning of retrieveItem() . 不幸的是,我能找到的唯一方法是解决事务的原因-所以现在我在retrieveItem()的开始处执行em.flush() retrieveItem() Can't wait to replace this app.. 等不及要替换此应用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM