CDI事务拦截器不起作用 [英] CDI Transactional Interceptor not working

查看:71
本文介绍了CDI事务拦截器不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下类的Java SE应用程序:

I have a Java SE application with these classes:

main:

public static void main(String args[])
    {
        Weld weld = new Weld(); 
        WeldContainer container = weld.initialize(); 

        ShopCar sc = container.instance().select(ShopCar.class).get(); 
        sc.execute();
        weld.shutdown();
    }

我的DAO(未完全实施):

My DAO(not fully implemented):

/**
 *
 * @author vFreitas
 * @param <T> The type T 
 */
public class JpaDAO<T> implements DAO<T>, Serializable
{
    /* The EntityManager of my connection */
    private final EntityManager em;
    /* The class to be persist */
    private final Class<T> classe;

    private ThreadLocal<EntityManager> threadLocal;

    /* Builder */
    /**
    *
    * @author info1
    * @param classe The class to that will represent T
    * @param em A new instance of EntityManager 
    */
    public JpaDAO(Class<T> classe, EntityManager em)
    {
        this.classe = classe;
        this.em = em;
        threadLocal = new ThreadLocal<>();
        threadLocal.set(em);
    }

    @Override
    @Transacional
    public void save(T entity)
    {
        //em.getTransaction().begin();
        em.persist(entity);
        //em.getTransaction().commit();
    }

...

我的DAOFactory:

My DAOFactory:

public class DAOFactory<T>
{
    @Inject @MyDatabase private EntityManager em;

    @SuppressWarnings({ "rawtypes", "unchecked" })    
    @Produces
    public JpaDAO<T> createJpaDAO(InjectionPoint injectionPoint) throws 
            ClassNotFoundException 
    {    
        ParameterizedType type = (ParameterizedType) injectionPoint.getType();    
        Class classe = (Class) type.getActualTypeArguments()[0];   
        return new JpaDAO<>(classe,em);    
    }    
}

拦截器注释:

@Target({ ElementType.METHOD, ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@InterceptorBinding
public @interface Transacional 
{

}

和我的拦截器:

@Interceptor
@Transacional
public class TransacionalInterceptor 
{

    @Inject @MyDatabase
    private EntityManager manager;

    @Inject private ThreadLocal<EntityManager> threadLocal;

    Logger logger = LoggerFactory.getLogger(TransacionalInterceptor.class);

    @AroundInvoke
    public Object invoke(InvocationContext context) throws Exception 
    {
        manager = threadLocal.get();
        //EntityTransaction trx = manager.getTransaction();
        if(!manager.getTransaction().isActive())
        {
            manager.getTransaction().begin();
            System.out.println("Starting transaction");
            Object result = context.proceed();
            manager.getTransaction().commit();
            System.out.println("Committing transaction");
            return result;
        }

beans.xml:

beans.xml:

<beans xmlns="http://java.sun.com/xml/ns/javaee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd" >

    <interceptors>
        <class>shopcar.util.TransacionalInterceptor</class>
    </interceptors>
</beans>

当我保存实体时,它说我需要打开交易...所以我的拦截器是没有被调用。我进行了大量搜索,但不知道我的代码有什么问题。感谢您的帮助。谢谢!

When I save a entity it says that I need to open my transaction...so my interceptor is not being invoked. I did a lot of search and don't know what is the problem with my code. I appreciate some help. Thanks!

推荐答案

好,我必须重新阅读您的问题才能看到问题。

Ok, I had to re-read your question to see the issue.

问题在于,在生产者方法中,您正在实例化DAO。因为使用 new 实例化了它,所以绕过了拦截器绑定。

The problem is that in your producer method, you're instantiating a DAO. Because you instantiate it using new you bypass the interceptor bindings.

这篇关于CDI事务拦截器不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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