我写的一个(LDAP)连接池,这个连接池类一加载,就建立了
30个常连接,同事检视之后,说我的连接都没有close
我当时就奇怪,我建立的是常连接,那就不用考虑close啊.
(当然会有连接的断开的情况,但那是断拉之后要重连的问题,那我考虑拉的)
他提出一种必需要close的情况,当客户端的jvm down拉之后
,那么这些连接实际上已经断开拉,但是服务器端并不能检测到
当然,连接最终会有个超时设置,服务器端(隔拉很久)才能检测到
但毕竟是太久拉,所以从资源利用效率的角度考虑,还是主动从客户端close为佳
所以它建议在连接池类中覆写finalize()方法,在这个方法中
close所有连接,但是我感到疑问的是jvm都dow拉,finalize()方法还会被调用吗??
我这位同事居然斩钉截铁说"一定会被调用".但他又说不出为什么如果说这是个处理连接池的一般思路,那么象tomcat的数据库连接池也是这么处理的吗??
我还没找到这方面的源码看?说不上什么体会
不知诸位大虾有何高见

解决方案 »

  1.   

    finalize进行内存回收也需要jvm啦,虚拟机都挂了,当然程序都跑不了了。
      

  2.   

    你是在jsp中使用connect吗?千万不要,不然没有办法关闭链接,只有等超时了才会释放,
    而且一定要在使用connect后close它,这个close不是真正的关闭connect,而是释放对connect的应用,就像yourconnect=null一样,链接池中的connect并没有真正关闭,现在流行的ConnectFactory都是这样使用的
      

  3.   

    Not only does the JLS provide no guarantee that finalizers will get executed promptly, it provides no guarantee that they'll get executed at all. It is entirely possible, even likely, that a program terminates without executing finalizers on some objects that are no longer reachable. As a consequence, you should never depend on a finalizer to update critical persistent state.---Effective Java要关闭你的连接,写在finally好了。finalize只是给你加一道保险而已,不可信赖(it provides no guarantee that they'll get executed at all)。JVM也是程序,程序死了还调什么啊~
      

  4.   

    Don't be seduced by the methods System.gc and System.runFinalization. They may
    increase the odds of finalizers getting executed, but they don't guarantee it. The only methods that claim to guarantee finalization are System.runFinalizersOnExit and its evil twin, Runtime.runFinalizersOnExit. These methods are fatally flawed and have been deprecated.---------------------------------------------------------------------------------
    The only methods that claim to guarantee finalization are System.runFinalizersOnExit and its evil twin, Runtime.runFinalizersOnExit. These methods are fatally flawed and have been deprecated.
      

  5.   

    最好的办法就是在finally把连接关闭。
    然后强制实行垃圾回收,System.gc();
      

  6.   

    System.gc();写了和没写一样。不写也会执行,写了也不会提前执行
      

  7.   

    我感觉connect很奇怪,你不close 它就一直在,会生成很多。不过你用连接池就可以了