Spring app losing connection to MySql after 8 hours. How to properly configure?

前端 未结 2 781
悲&欢浪女
悲&欢浪女 2021-02-04 01:10

I\'ve got a Spring app that I believe uses DBCP connection pooling to connect to a MySql database. I say believe because this isn\'t an area I\'m very strong in and I\'m not pos

2条回答
  •  春和景丽
    2021-02-04 01:54

    The short answer is it should be enough. DBCP supports testing the connection on borrowing from the connection pool (the default), but also supports test on return and test while idle.

    It's also worth understanding what may be going wrong here. It sounds like something between your Tomcat server and the database is dropping the idle connection after a timeout (such as a router or firewall). The problem with this is that Tomcat thinks it still has a valid connection, tries to do some work with the connection and fails, but keeps the connection alive and returns it to the pool. Now any further attempt to talk to the database will fail if it is given the same broken connection from the pool.

    I think it was Michael Nygard's excellent 'Release It!' book that described this scenario in one of his from-the-trenches stories.

    You will also want to look into how MySQL cleans up dead connections as when Tomcat loses the connection after 8 hours the DB will also be unaware of the failed connection.

    One final point, if you are using Tomcat 7 switch to their new connection pool as it offers better performance than DBCP.

提交回复
热议问题