Vaadin,JPA PersistentUnit 注入行为非常奇怪
我正在使用 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
|#]
但是,在调试器中我看到以下内容:
耳档结构
ear.ear
-- META-INF
---- application.xml
---- persistence.xml
-- ejb.jar
-- war.war
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:
Ear file structure
ear.ear
-- META-INF
---- application.xml
---- persistence.xml
-- ejb.jar
-- war.war
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这……
可能会在这里引起问题。容器将注入一个实体管理器工厂,该工厂对于后续请求可能会有所不同。因此:如果您创建 DofApplication 的实例并传入该 emf,您可能会对无效的 emf 调用 createEntityManager()。
要测试这一点,您可以尝试在 init() 中像这样获取 em:
I think this...
... 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():
将业务逻辑放入 EJB 中!
配置 EJB:
• 将 persistence.xml 放在那里
• 对实体管理器等进行编程并提供一些会话 bean
• 启用 cdi(web 和 ejb)
• 在 web 中提供扩展 vaadin servlet 类的 webservlet(@WebServlet 注释)
示例找到和经常从这里使用:
https://vaadin.com/wiki/-/wiki/Main /Creating+JEE6+Vaadin+Applications
滚动到“选项 2:使用上下文和依赖注入 (CDI)”
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
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)