简体   繁体   English

EJB Transaction属性覆盖方法级别

[英]EJB Transaction attribute overriding in method-level

I have a CDI conversation scoped action class which I am also making a stateful EJB for holding state of objects throughout the conversation life cycle. 我有一个CDI会话范围的动作类,我也在制作一个有状态的EJB,用于在整个会话生命周期中保持对象的状态。

As the action class is an EJB, so by default all methods will be transactional, which I intentionally don't want to do. 由于动作类是EJB,因此默认情况下所有方法都是事务性的,我有意不想这样做。 I just want a single method of the action class will be transactional where I will only perform database persistence tasks. 我只想要一个action类的单个方法将是transactional,我将只执行数据库持久化任务。 So I annotate that single method with @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW). 所以我使用@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)注释该单个方法。 And the action class is annotated with @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED). Action类使用@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)进行注释。

When I call the transactional method from other handler methods of the same action class, there no transaction starts. 当我从同一个动作类的其他处理程序方法调用事务方法时,没有事务启动。

Here is the code sample. 这是代码示例。

@Stateful
@Named
@ConversationScoped
@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
public class MyAction implements Serializable {

    @PersistenceContext(type = PersistenceContextType.EXTENDED)
    private EntityManager em;
    ........
    ........
    ........

    public String handlerMethod1() {
        // do some staffs.
        persist();
        return "view";
    }

    public String handlerMethod2() {
        // do some staffs.
        persist();
    }

    .......
    .......
    .......

    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    private void persist() {
        // save objects.
        em.flush();
    }
}

No transaction starts when invoked persist() method though I have annotated it with @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW), but why? 调用persist()方法时没有事务开始,虽然我用@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)注释了它,但为什么呢?

Please help me getting rid of this. 请帮我摆脱这个。

You have two problems here: 你有两个问题:

a) persist() is not a business method . a)persist()不是一种商业方法 A Business method have to be (among other rules) public. 商业方法必须(在其他规则中)公开。

b) you are calling the persist() method with a simple object method invocation, therefore, the Container is not able to manage the code. b)您使用简单的对象方法调用调用persist()方法,因此,Container无法管理代码。 Remember that @TransactionAttribute annotation needs to be interpreted by the Container, which doesn't occurs in this case. 请记住,@ TransnsAttribute注释需要由Container解释,在这种情况下不会发生。

One possible solution could be to create other EJB with the persist() method code and inject him in MyAction bean. 一种可能的解决方案是使用persist()方法代码创建其他EJB并将其注入MyAction bean。 This way, every time you invoke the persist() method, the Container will intercept the call and it will create a new transaction. 这样,每次调用persist()方法时,Container都会拦截调用并创建一个新事务。

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

相关问题 在方法之外使用方法级变量 - Using method-level Variables outside of the method 如何在ejb中获得方法级别的事务超时? - How can i get method level transaction time out in ejb? Java:带有自定义批注的方法级授权检查 - Java: Method-level authorization check with custom annotation TestNG:如何不忽略方法级别的@Test(testName) - TestNG: how NOT to ignore @Test(testName) on method-level 是否可以使用注释实现方法级访问检查? - Is it possible to implement method-level access checks with annotations? 使用静态类/方法中的方法级连接的池化数据库访问是否安全? - Is a pooled db-access using method-level connection in a static class/method safe? 使用Wily Introscope对基于Java的应用程序进行方法级跟踪 - Method-level tracing of Java-based application using Wily Introscope 在EJB 2.0中成功交易时调用方法 - Call a method on successful transaction in EJB 2.0 异步ejb方法调用中的事务传播 - Transaction propagation in asynchronous ejb method call 从Application类覆盖getSingletons方法时,Ejb查找失败 - Ejb lookup failing when overriding getSingletons method from Application class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM