JDBC garbage collection

前端 未结 5 823
傲寒
傲寒 2020-12-19 20:26

What happens if i don\'t close resultset or preparedstatements.

Will they be closed and released by the garbage collector.

I\'m asking this for local variabl

5条回答
  •  生来不讨喜
    2020-12-19 21:11

    if i don't close result set or prepared statements.Will they be closed and released by the garbage collector.

    resultset and preparedstatment are closed ,by explicitly calling close method. Garbage collector will not close these. I you do not call close , then the oracle cursor is not released at the oracle end.

    Will they be released by the garbage collector.
    

    Generally an object becomes eligible for garbage collection in Java on following cases:

    • All references of that object explicitly set to null e.g. object = null
    • Object is created inside a block and reference goes out scope once control exit that block.
    • Parent object set to null, if an object holds reference of another object and when you set container object's reference null, child or contained object automatically becomes eligible for garbage collection.
    • If an object has only live references via WeakHashMap it will be eligible for garbage collection.

    for your question : I'm asking this for local variables inside a function.

    ResultSet Object created inside a method, not closed and reference goes out scope ,once control exit that method . , then the reference is set to null and object is eligible for garbage collection. I did say eligible not guaranteed.The underlying oracle cursor is still there in the database.because u did not call close.

提交回复
热议问题