Win a copy of Head First Android this week in the Android forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Getting Database Connection from Servlet

 
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I just would like to clear my doubt regarding obtaining a connection from each servlet coming from a connection pool.
Basically, I have been seeing this type of construct wherein the connections are being initialize at the
servlets init() method per each servlets.



But I have been following this for sometime wherein each connection is being gathered on the doXXX methods.



I often wonder which one is the proper and optimize approach in getting the DB connection or are both these methods considered not optimize.
Thanks
 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should not hold a database connection open for one millisecond longer than it is actually needed. How does that help you decide?
 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You should not hold a database connection open for one millisecond longer than it is actually needed. How does that help you decide?



Thanks for the reply Bear.

I actually dont get what you mean by this clearly. But allow me to answer my question based on your reply. Does it mean that its practical to get the connection during the doXXX method? Since an init() is being called by the container when it starts-up but it doesnt mean that a request already comes in.
On the contrary, I am not getting a connection on the init() method but I am just initializing the datasource object on my first approach. The getting of my connection thru the call to the dataSource.getConnection() happens in the doXXX method.

Can you give me pointers please? I am not exactly sure about my answer.

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mark Reyes wrote:Hi All,

I just would like to clear my doubt regarding obtaining a connection from each servlet coming from a connection pool.
Basically, I have been seeing this type of construct wherein the connections are being initialize at the
servlets init() method per each servlets.



But I have been following this for sometime wherein each connection is being gathered on the doXXX methods.



I often wonder which one is the proper and optimize approach in getting the DB connection or are both these methods considered not optimize.
Thanks




Hi,
Performing a InitialContextLokup() may not be a good idea because every time when a user logs your servlet container is going to call service() method which in turn calls the doGet() or doPost() methods. So this piece of intialcontextlookup will be executed may times and this is not a good idea.
So better try to create the datasource object only once i.e. a singleton at the server startup. This can be achieved by having a servlet i.e. configured as loadonStartup in web.xml and make the datasource as a singleton object. Hope my reply makes sense..
 
Bear Bibeault
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Best practice, of course, would be to abstract all this away in the Model layer -- your presentation layer shouldn't have any DB code in it.

One-time initialization should be performed up-front (by whatever layer), and then connections should be grabbed and quickly released as needed. Grabbing a connection and holding it in case the code later needs it is a very poor thing to do.
 
giridharan radhakrishnan
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes! getting a connection object in inti method is not a great idea...my last post just suggested to avoid frequent IntialContextLookups and also to emphasize that create datasource as a singleton.
 
Marshal
Posts: 26912
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

giridharan radhakrishnan wrote:my last post just suggested to avoid frequent IntialContextLookups



Why would that be something to avoid?
 
Ranch Hand
Posts: 754
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gatther all the data you want to persist in the database, then you do the work with the database.
 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

Maybe my initial code wasn't clear enough on what I have been doing. Allow me please to show the snippet of my codes from my servlet(s).
I do think that I am explicitly closing my connection at the end of the servlet's service method.



Yes! getting a connection object in inti method is not a great idea...my last post just suggested to avoid frequent IntialContextLookups and also to emphasize that create datasource as a singleton.



Hi giridharan, I often hear this but I really dont know how is this being done. Can you share me a resource or link where I could read this. Thanks.


giridharan radhakrishnan wrote:my last post just suggested to avoid frequent IntialContextLookups



Why would that be something to avoid?



Hi Paul, this has been my practice eversince. I always do the context lookup's on all my service method. It's just that, I think that there is a better way other than doing this so that's why I post here at the ranch.
 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

While searching for an answer to my question, I found this http://www.javaranch.com/journal/200601/JDBCConnectionPooling.html topic regarding connection pooling at javaranch.

Its a little bit dated so I am not sure if this is still the optimized approach and this has been my approach for some time. Can somebody please share the best
practice in getting a database connection from a connection pool? A link or a tutorial would be better.

 
Mark Reyes
Ranch Hand
Posts: 426
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

This is my third attempt..
I was checking below approach in obtaining a proper datasource connection from a connection pool.
I basically created a class that will be initialized one time only. This is the class where I will get the connection



I created a listener class and has chosen the ServletContextListener to create my class.


And in my servlet I just get the connection from my DBUtilClass.



Please kindly advise if this is correct or optimize approach. Thanks.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic