• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

When to use Instance vs Local variables

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

I think I understand the difference between an instance and a local variable. But I seem to have trouble understanding how it affects the performance at runtime. For example, I have a class that looks similar to...




Left like that, if multiple form submissions, say about 30 within 20 seconds, causes it to throw an error that connection is closed. I researched the error, and found a suggestion to NOT declare the conn object in the as an "instance" variable, but as a "local". So, I did this...




After I did that, I was able to submit over 100 forms with in 30 seconds withOUT problems.

I'm very happy it's fixed, but I don't understand why it caused the error in the first place.

For this class, I really didn't need to use an instance anyway, since there was only one method. But I tend to use instance variables quite often when I write a class that has several methods that may need to use the instance variable, especially a connection object, because once the connection is obtained, other methods won't have to retreive it again. Also, I have better control over handling DB transactions.

But, in the case explained above, can anyone explain what the difference was, and/or point me to some documentation regarding when to use local vs. instance variables?

Thanks very much.
Lulu
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Lulu Carr:
....
After I did that, I was able to submit over 100 forms with in 30 seconds withOUT problems.

I'm very happy it's fixed, but I don't understand why it caused the error in the first place.

For this class, I really didn't need to use an instance anyway, since there was only one method. But I tend to use instance variables quite often when I write a class that has several methods that may need to use the instance variable, especially a connection object, because once the connection is obtained, other methods won't have to retreive it again. Also, I have better control over handling DB transactions.

But, in the case explained above, can anyone explain what the difference was, and/or point me to some documentation regarding when to use local vs. instance variables?

Thanks very much.
Lulu



Don't declare any instance variables in action class as they are never thread safe. Struts action classes are cached and resued for performance optimization at the cost of having to implement the action classes in a thread safe manner.

Naseem
 
Elle Atechsy
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Naseem,

That makes sense. You have come through for me twice in a row!

Thank you very much!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!