This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes EJB and other Java EE Technologies and the fly likes J2EE specifications: synchronized keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "J2EE specifications: synchronized keyword" Watch "J2EE specifications: synchronized keyword" New topic
Author

J2EE specifications: synchronized keyword

Jean-Claude Rouvinez
Ranch Hand

Joined: Aug 26, 2003
Posts: 35
Hi,

We will use in our J2EE application a third party package. This package performs internally some synchronization (e.g. public synchronized byte[] buildPdf(){...}).

I know that it is forbidden to start own threads in a J2EE environment. It stays in each specification, tutorial or white paper.

Is it the same with the use of "synchronized" ?
Is it clearly mentioned in the specifications that the use of "synchronized" is forbidden or allowed?

Thank you very much for your help.


Jean-Claude
Sunil Dixit
Ranch Hand

Joined: Sep 22, 2005
Posts: 46
Hi,
You are 100% right. As per the EJB spec 'synchronized' shd not be used,and the reason is very simple,as you must be knowing that 'synchronized' keyword is used to avoid sharing conflict of a common resource in multi-threaded env.
And J2EE application are multithread,so its container responsibilities to handle all these stuff for user.
Even if you use 'synchronized' keyowrd in J2EE application than you are trying to make your container life tuff which you shd not be the case. Conatiner will do better handling of common resource,so we shd not use 'synchronized' keyword.
hope that this will help you.

Cheers,
Sunil Dixit
[ July 05, 2007: Message edited by: Sunil Dixit ]
Jean-Claude Rouvinez
Ranch Hand

Joined: Aug 26, 2003
Posts: 35
Thank you Sunil for your answer.

I totally agree with you but I found the following answer in this forum:
The EJB spec forbids you to make EJB methods synchronized, but it's generally OK to call a synchronized method in a helper class...
Kyle

(Helper Class
This could apply to my case (use of a helper class).

So this question still remains:
Where is it stated in the specifications, that the "synchronized" keyword is forbidden?

Thank you for your help.
Sunil Dixit
Ranch Hand

Joined: Sep 22, 2005
Posts: 46
Hi,
I don't agree with Kyle quotes,if that is true than we can do I/O in EJB using some helper class right ?

THIN line is EJB spec are the guidelines which say you should do that BUT it does not enforce user that he has to do that.

It 100% true that "synchronized" keyword is forbidden and the reason I have explained you.

Cheers,
Sunil Dixit
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


I don't agree with Kyle quotes,if that is true than we can do I/O in EJB using some helper class right ?

Yes, under certain circumstances. Don't get confused between can and should. For example, a helper class called from a session bean that preforms IO should be OK, presuming you can tolerate the non-transactional nature of file system IO, you don't deploy your EJB in a distributed environment, and you don't do any OS specific IO. You will also be undermining your security model, and changes to admin privileges on the host OS will have to be handled carefully, but this may be acceptable. For the reasons above it's not a smart thing to do, following the specification means you will stear clear of any such issues.
[ July 09, 2007: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Jean-Claude Rouvinez
Ranch Hand

Joined: Aug 26, 2003
Posts: 35
Hi,

I think that we will follow the advice of Paul Sturrock:

Yes, under certain circumstances. Don't get confused between can and should.


An other argument that makes me comfortable we the usage of "synchronized" under certain circumstances is that some classes provided by SUN use the "synchronized" mechanism.

For example, it is not forbidden for an EJB to use a Vector (Java 2 Platform) and the Vector uses the "synchronized" mechanism.



Thank you for all your answers.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: J2EE specifications: synchronized keyword
 
Similar Threads
Record read/write, unlock/write synchronization
Any Ideas ???
Is using servlets means using J2EE?
How to use class inside the default package inside other packages!!
Projects for J2EE to get the practical knowledge