欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

采用数据库连接池连接到mysql时,因应用中没有关闭ResultSet导致的memory leak问题 mysqljdbc应用服务器数据库连接池 

程序员文章站 2022-04-22 08:33:42
...
/////////////begin///////
So it seems that even if you close all you statements and resultsets, if you do not close the connection,
it keeps references to them and the GarbageCollector can't free the resources.

My solution: after a long search I found this statement from the guys at MySQL:

"A quick test is to add "dontTrackOpenResources=true" to your JDBC URL.
If the memory leak goes away, some code path in your application isn't closing statements and result sets."
/////////////end/////////
==>是这样吗?
==>即使你关闭所有的statement和resultset,但是如果你没有关闭connection的话,connection就会引用到statement和resultset上,从而导致GC不能释放这些资源(statement和resultset)。
==>快速测试验证:如果在JDBC的url加上参数【dontTrackOpenResources=true】,memory leak消失的话,则说明应用中某些代码没有关闭statement和resultset。
==>"When the connection property dontTrackOpenResources=true was used, the result set was closed after a statement.close() was issued."
==>【小结】当采用参数【dontTrackOpenResources=true】时,在statement关闭后,resultset也会被关闭。==>从而证明,应用中有没有关闭的ResultSet对象。
==>http://bugs.mysql.com/bug.php?id=69746
==>考虑将连接池的最小空闲数配为0==>测试结果==>不可行
==>https://docs.oracle.com/cd/E17952_01/connector-j-relnotes-en/news-5-1-28.html
////////////begin/////////
When the connection property dontTrackOpenResources=true was used, the result set was closed after a Statement.close() was issued. (Bug #17164058, Bug #69746)
http://bugs.mysql.com/bug.php?id=69746
////////////end//////////

【Tip】程序中需要显式关闭ResultSet吗?

==>Note:When a Statement object is closed, its current ResultSet object, if one exists, is also closed.
==>http://docs.oracle.com/javase/6/docs/api/java/sql/Statement.html
  >>>因为:当statement对象关闭时,其所产生的或者关联的ResultSet对象,如果存在,也会被关闭的。
  >>>说明,不显式关闭ResultSet是正常的行为,但是实际开发中,还是建议用完后,立即关闭并赋为null。
==>对象间引用关系:Connection==>产生==>Statement==>产生==>ResultSet

==>A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row.
The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object,
it can be used in a while loop to iterate through the result set.
==>技术细节:ResultSet维护的是一个游标,其初始指向第一行之前,而不是指向第一行的。因为代码中,取数据需要执行next()方法的。

【温馨提示】
如果您觉得满意,可以选择支持下,您的支持是我最大的动力:

采用数据库连接池连接到mysql时,因应用中没有关闭ResultSet导致的memory leak问题
            
    
    
        mysqljdbc应用服务器数据库连接池