首页 文章

MySQL重启后,Hibernate应用程序无法连接到MySQL

提问于
浏览
1

我有一个在tomcat上使用hibernate和rest运行在amazon ec2上的webapp,我的mySQL是通过amazon rds的独立实例 .

一旦我启动我的webapp - 一切正常,但最近我在我的数据库上配置了每日备份,然后开始看到我的webapp连接到mySQL的问题 .

基本上只有在重新启动(备份)mysql实例之前我的webapp启动时才会出现问题 . 然后在mySQL重新启动后由于某种原因从我的webapp连接到它失败了 .

一旦我重新启动我的ec2虚拟机,这一切都解决了(如果我重新启动tomcat也可以解决,但我还没试过)

如何在mysql重启后确保我的webapp连接回mysql?

这就是我写入日志的内容:

21-May-2015 11:42:27.857 WARN [http-nio-8080-exec-2] org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions SQL Error: 0, SQLState: 08S01
21-May-2015 11:42:27.857 ERROR [http-nio-8080-exec-2] org.hibernate.engine.jdbc.spi.SqlExceptionHelper.logExceptions Communications link failure
The last packet successfully received from the server was 200,187 milliseconds ago.  The last packet sent successfully to the server was 0 milliseconds ago.

有关挖掘什么的任何建议?

1 回答

  • 2

    您应该使用连接池 . 对于Hibernate,您可以使用c3p0 . 在您的hibernate属性中设置以下内容

    hibernate.connection.provider_class = org.hibernate.connection.C3P0ConnectionProvider
    

    然后,在c3p0.properties文件中,将这些属性重新设置为在数据库关闭时每3秒无限重新连接:

    c3p0.acquireRetryAttempts = 0
    c3p0.acquireRetryDelay = 3000
    c3p0.breakAfterAcquireFailure = false
    

    有关如何从数据库中断中恢复的详细信息,请参阅this section .

相关问题