wood burning stoves 2.0*
The moose likes Tomcat and the fly likes Connection Pooling samples 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 » Products » Tomcat
Bookmark "Connection Pooling samples" Watch "Connection Pooling samples" New topic
Author

Connection Pooling samples

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi guys! I just read about Tomcat's connectio pooling. I don't have any more questions about configuration right now... What I need to know is how to use the connection pool in my code. In the tomcat docs they had this snippet



But it doesn't give a clear explanation like how do I get a connection? The line below doesn't tell anything about this.



Also, would I happen to have a single object that does all database transactions such as insert, edit, delete. I have several methods in it. Would it be a wise idea to close the connection before the method ends or return a value? Or would it be better if I close the connection before the servlet forwards/redirects to a JSP or don't close the connection at all?


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9947
    
161

conn = ... get connection from connection pool ...


I believe you have created a datasource in Tomcat for connection pooling. If so, then to get the connection from the pool, you will do a lookup of the datasource and invoke the getConnection method on the Datasource object.
Something like:



[My Blog] [JavaRanch Journal]
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
May I ask if you could show me a complete sample? Yes, I am about to create a datasource in Tomcat. About the Context type you used in your code is it similar to the context interface provided by Sun?

http://java.sun.com/j2se/1.4.2/docs/api/javax/naming/Context.html

I happen to be following another example right here...

http://marc.theaimsgroup.com/?l=tomcat-user&m=102225547106556&w=2

but I am really confused with the lines



Hmmm...

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9947
    
161



Your datasource definition will be something like the above (I copied this from the code that you pointed me to).

Tomcat will then create a Datasource for you and bind it at: java:comp/env/theNameThatYouHaveSpecifiedForYourDatasource. In this case it will be java:comp/env/jdbc/TestDB.

Now whenever you want to access this datasource through your code, you will be using javax.naming.Context and doing a lookup for the datasource.

So your code will look like:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Connection Pooling samples
 
Similar Threads
OutOfMemory error
Connection, Resultset and statement
how do I handle SQLExceptions?
Proper way to close Connection, Statement & ResultSet
Closing Connection Which way ????