添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

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:

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. Where is the problem? Was is wrong. I use spring 4 and hibernate with mysql as database language.

Has someone any ideas?

Thank.

There is CommunicationsException: Communications link failure.

If you get a SQLException: Connection refused or Connection timed out or a MySQL specific

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.
  • Hostname in JDBC URL is not recognized by local DNS server.
  • Port number is missing or wrong in JDBC URL.
  • DB server is down.
  • DB server doesn't accept TCP/IP connections.
  • DB server has run out of connections.
  • Something in between Java and DB is blocking connections, e.g. a firewall or proxy.
  • To solve the one or the other, follow the following advices:

  • Verify and test them with ping.
  • Refresh DNS or use IP address in JDBC URL instead.
  • Verify it based on my.cnf of MySQL DB.
  • Start the DB.
  • Verify if mysqld is started without the --skip-networking option.
  • 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

    I am running a Spring Boot Microservice with MariaDB database, where I am facing the same issue. None of the above reasons hold true in my case. The service is able to connect to the db for a few hours and then I start seeing this connection error. When I restart the service, the issue is resolved again. It appears that the connection pool runs out of valid connections. I have played around with spring.datasource.tomcat.* properties (testOnBorrow:true, validateQuery: SELECT 1, testWhileIdle: true, timeBetweenEvictionRunsMillis: 1000, logAbandoned: true), but that has not helped. – Piyush Oct 18, 2017 at 3:52 i can run succefully mysql -u root -p on the console. DB down. But when i run with sudo /usr/local/mysql/support-files/mysql.server start Starting MySQL .................................................................................................... i get this: ERROR! The server quit without updating PID file (/usr/local/mysql/data/Me-MBP.local.pid). – emoleumassi Aug 25, 2015 at 18:55 can't think of anything but ps aux | grep mysqld then if exists, sudo kill -9 pid, check firewall, restart. – ACV Aug 25, 2015 at 19:10 Thank but i don't time enough time. I decided to reinstall mysql and it is working fine now. – emoleumassi Aug 25, 2015 at 19:22

    I see that @Amit Tamrakar have good answer but I want to add:

  • 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:

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

    You can set size which you need. This config in app by default is 8. 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.

    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.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.