Hibernate 导致过多的 time_wait 连接
我正在使用 Hibernate 3,并且面临与关闭连接相关的问题。
我使用的是c3p0-0.9.1.2.jar,我检查了Hibernate打开的与数据库服务器的连接,发现已建立的连接数量为5个;在服务器的某些 TCP 端口(见下面的日志)。
但是这些已建立的连接会不断更改它们所建立的 TCP 端口,从而释放它们之前使用的端口,使这些端口处于 TIME_WAIT 状态(而不是关闭它们)。
这种情况持续下去,数量已达数百个。对于处于 TIME_WAIT 状态的连接。
我不确定发生了什么以及为什么端口从已建立状态切换到 TIME_WAIT 并且之前的端口都没有关闭。
下面是通过运行 NETSTAT -ano|find "x.9" 获取的示例,其中 x.9 是数据库服务器 IP。
TCP x.124.x.66:4379 x.124.x.9:1433 TIME_WAIT 0
TCP x.124.x.66:4381 x.124.x.9:1433 TIME_WAIT 0
TCP x.124.x.66:4382 x.124.x.9:1433 TIME_WAIT 0
TCP x.124.x.66:4383 x.124.x.9:1433 TIME_WAIT 0
TCP x.124.x.66:4384 x.124.x.9:1433 TIME_WAIT 0
TCP x.124.x.66:4385 x.124.x.9:1433 TIME_WAIT 0
TCP x.124.x.66:4386 x.124.x.9:1433 ESTABLISHED 5916
TCP x.124.x.66:4387 x.124.x.9:1433 ESTABLISHED 5916
TCP x.124.x.66:4388 x.124.x.9:1433 ESTABLISHED 5916
TCP x.124.x.66:4389 x.124.x.9:1433 ESTABLISHED 5916
TCP x.124.x.66:4390 x.124.x.9:1433 ESTABLISHED 5916
我使用的 Hibernate.properties 文件。
hibernate.c3p0.min_size=5
hibernate.c3p0.timeout=2
hibernate.c3p0.max_size=50
hibernate.c3p0.idle_test_period=10000
hibernate.connection.release_mode=auto
感谢您的帮助。
I am using Hibernate 3 and I am facing a problem related to connections being closed.
I am using c3p0-0.9.1.2.jar and I checked onto the connections to database server opened by Hibernate, I found that there are established connections which are 5 in number; at some TCP ports of server (see below log).
But these established connections keeps on changing the TCP Ports on which they are established and thus releasing the earlier ports used by them, making these ports in TIME_WAIT state (rather than closing them).
This keeps on going and make the count in hundreds; for connections in TIME_WAIT condition.
I am not sure what is happening and why the ports are switching from Established to TIME_WAIT and none of the earlier ones are closing.
Below is the sample taken by running NETSTAT -ano|find "x.9" where x.9 is database server IP.
TCP x.124.x.66:4379 x.124.x.9:1433 TIME_WAIT 0
TCP x.124.x.66:4381 x.124.x.9:1433 TIME_WAIT 0
TCP x.124.x.66:4382 x.124.x.9:1433 TIME_WAIT 0
TCP x.124.x.66:4383 x.124.x.9:1433 TIME_WAIT 0
TCP x.124.x.66:4384 x.124.x.9:1433 TIME_WAIT 0
TCP x.124.x.66:4385 x.124.x.9:1433 TIME_WAIT 0
TCP x.124.x.66:4386 x.124.x.9:1433 ESTABLISHED 5916
TCP x.124.x.66:4387 x.124.x.9:1433 ESTABLISHED 5916
TCP x.124.x.66:4388 x.124.x.9:1433 ESTABLISHED 5916
TCP x.124.x.66:4389 x.124.x.9:1433 ESTABLISHED 5916
TCP x.124.x.66:4390 x.124.x.9:1433 ESTABLISHED 5916
Hibernate.properties file used by me.
hibernate.c3p0.min_size=5
hibernate.c3p0.timeout=2
hibernate.c3p0.max_size=50
hibernate.c3p0.idle_test_period=10000
hibernate.connection.release_mode=auto
Thanks for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TIME_WAIT只是一方或双方已经终止连接但尚未关闭的状态。根据您的操作系统,您可以微调连接在 TIME_WAIT 中的时长。所以这并不意味着什么坏事,只要没有太多的连接阻止新连接的创建,它就不会影响性能。
另一方面,当客户端连接到由主机名和目标端口指定的服务器时,客户端的连接将被分配到一个端口。原因是连接由四项标识:目标主机、目标端口、源主机、源端口。
这正是您在 netstat 输出中看到的情况:您有 5 个由池创建的已建立连接,以及一些即将关闭的处于 TIME_WAIT 状态的连接。每个端口都有自己的从某个范围动态分配的源端口。这是完全正常的。
TIME_WAIT is just the state when one or both sides have already terminated the connection, but it's not closed yet. Depending on your operation system, you can fine-tune how long connections should be in TIME_WAIT. So it doesn't mean anything bad, and it has no effect on performance as long as there are not too many of them preventing the creation of new connections.
On the other part of the question, when a client connects to a server specified by a hostname and a destination port, the connection on the client side will be assigned to a port. The reason for this is that a connection is identified by four things: destination host, destination port, source host, source port.
This is exactly what you see happening in your netstat output: you have 5 established connections created by the pool, and a few soon-to-be-closed one in the TIME_WAIT state. Each has its own source port dynamically assigned from a range. This is perfectly normal.