• Post Reply Bookmark Topic Watch Topic
  • New Topic

How must i "synchronize" my servlets?  RSS feed

 
Malith Yapa
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • 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..
 
Herman Schelti
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • 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
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!