近期做一个后台管理系统, 用到了c3p0数据库连接池, 和struts2 。但是经常会出现获取不了connection的情况,遇到这个问题的时候, 我对于这个问题的分析
1.首先检查我数据库的 一些关键配置是否存在,比如说 最大连接数,和超过最大连接数的累加数的设置。 我试图将他们都改大,看能否解决我这个问题。但不是很乐观。 还是没能解决这个问题。
2.我开始去检查我mysql 和c3p0 的相关jar包是否全部引入。
3.是否超过mysql允许的最大连接数 。
经过检查后, 都不是上述问题引起的 。还是获取不了连接
报错信息如下
Connections could not be acquired from the underlying database!
在我转而想去用hibernate的时候。 在无意中发现。当我程序获取不了连接的时候, 数据库也获取不了连接了。
让我开始怀疑是不是我Connection.close()方法是无效的。
但是从网上的资料来看, c3p0的关闭其实不是真正意义的关闭。 而是将连接退回连接池中。
但是这也不能造成我mysql从客户端也连接不了。 而且, 我数据库的最大连接数 我设置了1000;
这时候, 我将问题的根源转向了 mysql的配置文件 my.ini。在这个文件中 我发现了一个配置项,
# How many threads we should keep in a cache for reuse. When a client
# disconnects, the client's threads are put in the cache if there aren't
# more than thread_cache_size threads from before. This greatly reduces
# the amount of thread creations needed if you have a lot of new
# connections. (Normally this doesn't give a notable performance
# improvement if you have a good thread implementation.)
thread_cache_size=8
我的问题解决了