无法打开 JPA 实体管理器进行交易;嵌套异常是 javax.persistence.PersistenceException

2022-09-04 22:33:36

当我运行我的代码时,我得到这个异常:

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)

我不知道问题出在哪里。但昨天我工作得很好。

我的数据库.属性:

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

我可以直接从命令行运行mysql。问题出在哪里?是错的。我使用spring 4并使用mysql作为数据库语言进行休眠。

有人有什么想法吗?

谢谢。


答案 1

有通信异常:通信链路故障。

如果您收到 SQLException:连接被拒绝或连接超时或 MySQL 特定

CommunicationsException: Communications link failure

然后这意味着数据库根本无法访问。这可能有以下一个或多个原因:

  • JDBC URL 中的 IP 地址或主机名错误。
  • 本地 DNS 服务器无法识别 JDBC URL 中的主机名。
  • JDBC URL 中的端口号缺失或错误。
  • 数据库服务器已关闭。
  • 数据库服务器不接受 TCP/IP 连接。
  • 数据库服务器的连接已用完。
  • Java和DB之间的某些东西正在阻止连接,例如防火墙或代理。

要解决其中一个问题,请遵循以下建议:

  • 使用 ping 验证和测试它们。
  • 刷新 DNS 或改用 JDBC URL 中的 IP 地址。
  • 根据MySQL DB的my.cnf验证它。
  • 启动数据库。
  • 验证 mysqld 是否在没有 --skip-networking 选项的情况下启动。
  • 重新启动数据库并相应地修复代码,使其最终关闭连接。
  • 禁用防火墙和/或配置防火墙/代理以允许/转发端口。

来源:更多详情


答案 2

数据库已关闭。您必须首先启动 DB 服务器并检查与 telnet 或 mysql 命令行客户端的连接。


推荐