This is because mysql stays connected for 8 hours by default. If the connection is not accessed within 8 hours, it will be closed. However, this problem is not considered in the design of connection pool, and taking out invalid connections will lead to exceptions.
Solution:
1, create a thread test connection every once in a while, keep the connection up-to-date, and never visit for 8 hours.
2. When the connection pool accepts the connection, it determines whether the connection is valid.
【java】? View the plain? copy
What if? (conn.isValid( 1000)){?
Return? conn?
}?
else{?
Return? This. create connection(); ?
}?
3, no connection pool, just take it and use it, and then return it.
The concept of connection pool:
Use pools to manage connections, so that connections can be reused. With the pool, we don't have to create the connection ourselves, but get the connection object through the pool. When the connection is completed, calling the close () method of the connection does not really close the connection, but "returns" the connection to the pool. The pool can reuse this connection object.
JDBC database connection pool interface (data source):
Java provides a common interface for database connection pooling: javax.sql.DataSource, and each vendor can make its own connection pooling implement this interface. In this way, applications can easily switch connection pools from different vendors!