• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Connection Pooling samples

 
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:

 
Timothy Sam
Ranch Hand
Posts: 751
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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:

 
Can't .... do .... plaid .... So I did this tiny ad instead:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic