TransactionRolledbackLocalException访问@Singleton时客户端的事务中止 [英] TransactionRolledbackLocalException Client's transaction aborted when accessing @Singleton

查看:130
本文介绍了TransactionRolledbackLocalException访问@Singleton时客户端的事务中止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:花了比我想找到的原因多得多的时间后,以简短的形式添加了这个问题并给出了答案.希望我能减轻别人的痛苦.

将方法调用委派给以@Singleton注释的EJB时,容器会沿着以下行抛出Exception:

When delegating a method call to an EJB annotated with @Singleton, the container throws an Exception along the lines of:

TransactionRolledbackLocalException Client's transaction aborted 

Singleton bean没有发生数据访问.

The Singleton bean has no data access occurring.

ServiceBeanImpl.java

@Stateless
@Local
public class ServiceBean extends BaseBean{ 
  @EJB private CacheService cacheService;

  public FooObj getFooFromCache(int id) {
    FooObj fooObj = (FooObj) cacheService.get(id);
    if (fooObj == null) {
      fooObj = getEntityById(FooObj.class, id);
      cacheService.put(id, fooObj);  //This throws exception
    }
    return cacheService.get(id);
  }
}

CacheServiceImpl.java

@Singleton
@Startup
public class CacheServiceImpl implements CacheService {
    private Cache cache;

    @PostConstruct
    public void init() {
        CacheManager instance = CacheManager.getInstance();
        cache = instance.getCache("cache");
    }

    @PreDestroy
    public void destroy() {
        CacheManager.getInstance().shutdown();
    }

    public Object get(Object id) {
      return cache.get(id);
    }

    public void put(Object id, Object obj) {
      return cache.put(id, obj);
    }
}

问题 为什么调用没有数据访问权限的Singleton bean会引发Transaction异常?

Question Why would calling a Singleton bean that does no data access throw a Transaction exception?

推荐答案

简短答案: 检查先前执行的代码堆栈(假设StackTrace中没有任何线索)以查找被捕获且未传播的异常(因此StackTrace中没有任何线索).

Short answer: check the code stack that executed prior (assuming there are no clues in the StackTrace) for an Exception that is trapped and not propagated (thus no clues in the StackTrace).

在这种特殊情况下,围绕创建命名查询存在try/catch.如果该命名查询失败,则在catch块中执行辅助查询.这些查询不需要事务,因此后备查询正确执行并返回了预期的实体.

In this particular case there was a try/catch around creating a named query. If that named query failed, a secondary query was executed in the catch block. The queries didn't require a transaction, so the fallback query executed properly and returned the expected entities.

但是...

(我认为)这也将交易标记为需要回滚. @Singleton bean有点像红色鲱鱼.关于将控制权转移到该bean的某种事情使容器检查了Transaction,这又在当时引发了异常,但是该异常是适当的,因为该事务不再有效.

That also (I think) marks the transaction as needing a rollback. The @Singleton bean is somewhat of a red-herring. Something about transferring control to that bean made the container check the Transaction, which in turn threw the exception at that point, but the exception was appropriate since the transaction was no longer valid.

故事的道德:

  • 将您的方法追溯到上一次数据库交互,因为这可能是您的问题所在.
  • 仅仅因为上一次数据库交互返回的数据并不意味着它没有问题(这对我来说是致命的).

这篇关于TransactionRolledbackLocalException访问@Singleton时客户端的事务中止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆