<a href="http://moongrails.blogspot.com/" rel="nofollow">grails</a>
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Reid - SCJP2 (April 2002)
If you are just doing a single query and then closing the connection, there is no need to explicitly close statements and result sets because the connection close automatically closes all open JDBC objects associated with the connection.
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Originally posted by Roger Chung-Wee:
This looks to me like something which is vendor-dependent, so I won't rely on it.
Reid - SCJP2 (April 2002)
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Reid - SCJP2 (April 2002)
Tell me the one implementation on the face of planet Earth released in the last 5 years that you know does not cause a close on the statement to be called when you call a close on the connection. Just one. Driver or connection pool, in container or outside of container, any one is ok by me.
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Reid - SCJP2 (April 2002)
Connection closes on a pool still close the associated JDBC objects.
Although Statements and ResultSets should be closed when a Connection is closed, per JDBC specification, it's a good practice to explicitly close Statements and ResultSets right after you finish using them if you create multiple Statements on one Connection object. If you don't explicitly close Statements and ResultSets right away, cursors may accumulate and exceed the maximum number allowed in your DB before the Connection is closed.
I think if you do some testing, you'll find that statements have nothing to do with database cursors, not even for prepared statements or SQL batching.
To increase performance, WebLogic Server provides a feature to cache prepared statements and callable statements when you use connection pool. When WebLogic Server caches a prepared or callable statement, in many cases, the DBMS will maintain a cursor for each open statement. Hence, statement caching could be a source of the "maximum open cursors exceeded" problem. The Statement Cache Size attribute determines the total number of prepared and callable statements to cache for each connection in each instance of the connection pool. If you cache too many statements, you may exceed the limit of open cursors on your database server.
Important: The cursor associated with a REF CURSOR is closed
whenever the statement object that produced the REF CURSOR is
closed.
Unlike in past releases, the cursor associated with a REF CURSOR
is not closed when the result set object in which the REF CURSOR
was materialized is closed.
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Reid - SCJP2 (April 2002)
Originally posted by Wei Dai:
statement.close() should have not infection on transaction, but connection.close() will call connection.rollback();
If a connection is in auto-commit mode, then all its SQL statements will be executed and committed as individual transactions. Otherwise, its SQL statements are grouped into transactions that are terminated by a call to either the method commit or the method rollback.
Originally posted by Roger Chung-Wee:
Indeed, closing a Statement does indeed result in closing the ResultSet according to the API documentation. And this is the point. A developer must code to the API, and the API documentation for closing a Connection does not say that a related Statement is also closed. On this basis, a developer must regard the closing of the Statement and Connection objects as separate tasks to be coded.
Note that no mention is made of what happens when a connection is closed without either a commit or rollback
--------------------------------------------------------------------------------
The JDBC 3.0 spec has this to say about transactions (when auto-commit mode has been disabled).
quote:
--------------------------------------------------------------------------------
When auto-commit is disabled, each transaction must be explicitly commited by calling the Connection method commit or else explicitly rolled back by calling the Connection method rollback.
--------------------------------------------------------------------------------
Furthermore, should the Connection not be closed, it will be if it is later garbage collected.
At any rate I believe that transactions inside a connection that is closed (either because you actually closed it or the connection is lost (bad network, time out etc)) should be rolled back. But one should be aware that nowhere is this behaviour garaunteed.
I cannot speak too much as to why this behaviour is not specified other than I would speculate that one or more databases may well not rollback in the case of close and so it was decided not to include it in the specification. I would guess that in those instances the transaction may be just left abandoned resulting in orphaned locks on tables and or rows used by the transactions in question.
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
today's feeble attempt to support the empire
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|