今天写的程序需要频繁执行连接断开mysql这一操作,而且时间间隔又很短,于是乎出现了下面这个问题
The driver was unable to create a connection due to an inability to establish the client portion of a socket.
This is usually caused by a limit on the number of sockets imposed by the operating system. This limit is usually configurable.
For Unix-based platforms, see the manual page for the 'ulimit' command. Kernel or system reconfiguration may also be required.
For Windows-based platforms, see Microsoft Knowledge Base Article 196271 (Q196271).
意思是说本地的套接字缓存不够了,因此获取不到数据库的连接,可参考这篇文章:http://support2.microsoft.com/kb/196271。
为什么套接字会不够了?因为,我刚才提到了,执行sql操作的时间间隔很短,但是在默认情况下,windows允许使用5000个临时TCP端口,任何端口关闭后,它将在TIME_WAIT状态下保持120秒,所以理论上讲如果 时间间隔 < 120/5000 的话,就会出现套接字用光的问题。
我的解决方法是:
1.使用连接池
2.每次执行完一个操作后再执行下Thread.sleep(200) 即可