Could not open JPA EntityManager for transaction; nested exception is javax.pers
java jpa exception    2017-07-19 22:15:22    266   
lightingfire   java jpa exception
错误栈:
org.zkoss.zk.ui.UiException: Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: org.hibernate.TransactionException: JDBC begin transaction failed:  at [file:/C:/Users/lyg21_000/git/Saas/target/classes/main.zul, line:26]
 at org.zkoss.bind.impl.MiscUtil.mergeExceptionInfo(MiscUtil.java:175) ~[zkbind-7.0.4.jar:7.0.4]
 at org.zkoss.bind.BindComposer.doBeforeComposeChildren(BindComposer.java:161) ~[zkbind-7.0.4.jar:7.0.4]
 at org.zkoss.zk.ui.impl.UiEngineImpl.doBeforeComposeChildren(UiEngineImpl.java:930) ~[zk-7.0.4.jar:7.0.4]
 at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild0(UiEngineImpl.java:869) ~[zk-7.0.4.jar:7.0.4]
 at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:826) ~[zk-7.0.4.jar:7.0.4]
 at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:735) ~[zk-7.0.4.jar:7.0.4]
 at org.zkoss.zk.ui.impl.UiEngineImpl.execCreateChild(UiEngineImpl.java:797) ~[zk-7.0.4.jar:7.0.4]
 at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate0(UiEngineImpl.java:757) ~[zk-7.0.4.jar:7.0.4]
 at org.zkoss.zk.ui.impl.UiEngineImpl.execCreate(UiEngineImpl.java:699) ~[zk-7.0.4.jar:7.0.4]
 at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage0(UiEngineImpl.java:442) ~[zk-7.0.4.jar:7.0.4]
 at org.zkoss.zk.ui.impl.UiEngineImpl.execNewPage(UiEngineImpl.java:356) ~[zk-7.0.4.jar:7.0.4]
 at org.zkoss.zk.ui.http.DHtmlLayoutServlet.process(DHtmlLayoutServlet.java:217) ~[zk-7.0.4.jar:7.0.4]
 at org.zkoss.zk.ui.http.DHtmlLayoutServlet.doGet(DHtmlLayoutServlet.java:136) ~[zk-7.0.4.jar:7.0.4]
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) ~[servlet-api-2.5.jar:2.5]
 at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) ~[servlet-api-2.5.jar:2.5]
 at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511) ~[jetty-6.1.26.jar:6.1.26]
 at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1221) ~[jetty-6.1.26.jar:6.1.26]
 at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:119) ~[spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
 at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) ~[spring-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
 at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212) ~[jetty-6.1.26.jar:6.1.26]
 at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449) ~[shiro-web-1.2.1.jar:1.2.1]
 at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365) ~[shiro-web-1.2.1.jar:1.2.1]
 at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90) ~[shiro-core-1.2.1.jar:1.2.1]
 at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83) ~[shiro-core-1.2.1.jar:1.2.1]
 at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383) ~[shiro-core-1.2.1.jar:1.2.1]
 at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362) ~[shiro-web-1.2.1.jar:1.2.1]
 at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125) ~[shiro-web-1.2.1.jar:1.2.1]
 at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) ~[spring-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
 at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) ~[spring-web-3.1.2.RELEASE.jar:3.1.2.RELEASE]
 at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1212) ~[jetty-6.1.26.jar:6.1.26]
 at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:399) ~[jetty-6.1.26.jar:6.1.26]
 at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) [jetty-6.1.26.jar:6.1.26] 
 at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) [jetty-6.1.26.jar:6.1.26]
 at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:766) [jetty-6.1.26.jar:6.1.26] 
 at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:450) [jetty-6.1.26.jar:6.1.26]
 at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230) [jetty-6.1.26.jar:6.1.26]
 at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) [jetty-6.1.26.jar:6.1.26]
 at org.mortbay.jetty.Server.handle(Server.java:326) [jetty-6.1.26.jar:6.1.26]
 at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) [jetty-6.1.26.jar:6.1.26]
 at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:928) [jetty-6.1.26.jar:6.1.26]
 at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:549) [jetty-6.1.26.jar:6.1.26]
 at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212) [jetty-6.1.26.jar:6.1.26]
 at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) [jetty-6.1.26.jar:6.1.26]
 at org.mortbay.jetty.bio.SocketConnector$Connection.run(SocketConnector.java:228) [jetty-6.1.26.jar:6.1.26]
 at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582) [jetty-util-6.1.26.jar:6.1.26]
Caused by: org.zkoss.zk.ui.UiException: Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: org.hibernate.TransactionException: JDBC begin transaction failed:
 at org.zkoss.bind.impl.AbstractAnnotatedMethodInvoker.invokeMethod(AbstractAnnotatedMethodInvoker.java:95) ~[zkbind-7.0.4.jar:7.0.4]
 at org.zkoss.bind.impl.BinderImpl.init(BinderImpl.java:342) ~[zkbind-7.0.4.jar:7.0.4]
 at org.zkoss.bind.AnnotateBinder.init(AnnotateBinder.java:57) ~[zkbind-7.0.4.jar:7.0.4]
 at org.zkoss.bind.BindComposer.doBeforeComposeChildren(BindComposer.java:159) ~[zkbind-7.0.4.jar:7.0.4]
 ... 43 common frames omitted
Caused by: org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: org.hibernate.TransactionException: JDBC begin transaction failed:
 at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:427) ~[spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
 at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:371) ~[spring-tx-3.1.2.RELEASE.jar:3.1.2.RELEASE]
 at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:336) ~[spring-tx-3.1.2.RELEASE.jar:3.1.2.RELEASE]
 at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:105) ~[spring-tx-3.1.2.RELEASE.jar:3.1.2.RELEASE]
 at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) ~[spring-aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]
 at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:622) ~[spring-aop-3.1.2.RELEASE.jar:3.1.2.RELEASE]
 at com.conf.object.dao.ModuleDAO$$EnhancerByCGLIB$$7099a01c.findById(<generated>) ~[cglib-2.2.jar:na]
 at com.prototype.login.ModuleViewModel.getModules(ModuleViewModel.java:579) ~[classes/:na]
 at com.prototype.login.ModuleViewModel.init(ModuleViewModel.java:188) ~[classes/:na]
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_67]
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_67]
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_67]
 at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_67]
 at org.zkoss.bind.impl.ParamCall.call(ParamCall.java:122) ~[zkbind-7.0.4.jar:7.0.4]
 at org.zkoss.bind.impl.AbstractAnnotatedMethodInvoker.invokeMethod(AbstractAnnotatedMethodInvoker.java:90) ~[zkbind-7.0.4.jar:7.0.4]
 ... 46 common frames omitted
Caused by: javax.persistence.PersistenceException: org.hibernate.TransactionException: JDBC begin transaction failed:
 at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1387) ~[hibernate-entitymanager-4.2.9.Final.jar:4.2.9.Final]
 at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1310) ~[hibernate-entitymanager-4.2.9.Final.jar:4.2.9.Final]
 at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:1397) ~[hibernate-entitymanager-4.2.9.Final.jar:4.2.9.Final]
 at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:62) ~[hibernate-entitymanager-4.2.9.Final.jar:4.2.9.Final]
 at org.springframework.orm.jpa.DefaultJpaDialect.beginTransaction(DefaultJpaDialect.java:70) ~[spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
 at org.springframework.orm.jpa.vendor.HibernateJpaDialect.beginTransaction(HibernateJpaDialect.java:59) ~[spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
 at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:377) ~[spring-orm-3.1.2.RELEASE.jar:3.1.2.RELEASE]
 ... 60 common frames omitted
Caused by: org.hibernate.TransactionException: JDBC begin transaction failed:
 at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:76) ~[hibernate-core-4.2.9.Final.jar:4.2.9.Final]
 at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160) ~[hibernate-core-4.2.9.Final.jar:4.2.9.Final]
 at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1351) ~[hibernate-core-4.2.9.Final.jar:4.2.9.Final]
 at org.hibernate.ejb.TransactionImpl.begin(TransactionImpl.java:59) ~[hibernate-entitymanager-4.2.9.Final.jar:4.2.9.Final]
 ... 63 common frames omitted
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet successfully received from the server was 150,279 milliseconds ago.  The last packet sent successfully to the server was 8 milliseconds ago.
 at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.7.0_67]
 at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57) ~[na:1.7.0_67]
 at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.7.0_67]
 at java.lang.reflect.Constructor.newInstance(Constructor.java:526) ~[na:1.7.0_67]
 at com.mysql.jdbc.Util.handleNewInstance(Util.java:406) ~[mysql-connector-java-5.1.9.jar:na]
 at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074) ~[mysql-connector-java-5.1.9.jar:na]
 at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3052) ~[mysql-connector-java-5.1.9.jar:na]
 at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2938) ~[mysql-connector-java-5.1.9.jar:na]
 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3481) ~[mysql-connector-java-5.1.9.jar:na]
 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959) ~[mysql-connector-java-5.1.9.jar:na]
 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109) ~[mysql-connector-java-5.1.9.jar:na]
 at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2637) ~[mysql-connector-java-5.1.9.jar:na]
 at com.mysql.jdbc.ConnectionImpl.setAutoCommit(ConnectionImpl.java:5029) ~[mysql-connector-java-5.1.9.jar:na]
 at org.apache.commons.dbcp.DelegatingConnection.setAutoCommit(DelegatingConnection.java:371) ~[commons-dbcp-1.4.jar:1.4]
 at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.setAutoCommit(PoolingDataSource.java:328) ~[commons-dbcp-1.4.jar:1.4]
 at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:72) ~[hibernate-core-4.2.9.Final.jar:4.2.9.Final]
 ... 66 common frames omitted
Caused by: java.io.EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost.
 at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2497) ~[mysql-connector-java-5.1.9.jar:na]
 at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2949) ~[mysql-connector-java-5.1.9.jar:na]
 ... 75 common frames omitted

原因是mysql的设置的interactive_timeout和wait_timeout 两个值为8小时(28800s),所以此问题多数出现在隔夜再访问报错的情景,暴力的方式是修改msyql的配置文件,将这两个值设置为-1(-1表示永不超时)或者很大,但是此方法从开发的角度讲是不合理的,不能指望客户每套环境都要去改mysql的配置


解决办法:dbcp的datasource添加如下两个属性即可
<property name="testOnBorrow" value="true"/>  
<property name="validationQuery" value="select 1"/>

关于c3p0的解决办法,以及整体分析结果:
http://blog.csdn.net/wangfayinn/article/details/24623575



 

by 刘迎光@萤火虫工作室 
OpenBI交流群:495266201 
MicroService 微服务交流群:217722918 
mail: liuyg#liuyingguang.cn 
博主首页(==防止爬虫==):http://blog.liuyingguang.cn

Pre: RedHat 6.5离线安装GCC方法

Next: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test)


Table of content