WebSphere 8.5.5:PuId

时间:2016-10-28 10:10:28

标签: jpa java-ee-6 ear jta websphere-8

我正在研究在WebSphere 8.5.5上运行的JaveEE6 Web应用程序。 为了访问数据库,我使用CDI将@Stateless EJB注入我的支持bean。查询数据库工作正常,但在尝试持久化实体时会抛出以下异常:

javax.persistence.TransactionRequiredException: No active transaction for PuId=...

应用程序打包在一个包含四个模块(EAR setup):

的EAR中
  • 包含JPA实体和EJB的实用程序模块,用于与数据库交互
  • 包含Primefaces 5.2的实用程序模块
  • 使用数据库实用程序模块的JAX-RS Web服务
  • 使用数据库实用程序模块的JSF Web应用程序

事务在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>

EJB的示例代码:

@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;
    }

    ...

}

来自网络服务的工作示例:

@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);
    }
}

来自网络应用程序的问题示例:

@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)

使用@EJB代替@Inject会导致 com.ibm.ejs.container.EJBNotFoundException:

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 annotiation的属性(查找,名称等)来正确指定我的EJB,但到目前为止还没有运气。我会对此进行一些阅读,因为我对这个主题不是很熟悉。

尽管如此,我想知道为什么CDI +转换在Web服务中起作用而在Web应用程序中起作用。 : - \

1 个答案:

答案 0 :(得分:0)

正如@Gas在评论中提到的那样,结果是类路径/ EAR的设置存在问题。

我能够通过以下方式使用@EJB@Inject

  1. 将DB实用程序模块转换为EJB模块(Project Facets
  2. 在EAR的application.xml文件(EAR setup)中引用EJB模块

    <强> application.xml中:

    <?xml version="1.0" encoding="UTF-8"?>
    <application 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/application_6.xsd" version="6">
        <application-name>hcms</application-name>
        <module id="Module_1473757101927">
            <web>
                <web-uri>de.holistic.hcms.web.service.war</web-uri>
                <context-root>hcms</context-root>
            </web>
        </module>
        <module id="Module_1476173932514">
            <web>
                <web-uri>de.holistic.hcms.admin.war</web-uri>
                <context-root>hcms/admin</context-root>
            </web>
        </module>
        <module id="Module_1478169092726">
            <ejb>de.holistic.hcms.data.jar</ejb>
        </module>
    
    </application>
    
  3. 引用我的WAR清单中的模块

    <强> MANIFEST.MF:

    Manifest-Version: 1.0
    Class-Path: de.holistic.hcms.data.jar