问题描述
我失去了很多时间尝试解决这个发行人,但我在同一个地方。我怀疑我将一些CDI与EJB混合在一起。问题是持久性,删除只能不起作用。
导致:javax.persistence.TransactionRequiredException:WFLYJPA0060:执行此操作需要执行事务(使用事务或扩展持久性上下文)
在org.jboss.as.jpa.container.AbstractEntityManager.transactionIsRequired(AbstractEntityManager.java:866)
在org.jboss.as.jpa.container.AbstractEntityManager.persist(AbstractEntityManager.java: com.oki.scope.console.model.dao.GenericDAO.save(GenericDAO.java:29)
$ com $。 GenericConsoleDAO.java:12)
在com.oki.scope.console.service.ServidorServiceImp.salvar(ServidorServiceImp.java:27)
在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.Native MethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
在java.lang.reflect.Method.invoke(Method.java:497)
在com.sun.el.parser.AstValue.invoke(AstValue.java:292)
在com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:304)
在org .jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
在org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
在org.jboss .weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:40)
在org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
在com.sun.faces .facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
在javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:87)
... 40更多
我的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>扩展GenericDAO< T,K> {
public GenericConsoleDAO(Class< T> entityClass,EntityManager em){
super(entityClass,em);
}
public void save(T t){
super.save(t);
}
}
DAO工厂:
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); }
// @生成public RedeAutorizadoraDAO criaRedeAutorizadoraDAO(){return getDAO(RedeAutorizadoraDAO.class); }
@Produces public RedeDAO criaRedeDAO(){return getDAO(RedeDAO.class); }
@Produces public RoteadorDAO criaRoteadorDAO(){return getDAO(RoteadorDAO.class); }
@生成公共终端DAO 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ãopossui um construtor que tenha EntityManager como parametro);
}
}
return(E)mapa.get(key);
}
}
我的课程:
@Named
@ApplicationScoped
public class ServidorServiceImp实现ServidorService {
@Inject private ServidorDAO道;
@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);
}
}
为了提高性能,您可以规避容器应该为您做什么,这是在事务中实例化一个bean。
我会说删除 @Singleton
和 @TransactionManagement(TransactionManagementType.CONTAINER)
从 DAOConsoleFactory
,并允许EJB事务由使用DAO的EJB bean进行处理。
更新:另外, @ApplicationScoped
不是EJB注释类 ServidorServiceImp
需要是一个EJB bean,它应该用 @Stateless
或可能 @Statefull
注释,并删除 @ApplicationScoped
。它看起来像一个无状态的EJB bean,所以没有必要使它的应用程序范围。
再次,在我看来,你正在专注于尝试优化性能而不了解EJB应该如何在容器中工作。我建议让一切工作,遵循建筑最佳做法,特别是在会议外墙概念中。其中一些帖子可能有所帮助:或。
I losted many time try solve this issuer, but I am in the same place. I suspect that I mixed something of CDI with EJB.
The problem is persist and delete only don't work.
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
My 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);
}
Generic 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);
}
}
DAO Factory:
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);
}
}
My class:
@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);
}
}
In trying to enhance performance, you have circumvented what the container is supposed to be doing for you, which is instantiating a bean inside a transaction.
I would say remove the @Singleton
and @TransactionManagement(TransactionManagementType.CONTAINER)
from DAOConsoleFactory
and allow the EJB transaction to be handled by the EJB bean that's using the DAO's.
UPDATE: Also, @ApplicationScoped
is not an EJB annotation Class ServidorServiceImp
needs to be an EJB bean so, it should be annotated with @Stateless
or perhaps @Statefull
and remove the @ApplicationScoped
. It reads like a stateless EJB bean, so there is no need to make it application scoped.
Again, it seems to me you are concentrating too much on trying to optimize performance without having a good understanding of how EJB's are supposed to work in a container. I would recommend getting everything to work and follow architectural best practices, especially in the "Session Façade" concept. Some of these posts may help: What is the point of a Facade in Java EE? or Why use Facade pattern for EJB session bean.
这篇关于WFLYJPA0060:执行此操作需要执行(使用事务或扩展持久性上下文)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!