Connection pool issue

前端 未结 1 1314
Happy的楠姐
Happy的楠姐 2021-01-14 05:38

If I launch my application after it was idle for some time, I used to get below error. ( I am using Spring+Hibernate+MySQL as DB )

ERROR [org.hibernate.uti         


        
1条回答
  •  难免孤独
    2021-01-14 05:57

    Here is the flow of events to illustrate what's happening:

    1. A connection is requested and used by the caller (application or connection pool)
    2. The caller keeps a reference to it so that the connection can be re-used
    3. The caller goes through a period of inactivity (for example, a dev system overnight or a QA system over the weekend).
    4. Once that database connection is not in use, the database considers the connection to be idle. Because it is idle, after a certain amount of time (MySQL default is 8 hours) the database closes the connection.
    5. The caller still has a handle to the connection, and when the caller tries to use the connection again unpleasantly discovers that connection has been closed.

    The reason autoReconnect=true works, and that the pool testing the validity of the connection works, is that you are instructing the calling system to test the connection for this situation and to try again if this situation happens.

    As for whether the validation query will affect performance: In theory it is using a connection to do something. In practice that something is so trivial that its effect is negligible in the context of your entire system.

    [EDIT]

    In this case Apache DBCP is the connection pool hanging on to the connection, but you do NOT want DBCP to close the connection after every call. The point of the connection pool is to keep a connection ready for the next call because creating connections is expensive. The connection objects maintained by the pool are backed by actual database connections, and the database is the one who closes that actual connection after the idle timeout period. Note that the timeout to close idle connections is configured on the database, not on the connection pool. Because of this, DBCP has no way of knowing whether the connection has been closed or not unless it actually tries to connect with it. That’s why you need a validation query.

    For more information about configuring DBCP, see the configuration page and the API docs.

    0 讨论(0)
提交回复
热议问题