Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

J2EE specifications: synchronized keyword

 
Jean-Claude Rouvinez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sunil Dixit
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Jean-Claude Rouvinez
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic