Could not open JPA EntityManager for transaction; nested exception is javax.pers

错误栈:
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的解决办法,以及整体分析结果:
报错信息: Exception in thread "main" org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is java.lang.UnsupportedOperationException: The application must supply JDBC connections at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:431) at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:373) at org.springframework.transaction.interceptor.TransactionAspectSupport.createTransactionIfNecessary(TransactionAspectSupport.java:427) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:276) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:654) at com.yao.service.PersonService$$EnhancerBySpringCGLIB$$324a56ab.getAddressCity(<generated>) at com.yao.test.MainTest.main(MainTest.java:21) Caused by: java.lang.UnsupportedOperationException: The application must supply JDBC connections at org.hibernate.engine.jdbc.connections.internal.UserSuppliedConnectionProviderImpl.getConnection(UserSuppliedConnectionProviderImpl.java:61) at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:380) at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:228) at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:171) at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67) at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:162) at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1471) at org.hibernate.jpa.internal.TransactionImpl.begin(TransactionImpl.java:61) at org.springframework.orm.jpa.vendor.HibernateJpaDialect.beginTransaction(HibernateJpaDialect.java:170) at org.springframework.orm.jpa.JpaTransactionManager.doBegin(JpaTransactionManager.java:380) ... 8 more 我的配置文件: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:repository="http://www.springframework.org/schema/data/repository" xsi:schemaLocation="http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository-1.6.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!--配置扫描包 --> <context:component-scan base-package="com.yao"/> <context:property-placeholder location="classpath:db.properties"/> <!--配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="minPoolSize" value="${jdbc.minPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean> <!--配置实体管理工厂类(容器管理类) --> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <!--实体包扫描 --> <property name="packagesToScan" value="com.yao.model"></property> <!--JPA提供商 --> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"></bean> </property> <!--配置JPA实现者Hibernate的一些基本属性 --> <property name="jpaProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.format_sql">true</prop> <!-- *配置自动生成数据表列名的策略. 例如: lastName - last_name --> <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop> </props> </property> </bean> <!-- 定义 Spring JPA 事务管理程序 --> <bean id="jpaTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <!-- 配置我们自己的repository并添加依赖 --> <jpa:repositories base-package="com.yao.repository" entity-manager-factory-ref="entityManagerFactory" transaction-manager-ref="jpaTransactionManager"/> <!--声明注解驱动 --> <tx:annotation-driven transaction-manager="jpaTransactionManager"/> </beans>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页