1

我浪费了很多时间尝试解决这个问题,但我在同一个地方。我怀疑我将 CDI 与 EJB 混合在一起。

问题仍然存在,仅删除不起作用。

Caused by: javax.persistence.TransactionRequiredException: WFLYJPA0060: Transaction is required to perform this operation (either use a transaction or extended persistence context)
    at org.jboss.as.jpa.container.AbstractEntityManager.transactionIsRequired(AbstractEntityManager.java:866)
    at org.jboss.as.jpa.container.AbstractEntityManager.persist(AbstractEntityManager.java:579)
    at com.oki.scope.console.model.dao.GenericDAO.save(GenericDAO.java:29)
    at com.oki.scope.console.model.dao.GenericConsoleDAO.save(GenericConsoleDAO.java:12)
    at com.oki.scope.console.service.ServidorServiceImp.salvar(ServidorServiceImp.java:27)
    at com.oki.scope.console.service.ServidorServiceImp$Proxy$_$$_WeldClientProxy.salvar(Unknown Source)
    at com.oki.scope.console.managedBean.consulta.ServidorMB.salvar(ServidorMB.java:65)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:292)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
    ... 40 more

我的 DAO

public class GenericDAO<T, K> {

    protected EntityManager em;
    private Class<T> entityClass;

    public GenericDAO(Class<T> entityClass,  EntityManager em) {
        this.entityClass = entityClass;
        this.em = em;
    }

    @Transactional
    protected void save(T entity) {
        em.persist(entity);
    }

通用 DAO:

import javax.persistence.EntityManager;

public abstract class GenericConsoleDAO<T, K> extends GenericDAO<T, K> {

     public GenericConsoleDAO(Class<T> entityClass, EntityManager em) {
        super(entityClass, em);
    }

    public void save(T t){
        super.save(t);
    }

}

道厂:

package com.oki.scope.console.model.dao;

import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import javax.ejb.Singleton;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Singleton
@TransactionManagement(TransactionManagementType.CONTAINER)
public class DAOConsoleFactory {
    private final static String UNIT_NAME = "scope-console";

    private static Map<String, Object> mapa = Collections.synchronizedMap(new HashMap<String, Object>());

    @PersistenceContext(unitName = UNIT_NAME )  
    private EntityManager entityManager;

    @Produces public ServidorDAO            criaServidorDAO(){          return getDAO(ServidorDAO.class); }
    @Produces public ConexaobdDAO           criaConexaoDAO(){           return getDAO(ConexaobdDAO.class);      }
    @Produces public ContratoDAO            criaContratoDAO(){          return getDAO(ContratoDAO.class);   }
    @Produces public EmpresaDAO             criaEmpresaDAO(){           return getDAO(EmpresaDAO.class); }
    @Produces public LojaDAO                criaLojaDAO(){              return getDAO(LojaDAO.class);   }
    //@Produces public RedeAutorizadoraDAO  criaRedeAutorizadoraDAO(){  return getDAO(RedeAutorizadoraDAO.class); }
    @Produces public RedeDAO                criaRedeDAO(){              return getDAO(RedeDAO.class);   }
    @Produces public RoteadorDAO            criaRoteadorDAO(){          return getDAO(RoteadorDAO.class);   }
    @Produces public TerminalDAO            criaTerminalDAO(){          return getDAO(TerminalDAO.class);   }
    @Produces public TipoHeaderDAO          criaTipoHeaderDAO(){        return getDAO(TipoHeaderDAO.class);     }

    @SuppressWarnings("unchecked")
    public <E> E getDAO(Class<E> classe){
        String key = classe.getSimpleName();
        if (!mapa.containsKey(key))
        {
            try {
                mapa.put(key, classe.getDeclaredConstructor(EntityManager.class).newInstance(entityManager));
            } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
                    | InvocationTargetException | NoSuchMethodException | SecurityException e) {
                System.out.println("Classe "+ key +" não possui um construtor que tenha EntityManager como parametro.");
            }
        }
        return (E)mapa.get(key);

    }
}

我的课:

@Named
@ApplicationScoped
public class ServidorServiceImp implements ServidorService {

    @Inject private ServidorDAO dao;


    @Override
    public List<Servidor> getLista() {
        return dao.getLista();
    }

    @Override
    public void salvar(Servidor servidor) {
        if (servidor.getId()==0){
            dao.save(servidor);
        }
        else
        {
            dao.update(servidor);
        }
    }

    @Override
    public void remover(Servidor servidor) {
        dao.delete(servidor);

    }

}
4

2 回答 2

5

在尝试提高性能时,您已经绕过了容器应该为您做的事情,即在事务中实例化一个 bean。

我会说删除and@Singleton并允许 EJB 事务由使用 DAO 的 EJB bean 处理。@TransactionManagement(TransactionManagementType.CONTAINER)DAOConsoleFactory

更新:此外,@ApplicationScoped不是 EJB 注释类ServidorServiceImp需要是 EJB bean,因此,它应该使用@Stateless或可能进行注释@Statefull并删除@ApplicationScoped. 它读起来就像一个无状态的 EJB bean,所以没有必要使它成为应用程序范围。

同样,在我看来,您似乎过于专注于尝试优化性能而没有很好地理解 EJB 应该如何在容器中工作。我建议让一切正常工作并遵循架构最佳实践,尤其是在“会话外观”概念中。其中一些帖子可能会有所帮助:Java EE 中的外观有什么意义?为什么对 EJB 会话 bean 使用 Facade 模式

于 2016-04-18T13:03:52.280 回答
2

解决:前2天受苦。问题出在我的课堂上。@Named @ApplicationScoped 在哪里,我改成@Statefull。

于 2016-04-18T13:53:02.457 回答