我正在开发一个运行在JaveEE6 8.5.5上的WebSphere web应用程序。为了访问数据库,我使用CDI将@无状态EJB注入我的后台bean中。查询数据库可以正常工作,但是当试图持久化实体时,会引发以下异常:
javax.persistence.TransactionRequiredException: No active transaction for PuId=...应用程序打包在包含四个模块(耳设置)的EAR中:
事务在web服务模块中运行良好,或者在将数据库类和web应用程序组合成单个模块时工作良好(不过,我真的很想避免这种情况)。
我试着把一些类似问题的答案中的一些解决方案应用到类似的问题上,但到目前为止,解决这个问题还没有成功。任何帮助都是非常感谢的。
谢谢,
卡尔
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="hcms" transaction-type="JTA">
<jta-data-source>jdbc/hcms</jta-data-source>
</persistence-unit>
</persistence>示例代码片段:
@Stateless
public class CommunityAddressDao extends Dao<CommunityAddressEntity, Integer> {
@PersistenceContext(unitName="hcms")
private EntityManager em;
public CommunityAddressDao() {
super(CommunityAddressEntity.class);
}
@Override
protected EntityManager getEntityManager() {
return em;
}
...
}来自web服务的工作示例:
@Stateless
@Path("/addresses")
@Produces(MediaType.APPLICATION_JSON)
@RolesAllowed({"user"})
public class CommunityAddressService {
@Inject
private CommunityAddressDao addressDao;
...
@POST
@Consumes(MediaType.APPLICATION_JSON)
public CommunityAddressDto registerAddress(CommunityAddressDto address) throws DuplicateAddressException, ManagedAddressException, BadRequestException {
// Validation
CommunityAddressEntity entity = new CommunityAddressEntity();
entity.setCommunityUuid(address.getCommunityUuid());
...
addressDao.persist(entity);
return new CommunityAddressDto(entity);
}
}来自web应用程序的问题示例:
@ViewScoped
@Named("addressBean")
public class CommunityAddressBean implements Serializable {
private static final long serialVersionUID = -1685666287294618708L;
@Inject
private CommunityAddressDao addressDao;
private List<CommunityAddressEntity> addresses;
...
@PostConstruct
private void init() {
// Retrieve first 10 addresses - Works fine
addresses = addressDao.findAll(10, 0);
}
public void createAddress(ActionEvent event) {
CommunityAddressEntity entity = new CommunityAddressEntity();
entity.setCommunityUuid("some UUID");
...
// Throws javax.persistence.TransactionRequiredException: No active transaction for PuId=...
addressDao.persist(entity);
}
}更新(2016-11-02)
用于与数据库交互的所有EJB都继承了一个提供基本CRUD功能的抽象类。
抽象DAO基类:
public abstract class Dao<E, K> {
protected Class<E> entityClass;
public Dao(Class<E> type) {
entityClass = type;
}
protected abstract EntityManager getEntityManager();
protected abstract String[] getFilterAttributes();
...
public void persist(E entity) {
getEntityManager().persist(entity);
}
public void merge(E entity) {
getEntityManager().merge(entity);
}
public void remove(E entity) {
getEntityManager().remove(entity);
}
public E findById(K id) {
return getEntityManager().find(entityClass, id);
}
}全堆栈跟踪:
[err] javax.persistence.TransactionRequiredException: No active transaction for PuId=hcms#de.holistic.hcms.admin.war#hcms
[err] at com.ibm.ws.jpa.management.JPATxEntityManager.getEMInvocationInfo(JPATxEntityManager.java:230)
[err] at [internal classes]
[err] at de.holistic.hcms.data.Dao.persist(Dao.java:84)
[err] at de.holistic.hcms.admin.beans.CommunityAddressBean.createAddress(CommunityAddressBean.java:38)
[err] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[err] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:88)
[err] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:55)
[err] at java.lang.reflect.Method.invoke(Method.java:618)
[err] at org.apache.el.parser.AstValue.invoke(AstValue.java:268)
[err] at [internal classes]
[err] at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:78)
[err] at javax.faces.event.ActionEvent.processListener(ActionEvent.java:51)
[err] at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:404)
[err] at javax.faces.component.UICommand.broadcast(UICommand.java:103)
[err] at javax.faces.component.UIData.broadcast(UIData.java:772)
[err] at javax.faces.component.UIViewRoot._broadcastAll(UIViewRoot.java:993)
[err] at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:276)
[err] at javax.faces.component.UIViewRoot._process(UIViewRoot.java:1305)
[err] at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:731)
[err] at org.apache.myfaces.lifecycle.InvokeApplicationExecutor.execute(InvokeApplicationExecutor.java:34)
[err] at [internal classes]
[err] at javax.faces.webapp.FacesServlet.service(FacesServlet.java:189)
[err] at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1287)
[err] at [internal classes]
[err] at org.ocpsoft.rewrite.servlet.RewriteFilter.doFilter(RewriteFilter.java:226)
[err] at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:207)
[err] at [internal classes]
[err] at org.ocpsoft.rewrite.servlet.impl.HttpRewriteResultHandler.handleResult(HttpRewriteResultHandler.java:42)
[err] at org.ocpsoft.rewrite.servlet.RewriteFilter.rewrite(RewriteFilter.java:297)
[err] at org.ocpsoft.rewrite.servlet.RewriteFilter.doFilter(RewriteFilter.java:198)
[err] at com.ibm.ws.webcontainer.filter.FilterInstanceWrapper.doFilter(FilterInstanceWrapper.java:207)
[err] at [internal classes]
[err] at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1156)
[err] at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:626)
[err] at java.lang.Thread.run(Thread.java:804)更新2 (2016-11-02)
在com.ibm.ejs.container.EJBNotFoundException:中使用@EJB而不是@Inject的结果
CWNEN0030E: The server was unable to obtain an object instance for the java:comp/env/de.holistic.hcms.admin.beans.CommunityAddressBean/addressDao reference. The exception message was: The EJB reference in the de.holistic.hcms.admin.war module of the hcms application could not be resolved; nested exception is: com.ibm.ejs.container.EJBNotFoundException: EJB with interface de.holistic.hcms.data.CommunityAddressDao not present in application hcms.
[ERROR ] An error occurred while executing [@PostConstruct.]
java.lang.NullPointerException
[ERROR ] Error Rendering View[/addresses.xhtml]
java.lang.NullPointerException
[ERROR ] An exception occurred
java.lang.NullPointerException因此,服务器正在错误的模块中查找EJB。我尝试使用@EJB分析的属性(查找、名称等)来正确地指定EJB,但到目前为止没有成功。我会对此做一些阅读,因为我不太熟悉这个话题。
尽管如此,我还是想知道为什么CDI + transcations在web服务中工作,而不是在web应用程序中工作。*-\
https://stackoverflow.com/questions/40302850
复制相似问题