wood burning stoves 2.0*
The moose likes JDBC and the fly likes PreparedStatements & Connection Pools Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "PreparedStatements & Connection Pools" Watch "PreparedStatements & Connection Pools" New topic
Author

PreparedStatements & Connection Pools

Anthony Smith
Ranch Hand

Joined: Sep 10, 2001
Posts: 285
I have read a little about this on the internet on previous posts, but I have seen different things, so I want to make sure that I am understanding this correctly.

I have an web app server (tomcat). It maintains a persistent conncetion to the database (mypool) that I store in the servlet context. Each time my code needs to access the database it gets its own copy of mypool. So, even though I have several places in my code where I say:

Connection conn = mypool.getConnection();

Am I trully taking advantage of preparedstatements?

I have found articles and posts that says yes, because I am using the same Connection (well, a copy of it).

I have also found articles and posts that say (if I was reading it correctly) only for tat instance of the Conncetion (if I were doing a loop, which executed my sql).

Now that I think about it, objects are handled by reference. So, in theory, that code statement above does use the same connection and can take advantage of PreparedStatements.

right?


PreparedStatements
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30076
    
149

Anthony,
You are correct that you really have a reference to the pool rather than a copy of it. If there is one connection in the pool, everybody shares that connection. If there are multiple connections, each one gets it's own copy of the prepared statement. Over time, all the connections wind up with a copy.


[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
 
 
subject: PreparedStatements & Connection Pools
 
Similar Threads
JDBC-CONNECTION POOL
What is the concept of wrapper for JDBC
Comparing Session Management between JavaBean and Servlet.
Connection pool + performance
JSP and JDBC