Jdbc should i close resultset




















Zeemee Zeemee 9, 12 12 gold badges 48 48 silver badges 76 76 bronze badges. Add a comment. Active Oldest Votes. What you have done is perfect and very good practice. So you can't always count on connection. I hope this helps :. Improve this answer. Paul Paul 4, 3 3 gold badges 27 27 silver badges 38 38 bronze badges. I agree that it is good practice to close result sets and statements.

However, the result sets and statements are garbage collected - they don't stay open forever and you don't "run into many different new problems".

Ralph Stevens - You cannot count on that. Paul - Interesting. That sounds to me like a shortcoming of the JDBC driver. Show 7 more comments. Christian Hujer You don't need to nest like this, you can do it all in one try-with-resources, just treat the resource declarations as separate statements separated by ; — Mark Rotteveel.

Mark Rotteveel: you can use a single try for all three Connection, Statement and ResultSet, but if you want to perform several queries, you must close the previous ResultSet before starting a new query. In other words, as the answer is now. Show 4 more comments. From the javadocs : When a Statement object is closed, its current ResultSet object, if one exists, is also closed.

They simply state that closing a Connection: Releases this Connection object's database and JDBC resources immediately instead of waiting for them to be automatically released. Thanks dogbane. The point is that you cannot depend on the implementation of Connection. I'm now using Oracle with Java. Here my point of view : You should close ResultSet and Statement explicitly because Oracle has problems previously with keeping the cursors open even after closing the connection.

I think you may encounter with the same problem with other databases you use. Here is tutorial Close ResultSet when finished : Close ResultSet when finished Close ResultSet object as soon as you finish working with ResultSet object even though Statement object closes the ResultSet object implicitly when it closes, closing ResultSet explicitly gives chance to garbage collector to recollect memory as early as possible because ResultSet object may occupy lot of memory depending on query.

Thanks hilal, these are good reasons to close it as early as possible. However, does it matter if ResultSet and Statement are closed direcly before the Connection this means in some cases: not as early as possible? If you close connection, it will close all the resultset ans statement also but you should close resultset before the connection — user And why should i close the resultset before the connection?

You mean because of the oracle driver problems? In theory, if you close the statement you don't have to close the resultsets, but it's probably good practice. Show 3 more comments. Enerccio Enerccio 1 1 gold badge 10 10 silver badges 22 22 bronze badges.

Things change when you mix Java with external applications. If these applications manage a finite number of resources, Java can try to get it right for you but may not be perfect. I believe you can have similar problems in AWT if you don't close some of the resources. Personally I prefer the defensive technique: Always try to make sure you release resources when required, because if it ever becomes an issue it will take you two weeks or more to diagnose and fix the problem.

Sirish Kumar Gongal Reddy. Hi, what David O'Meara said is exaclty suitable. Thanks, G Sirish Reddy. Consider Paul's rocket mass heater. Random Using java. Random How does java. Random work and how good is it?

Subclassing java. Random Randomness of bits with LCGs 'java. Introductions to Exceptions and error handling in Java Exceptions in Java: when to catch and when to throw? When the connection is "too old" see below. Statement In a finally clause, after executing the query.

If convenient, just before closing the Connection. ResultSet Never: just rely on closing the Statement but see below. In a finally clause, after reading all required results. Summary of when to call close on various JDBC resources.



0コメント

  • 1000 / 1000