接缝@Transactional注释不起作用? [英] Seam @Transactional annotation not working?

查看:138
本文介绍了接缝@Transactional注释不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在接缝组件上使用@Transactional批注,类似于:

I'm using the @Transactional annotation on a seam component similar to:

@Name( "myComponent" )
@AutoCreate
public class MyComponent
{
    public void something() {
        ...
        doWork();
    }
    ...
    @Transactional
    protected void doWork() {
        try {
            log.debug( "transaction active: " + Transaction.instance().isActive() );
        } catch (Exception ignore) {}

        // some more stuff here that doesn't appear to be inside a transaction
    }
}

在更多内容"部分中,我正在修改一些Hibernate实体,然后出现了一个引发Exception的错误.我注意到,该异常并未导致事务回滚(修改后的实体仍在数据库中进行了修改),因此我添加了事务活动"日志记录.执行此代码时,isActive()返回false.

In the "some more stuff" section, I'm modifying some Hibernate entities and then had a bug where an Exception was thrown. I noticed that the Exception wasn't causing the transaction to be rolled back (the modified entities were still modified in the db) so I added the "transaction active" logging. When this code executes, isActive() returns false.

有什么我想念的吗?为什么交易不活跃?

Is there something I'm missing? Why isn't the transaction active?

如果有问题,我将从另一个使用RESTEasy批注来触发我的方法调用的组件内部使用Seam组件.

In case it matters, I'm using the Seam component from inside another component that is using RESTEasy annotations to trigger my method calls.

推荐答案

我对Seam的工作方式并不熟悉,因此如果此答案不适用,我会提前道歉.

I'm not familiar with how Seam works so my apologies in advance if this answer does not apply.

我注意到@Transactional的方法是protected.在我看来,这意味着它是由另一个内部方法调用的.

I noticed that the method that is @Transactional is protected. This implies to me that it is being called by another internal method.

在Spring的AOP中,用@Transactional标记 public 方法,这些方法被包装并替换为事务代理.当外部类调用public方法时,它正在调用构成事务的代理.如果外部类调用另一个标记为@Transactional not public方法,然后调用一个内部方法,则不会创建任何事务,因为根本没有调用代理.

With Spring's AOP, you mark the public methods with @Transactional which are wrapped and replaced with a transaction proxy. When an external class calls the public method, it is calling the proxy which forms the transaction. If the external class calls another public method that is not marked with @Transactional which then calls an internal method which is, there will be no transaction created because the proxy is not being called at all.

在Spring中,即使将doWork()方法更改为公开方法,也会发生相同的问题.没有事务,因为未调用代理对象.在类内部进行的方法调用不会对代理对象进行调用.

In Spring, even if you change your doWork() method to be public, the same problem would happen. No transaction because the proxy object is not being called. Method calls made inside of the class are not making calls to the proxy object.

快速阅读一些文档似乎表明,就像Spring AOP一样,Seam正在使用 CGLib代理.问题是,它是否能够代理所有方法-即使从代理对象内部调用它们也是如此.如果此答案不适用,很抱歉浪费您的时间.

A quick read of some documentation seems to indicate that, like Spring AOP, Seam is using CGLib proxying. The question is if it is able to proxy all methods -- even if they are called from within the proxied object. Sorry for wasting your time if this answer does not apply.

这篇关于接缝@Transactional注释不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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