简体   繁体   English

无法打开 JPA EntityManager 进行事务处理; 嵌套异常是 javax.persistence.PersistenceException

[英]Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException

when i run my code, i get this exception:当我运行我的代码时,我得到了这个异常:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1121)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:357)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2482)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2519)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2304)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:834)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:416)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:346)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:208)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:153)
    at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriver(DriverManagerDataSource.java:144)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnectionFromDriver(AbstractDriverBasedDataSource.java:155)
    at org.springframework.jdbc.datasource.AbstractDriverBasedDataSource.getConnection(AbstractDriverBasedDataSource.java:120)
    at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:139)
    at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
    at org.hibernate.tool.hbm2ddl.DatabaseExporter.<init>(DatabaseExporter.java:52)
    at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:367)
    at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:304)
    at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:293)
    at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:517)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Aug 25, 2015 8:23:45 PM com.sun.jersey.spi.container.ContainerResponse mapMappableContainerException
SCHWERWIEGEND: The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Could not open connection
    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:457)
    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.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
    at com.sun.proxy.$Proxy76.create(Unknown Source)

I don't know where is the problem.我不知道问题出在哪里。 But yesterday i worked fine.但是昨天我工作得很好。

my database.properties:我的database.properties:

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/streaming
jdbc.username=root
jdbc.password=password

hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.showsql=true
hibernate.hbm2ddl.auto=create

It is possible for me to run mysql directly from command line.我可以直接从命令行运行 mysql。 Where is the problem?问题出在哪里? Was is wrong.是错的。 I use spring 4 and hibernate with mysql as database language.我使用 spring 4 并使用 mysql 作为数据库语言进行休眠。

Has someone any ideas?有人有什么想法吗?

Thank.感谢。

There is CommunicationsException: Communications link failure.出现 CommunicationsException:通信链路故障。

If you get a SQLException: Connection refused or Connection timed out or a MySQL specific如果您收到 SQLException: Connection denied 或 Connection timed out 或 MySQL 特定的

CommunicationsException: Communications link failure

then it means that the DB isn't reachable at all.那么这意味着数据库根本无法访问。 This can have one or more of the following causes:这可能有以下一种或多种原因:

  • IP address or hostname in JDBC URL is wrong. JDBC URL 中的 IP 地址或主机名错误。
  • Hostname in JDBC URL is not recognized by local DNS server.本地 DNS 服务器无法识别 JDBC URL 中的主机名。
  • Port number is missing or wrong in JDBC URL. JDBC URL 中的端口号缺失或错误。
  • DB server is down.数据库服务器已关闭。
  • DB server doesn't accept TCP/IP connections.数据库服务器不接受 TCP/IP 连接。
  • DB server has run out of connections.数据库服务器已用完连接。
  • Something in between Java and DB is blocking connections, eg a firewall or proxy. Java 和 DB 之间的某些东西正在阻止连接,例如防火墙或代理。

To solve the one or the other, follow the following advices:要解决其中一个问题,请遵循以下建议:

  • Verify and test them with ping.使用 ping 验证和测试它们。
  • Refresh DNS or use IP address in JDBC URL instead.刷新 DNS 或改用 JDBC URL 中的 IP 地址。
  • Verify it based on my.cnf of MySQL DB.根据 MySQL DB 的 my.cnf 进行验证。
  • Start the DB.启动数据库。
  • Verify if mysqld is started without the --skip-networking option.验证 mysqld 是否在没有 --skip-networking 选项的情况下启动。
  • Restart the DB and fix your code accordingly that it closes connections in finally.重新启动数据库并相应地修复您的代码,使其最终关闭连接。
  • Disable firewall and/or configure firewall/proxy to allow/forward the port.禁用防火墙和/或配置防火墙/代理以允许/转发端口。

Source: More Details资料来源: 更多详情

DB is down.数据库已关闭。 You must start the DB server and check connectivity with either telnet or mysql command line client first.您必须先启动 DB 服务器并检查与 telnet 或 mysql 命令行客户端的连接。

I was not unable to figure out the exact cause, but I realized that it was because of some thread pools since I was able to connect to the db using the terminal.我无法找出确切的原因,但我意识到这是因为一些线程池,因为我能够使用终端连接到数据库。 for checking the connectivity you can use the below command.要检查连接,您可以使用以下命令。

psql -h hostname -U username -d database

so restarting the server worked for me.所以重新启动服务器对我有用。

I see that @Amit Tamrakar have good answer but I want to add:我看到@Amit Tamrakar 有很好的答案,但我想补充:

  • if you make a lot of requests to db, (I mean connections), may be that application (some app thread) just wait for use free connection from app db connection pool, but throws timeout exception.如果您向数据库发出大量请求(我的意思是连接),可能是该应用程序(某些应用程序线程)只是等待使用来自应用程序数据库连接池的免费连接,但会引发超时异常。
.h.engine.jdbc.spi.SqlExceptionHelper   : HikariPool-1 - Connection is not available, request timed out after 30005ms.

Could not open JPA EntityManager for transaction; nested exception is org.hibernate.exception.JDBCConnectionException: Unable to acquire JDBC Connection

You can try to increase db connection pool in application config file, for example:您可以尝试在应用程序配置文件中增加 db 连接池,例如:

spring.datasource.hikari.maximum-pool-size=20

You can set size which you need.你可以设置你需要的大小。 This config in app by default is 8 .应用程序中的此配置default8 And we need remember that each db have own free db connection size which we don't need exceed.我们需要记住,每个数据库都有自己的空闲数据库连接大小,我们不需要超过。 I mean if db have 5 free connection and you want to use 10 connections in your app then we will receive so exceptions.我的意思是,如果 db 有 5 个免费连接并且您想在您的应用程序中使用 10 个连接,那么我们将收到这样的异常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有Java SE的JPA:javax.persistence.PersistenceException:EntityManager没有持久性提供程序 - JPA with Java SE: javax.persistence.PersistenceException: No Persistence provider for EntityManager 线程“主”中的异常javax.persistence.PersistenceException:名为jpa-test的EntityManager的持久性提供程序没有 - Exception in thread “main” javax.persistence.PersistenceException: No Persistence provider for EntityManager named jpa-test javax.persistence.PersistenceException:EntityManager 没有持久性提供程序 - javax.persistence.PersistenceException: No Persistence provider for EntityManager org.hibernate.exception.SQLGrammarException: 无法准备语句; 嵌套异常是 javax.persistence.PersistenceException - org.hibernate.exception.SQLGrammarException: could not prepare statement; nested exception is javax.persistence.PersistenceException javax.persistence.PersistenceException:事务未能刷新 - javax.persistence.PersistenceException: Transaction failed to flush Hibernate-JPA。 javax.persistence.PersistenceException - Hibernate-JPA. javax.persistence.PersistenceException 线程“ main”中的异常javax.persistence.PersistenceException:没有名为EntityTest的EntityManager的持久性提供程序 - Exception in thread “main” javax.persistence.PersistenceException: No Persistence provider for EntityManager named databaseTest 异常 javax.persistence.PersistenceException 问题 - exception javax.persistence.PersistenceException problem 线程“ main”中的异常javax.persistence.PersistenceException:名为lanceurApplication的EntityManager的持久性提供程序没有 - Exception in thread “main” javax.persistence.PersistenceException: No Persistence provider for EntityManager named lanceurApplication javax.persistence.PersistenceException:名为aramis的EntityManager的持久性提供程序 - javax.persistence.PersistenceException: No Persistence provider for EntityManager named aramis
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM