Win a copy of Terraform in Action this week in the Cloud 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

How must i "synchronize" my servlets?

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

I'm quite new to java servlets.. I wrote a web app where it uses a hsqldb database and the jdbc driver to access it.. I wrote a specific class which is not a servet to handle the database..



Is this how i must do it? Is it safe to synchronize all database access methods.. Will it slow down the web app?

And this class returns the result set object to be handled by the exact servlet.. Is it safe to do so? That way i won't be able to close the connection.. Please give me some advice.. Thank you..
 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Malith,

In general it's a good thing to separate servlet from the jdbc/database related code.

I think it's better to define and use dbPath and conn only as local variables (inside the methods), and then you don't have to worry about synchronization.

on't return the resultset to the servlet:
-a servlet is for request/response, and should not have knowledge about how you store you data (rel. database or files or whatever)
-make sure you always close your connections, statements and resultsets
(do it in a finally block)

One more tip:
try to use connection pooling (with a datasource). It's faster and makes your code less dependant on a particular driver.

gr,
Herman
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic