开发者

Vaadin, JPA PersistentUnit injection behaves very strange

I'm developing EAR application using EJB, JPA and Vaadin for web interface. The application server - GlassFish 3.1.1.

File DofApplicationServlet.java

package test.servlet;

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.AbstractApplicationServlet;
import test.ejb.TestBean;

import javax.ejb.EJB;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

public class DofApplicationServlet extends AbstractApplicationServlet {
    @EJB
    private TestBean testBean;

    @PersistenceUnit(name="mainPU")
    private EntityManagerFactory emf;

    @Override
    protected Application getNewApplication(HttpServletRequest httpServletRequest) throws ServletException {
        return new DofApplication(testBean, emf);
    }

    @Override
    protected Class<? extends Application> getApplicationClass() throws ClassNotFoundException {
        return DofApplication.class;
    }
}

File DofApplication.java

package test.servlet;

import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label开发者_开发技巧;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;
import test.ejb.TestBean;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;

public class DofApplication extends Application  {
    private TestBean testBean;
    private Label labelResult;
    private Label labelCount;
    private Label labelUser;
    private int counter;
    private EntityManagerFactory emf;

    public DofApplication(TestBean testBean, EntityManagerFactory emf) {
        this.testBean = testBean;
        this.emf = emf;
    }

    @Override
    public void init() {
        Window wnd = new Window();
        final TextField tfLogin = new TextField("login");
        wnd.addComponent(tfLogin);

        final TextField tfPass = new TextField("password");
        wnd.addComponent(tfPass);

        labelResult = new Label("result");
        wnd.addComponent(labelResult);

        labelCount = new Label("Counter");
        wnd.addComponent(labelCount);

        labelUser = new Label("Current user");
        wnd.addComponent(labelUser);

        Button btnLogin = new Button("Login");
        wnd.addComponent(btnLogin);

        Button btnSess = new Button("Session");
        wnd.addComponent(btnSess);


        btnLogin.addListener(new Button.ClickListener() {
            public void buttonClick(Button.ClickEvent clickEvent) {
                boolean auth = testBean.checkOracleConnection(tfLogin.getValue().toString(), tfPass.getValue().toString());
                if (auth) {
                    labelResult.setCaption("auth ok " + tfLogin.getValue().toString());
                    labelUser.setCaption("Current username: " + tfLogin.getValue().toString());
                }  else {
                    labelResult.setCaption("auth fail");
                    labelUser.setCaption("Current username: NONE");
                }

            }
        });

        btnSess.addListener(new Button.ClickListener() {
            public void buttonClick(Button.ClickEvent clickEvent) {
                EntityManager em = emf.createEntityManager(); // <-- emf is not null here in debugger, but call always returns exception
                Query q = em.createQuery("SELECT c FROM CountryEntity c");

            }
        });


        setMainWindow(wnd);
    }


}

And exception I got in btnSess click handler:

[#|2011-09-09T17:42:51.769+0600|SEVERE|glassfish3.1.1|com.vaadin.Application|_ThreadID=20;_ThreadName=Thread-2;|Terminal error:
com.vaadin.event.ListenerMethod$MethodException
Cause: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName mainPU
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:510)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:164)
    at com.vaadin.ui.AbstractComponent.fireEvent(AbstractComponent.java:1193)
    at com.vaadin.ui.Button.fireClick(Button.java:539)
    at com.vaadin.ui.Button.changeVariables(Button.java:206)
    at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariableBurst(AbstractCommunicationManager.java:1299)
    at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.handleVariables(AbstractCommunicationManager.java:1219)
    at com.vaadin.terminal.gwt.server.AbstractCommunicationManager.doHandleUidlRequest(AbstractCommunicationManager.java:735)
    at com.vaadin.terminal.gwt.server.CommunicationManager.handleUidlRequest(CommunicationManager.java:296)
    at com.vaadin.terminal.gwt.server.AbstractApplicationServlet.service(AbstractApplicationServlet.java:501)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1539)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
    at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
    at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
    at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
    at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:330)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:174)
    at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:828)
    at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:725)
    at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1019)
    at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
    at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
    at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
    at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
    at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
    at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
    at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
    at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName mainPU
    at com.sun.enterprise.container.common.impl.EntityManagerFactoryWrapper.getDelegate(EntityManagerFactoryWrapper.java:100)
    at com.sun.enterprise.container.common.impl.EntityManagerFactoryWrapper.createEntityManager(EntityManagerFactoryWrapper.java:110)
    at test.servlet.DofApplication$2.buttonClick(DofApplication.java:86)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:490)
    ... 35 more
|#]

But, in debugger I see the following:

Vaadin, JPA PersistentUnit injection behaves very strange

Ear file structure

ear.ear
-- META-INF
---- application.xml
---- persistence.xml
-- ejb.jar
-- war.war


I think this...

protected Application getNewApplication(HttpServletRequest httpServletRequest) throws ServletException {
    return new DofApplication(testBean, emf);
}

... may cause problems here. The container will inject an entity manager factory which may be different for subsequent requests. So: if you create an instance of DofApplication and pass in that emf, you might call createEntityManager() on an invalid emf.

To test this you could try to get the em like so within init():

EntityManagerFactory emf = Persistence.createEntityManagerFactory("mainPU");
em = emf.createEntityManager();


Put businesslogic into the EJB! Configure the EJB:

• put the persistence.xml there

• programm the entitymanagers etc there and provide some session beans

• enable cdi (web and ejb)

• in the web provide a webservlet (@WebServlet annotation) extending the vaadin servlet class

@WebServlet(urlPatterns = "/*")
public class VaadinAppServlet extends AbstractApplicationServlet {

    @Inject
    VaadinApp application;

    @Override
    protected Class<? extends Application> getApplicationClass() throws ClassNotFoundException {
        return VaadinApp.class;
    }

    @Override
    protected Application getNewApplication(HttpServletRequest request) throws ServletException {
        return application;
    }
}



@SessionScoped
public class VaadinApp extends Application {
    //@Inject //if resource is annotated as @named
    @EJB //if resource is a session bean
}

Example found and often used from here: https://vaadin.com/wiki/-/wiki/Main/Creating+JEE6+Vaadin+Applications

Scroll to "Option 2: Use Context and Dependency Injection (CDI)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜