wood burning stoves 2.0*
The moose likes Servlets and the fly likes Can we declare db connection as an instance veriable in the class which is called from servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Can we declare db connection as an instance veriable in the class which is called from servlet" Watch "Can we declare db connection as an instance veriable in the class which is called from servlet" New topic
Author

Can we declare db connection as an instance veriable in the class which is called from servlet

venkata vinay bysani
Greenhorn

Joined: Feb 09, 2010
Posts: 25
What I am doing currently is:
I have a servlet and I am writing all the DB related operations in one class named "DBOperations"
Servlet will create the object of the DBOperations to call the methods in it and DBOperations is not a singleton
In DBOperations
  • I declared the Connection object as an instance veriable but not static
  • declared instance veriable as "private Connection dbConnection"
  • instantiating that veriable in the constructor

  • and using the connection in all the methods used in the DBOperations with out creating the connection in each and every method, my requirement has multiple db operations per request so my intention was to use only one connection per request instead of getting connection for each db operation

    Is there any problem if I declare the connection as an instance veriable.

    Also Wanted to know will the servlet refered classes also should be built as a thread safe.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60791
        
      65

    No. If you do that, every request that comes in will share that same instance variable.

    Rule #1: no read/write instance variables in a servlet.

    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    venkata vinay bysani
    Greenhorn

    Joined: Feb 09, 2010
    Posts: 25
    Hi Bear Bibeault thanks for the reply,

    I am not creating the instance veriable in the servlet directly.
    It is created in one of the class whose object is created by the servlet in get or post methods so that class object is also not an instance veriable in servlet.

    If in the above case also if the servlet shares the same DBOperations object(Even though the object is not refered as an instance veriable in servlet) to all the requests? In that case do we need to design the classes refered by the servlet in a thread safe manner as well
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 60791
        
      65

    We'll need more info on how the servlet is using this class. If a unique instance of the class is created for each invocation, usually there are no problems.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
     
    subject: Can we declare db connection as an instance veriable in the class which is called from servlet
     
    Similar Threads
    Please comment on FBN design
    why static should be avoided with web app?
    FBN Design Doubts Please Help!!
    Re: Closing database connection in destroy metod()?
    Java and MQSeries