• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

local varriable vs instance varriable

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am creating a dao servlet class .All the database transaction are here.which one in better to use .local varriable or instance varriable ?
with respective to single thread and multithread?
 
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is DAO servlet ?
 
sahidul karim
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Database accesing class
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
your DB access code should not go in the Servlet... but that is another story.
Multithreading and instance variables do always offer the possibilities that something goes wrong IF not proper accessed/synchronized. Method local variables on the other hand are on the stack and are not "visble" to other threads.

That said, there are of course variables that need to be instance level and some that need to be method level...

So what variables (references ?) are you talking about ?

pascal
 
sahidul karim
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When will i go for instance varriable and when will i go for method varriable? give me some example.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are talking about a servlet, right?

Never use instance variables in servlets to store state. You do not have control over how many instances of your servlet are created; the servlet container controls this.

If you need to store state that needs to be accessible beyond the method in a web application, you should store it as attributes in the session. For example (untested code, it's been a while since I programmed a servlet):

I don't know what you are planning to store, but note that you cannot store database connections in the session this way. You should use a database connection pool for that, and retrieve a connection from the pool each time you need it (and release it back to the pool once you're done with it).
[ November 17, 2006: Message edited by: Jesper Young ]
reply
    Bookmark Topic Watch Topic
  • New Topic