File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

PreparedStatements & Connection Pools

 
Anthony Smith
Ranch Hand
Posts: 285
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Marshal
Posts: 33671
316
Eclipse IDE Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic