Win a copy of Spring Boot in Practice this week in the Spring 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 ...
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

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.

Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic