• 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

Synchronized keyword will help to resolve ny defect?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In my program a cron job is runing for every five minutes and it calls a method to send an xml message.I am using Application server with two nodes.so when server starts two nodes are invoking the cron job simultaneously so my method is called twice and
i am getting some deffects.

1)Due to two server two times methods are called and two messages with same details sent,evethough I am checking my database properly the condition in my table.
2)Two different messages are appended and it is going as a single message.

I am going to use synchronized keyword to my method.That method will call other 3 methods.Do i need to use the synchronized keyword for other methods also?

By using synchronized keyword My issue will resolve?

Please give some suggestion.

Thanks
 
Ranch Hand
Posts: 84
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Balaji, you should use the synchronized keyword only in the entry points, if the 3 methods remaining are just called from this method then it will be ok not to synchronize them.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Synchronized may help but may not solve the problem by itself. We would need to see code to see if it would be useful. For example if the two nodes aren't using the same instance of the Object with the synchronized method, then synchronizing the method wouldn't help at all. One thing you would notice is that synchronizing won't prevent the method from being called twice. It will just prevent it from being executed twice at the same time. When the first time the method executes finishes, the second time will begin to execute and the code will still run twice.

So why do you have two nodes running the cron job, why not just one? And what defects do you get when the job is run twice (other than duplicating the work done)?
[ December 24, 2008: Message edited by: Steve Luke ]
 
balaji anatha padmanaban
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi steve,

Thanks for your reply.

The method should not exceute paralley,you mentioned that. just prevent it from being executed twice at the same time.This condition we need .
If the method run twice seperately that's not the problem because we are checking the tables with key's.

Our code sending the message using JMS.When we start the server two methods are called paralley(due to two server nodes present in the server) and executed twice.

So why do you have two nodes running the cron job, why not just one?
we configured only once in our code.I am not aware of how to run the cron once(eventhough two node are preseent) in server.

And what defects do you get when the job is run twice (other than duplicating the work done)?
 
balaji anatha padmanaban
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi steve,

Thanks for your reply.

THe problem is the method should not exceute paralley,you mentioned that. "synchronized just prevent it from being executed twice at the same time".This condition we need .
If the method run twice seperately that's not the problem because we are checking the tables with key's.

Our code sending the message using JMS.When we start the server two methods are called paralley(due to two server nodes present in the server) and executed twice.


So why do you have two nodes running the cron job, why not just one?

we configured only once in our code .I am not aware of how to run the cron once(eventhough two node are preseent) in Web sphere server.

And what defects do you get when the job is run twice (other than duplicating the work done)?

The defect we got is it sends the same message twice to the queue.

Thanks...
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by balaji anatha padmanaban:
Hi steve,

Thanks for your reply.

THe problem is the method should not exceute paralley,you mentioned that. "synchronized just prevent it from being executed twice at the same time".This condition we need .
If the method run twice seperately that's not the problem because we are checking the tables with key's.



If all you need to do is prevent simultaneous execution, and not duplicate calls, then synchronization is what you need. Now you have to ask yourself if both nodes using the same instance of the class with the method you want to synchronize? If they are, then synchronizing the method should be fine. But if they both are using different instances you will have to synchronize on something else that both nodes do share.
 
balaji anatha padmanaban
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Steve,

Now you have to ask yourself if both nodes using the same instance of the class with the method you want to synchronize?

NO,The problem you mentioned is right .Here two different instances are runing at so synchronized is not working for me.So i need to synchronize the object.Can you tell any suggestion?

Thanks..
 
Steve Luke
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Synchronize on something other than the Object itself. Either generate an Object in a context that both nodes share if the two nodes are running in the same JVM, or lock on some other resource they can both access, like a database row/value, a file, or a socket.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic