• 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

Static Method

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a static method that instantiates an object. If more than one person accesses the method, will it be an issue ??

Any help will be appreciated ...
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it depends on what you're doing with the object. If you're doing database operations, then you might have an issue. So....what are you doing?
 
Vicky Mohan
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes,we are using database operation. I think the only way to avoid this is my making the method synchrnoized ..

Thanks for your reply
 
Richard Anderson
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You got it!
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why is an issue during the database operations? why not in another case (if not all another cases, many).

Can somebody explian?
 
Richard Anderson
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it might be a good idea to use synchronized methods on database operations like updates. Think about it...you wouldn't want two people updating the same row at the same time because you run the risk of one person having inaccurate data.
[ July 22, 2005: Message edited by: Richard Anderson ]
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The problem is not confined to database operations, not does it occur in static methods only. If a piece of code might be called by two callers simultaneouly, you need to ensure that the data they work with isn't altered by one of them while the other also accesses it; that could lead to inconsistencies. Synchronizing methods (or classes, or blocks) is a way in Java to avoid this concurrency issue. In databases this can sometimes be handled automatically; research "transaction isolation" (in which case you actually do not need to be concerned about them).
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Richard Anderson:
I think it might be a good idea to use synchronized methods on database operations like updates. Think about it...you wouldn't want two people updating the same row at the same time because you run the risk of one person having inaccurate data.



I respectfully disagree. Yes, it's definately important to make sure that no two threads can modify the same record at the same time, but this taks is much better left to your DBMS than to a bit of ad-hoc written Java code. DBMS vendors have invested countless hours and dollars in working out an efficient and secure locking scheme; this is their bread-and-butter, their very reason for existing. Don't think for a moment you can do better by putting a synchronized keyword here and there, you're wildly underestimating the difficulties you'll run into. Randomly tagging java methods with the synchronized keyword is a very quick way of killing any and all performance of your Java application.

Put your database updates in a relational transaction and leave record locking to the DBMS. That's what it's for.
 
Barend Garvelink
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mohen Vijay:
I have a static method that instantiates an object. If more than one person accesses the method, will it be an issue ??

Any help will be appreciated ...



That fully depends on the implementation of the static method. If the static method only accesses variables on the stack, you'll be fine. If the method does r/w access on any static variables or shared resources, you might get into trouble. Without a code fragment, it's impossible to say.

The key issue here, as Ulf Dittmer pointed out, is shared resources. That's where you might run into trouble. Two threads executing the same method per se is no cause for concern.
[ July 24, 2005: Message edited by: Barend Garvelink ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic