It's not a secret anymore!*
The moose likes JDBC and the fly likes Closing Statement. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Closing Statement." Watch "Closing Statement." New topic
Author

Closing Statement.

Costa lamona
Ranch Hand

Joined: Sep 24, 2006
Posts: 102
Hi

I am new to jdbc, closing a Statement is releasing its resources, I am wondering if this will include closing the connection itself.
also If I am connecting to localhost db,
Is creating a new Connection will be costy?
Is creating new Statement will be costy?

Thanks

SCJP 5
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30130
    
150

Hi Costa,
Closing a statement will not close the connection because a connection is a higher level concept. Closing a connection will sometimes close a statement but you should not rely on this.

Creating a connection is usually relatively costly. This is why people use connection pools. Creating a statement is not costly and a frequent operation.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Assuming that you're using a decent JDBC driver, closing a statement will only close any opened resultsets and closing a connection will close any opened statements and indirectly also its opened resultsets.

Obtaining a connection is the most expensive task in JDBC. You can improve performance by using connection pooling. When you're developing a webapplication, it's good to know that most self-respected application servers offers JNDI connection pooling out of the box. When you're developing a client application, then you may need to add connection pooling libraries so that you can make use of it. Commonly used ones are Apache Commons DBCP, C3P0 and Proxool
 
Consider Paul's rocket mass heater.
 
subject: Closing Statement.
 
Similar Threads
Hibernate Connection Pool
Jboss connection pool not releasing the connection from pool
refreshing a connection
J2CA0081E: Method cleanup failed while trying to execute method cleanup on ManagedConnection
Staying Connected?