File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
venkata vinay bysani
Greenhorn
Posts: 25
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Pie
    Posts: 64194
    83
    IntelliJ IDE Java jQuery Mac Mac OS X
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    venkata vinay bysani
    Greenhorn
    Posts: 25
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 64194
    83
    IntelliJ IDE Java jQuery Mac Mac OS X
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic