Vaadin,JPA PersistentUnit注入表现得非常奇怪

时间:2011-09-09 12:07:33

标签: java vaadin ear

我正在使用EJB,JPA和Vaadin为Web界面开发EAR应用程序。应用程序服务器 - GlassFish 3.1.1。

文件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;
    }
}

文件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);
    }


}

我在btnSess点击处理程序中遇到了异常:

[#|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
|#]

但是,在调试器中我看到以下内容:

debugger

耳朵文件结构

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

2 个答案:

答案 0 :(得分:2)

我想这......

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

......可能会在这里引起问题。容器将注入实体管理器工厂,后者的后续请求可能不同。所以:如果你创建一个DofApplication实例并传入那个emf,你可以在一个无效的emf上调用createEntityManager()。

要测试这个,你可以尝试在init()中获取em:

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

答案 1 :(得分:1)

将businesslogic放入EJB中! 配置EJB:

•将persistence.xml放在那里

•在那里对实体管理员等进行编程并提供一些会话bean

•启用cdi(web和ejb)

•在Web中提供扩展vaadin servlet类的webservlet(@WebServlet注释)

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

从这里找到并经常使用的示例: https://vaadin.com/wiki/-/wiki/Main/Creating+JEE6+Vaadin+Applications

滚动到“选项2:使用上下文和依赖关系注入(CDI)