jQuery in Action, 3rd edition
The moose likes JDBC and Relational Databases 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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "PreparedStatements & Connection Pools" Watch "PreparedStatements & Connection Pools" New topic

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.


Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33132

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.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link: http://aspose.com/file-tools
subject: PreparedStatements & Connection Pools
It's not a secret anymore!