File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 29241
    
139

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: PreparedStatements & Connection Pools
 
Similar Threads
Comparing Session Management between JavaBean and Servlet.
What is the concept of wrapper for JDBC
Connection pool + performance
JDBC-CONNECTION POOL
JSP and JDBC