Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusion on Transaction  RSS feed

 
Ying Zhou
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys:
I have this question about using Transaction.
I am working on a project developed by a 3rd party, and now when I look through their code, I see they have used many static "Helper" class. And most of the methods in helper classes are, synchronized. It seems runs fine, but, does this make sense?
Also, if say, I start a transaction in an ejb method, and from it, can I use a static helper class do some persistent work and back to original ebj method? Will transaction protect the data integrity if transaction failed in that static helper class method?
Any feedback are welcome.
Thanks
yz
 
Dave Landers
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ying Zhou:

I am working on a project developed by a 3rd party, and now when I look through their code, I see they have used many static "Helper" class. And most of the methods in helper classes are, synchronized. It seems runs fine, but, does this make sense?

This is probably fine, but it really depends on what the Helper classes are doing.
The developer using/writing the synchronized code has to recognize that the "synchronized" keyword might not have the right "scope" in the EJB world. That is, you might have an EJB deployed to several different machines in a load-balanced cluster. In that case, the "EJB component" is spread out over many machines and the java synchronized keyword does not know about those other machines.
So it really depends on what they are trying to protect with the synchronization. It is probably OK, but is not a substitute for transactions and data that should be in a database.
Also, when you load test the application, you might find a bottle-neck at these synchronized points. Some developers over-synchronize as a substitute for thinking about the hard problem of multiple threads.

Also, if say, I start a transaction in an ejb method, and from it, can I use a static helper class do some persistent work and back to original ebj method? Will transaction protect the data integrity if transaction failed in that static helper class method?

Yes, as long as the Helper class uses the TxDataSource obtained from the EJB container. The EJB could pass it to the helper or the helper could look it up using
new InitialContext().lokup("java:comp/env/MyDataSource")
or whatever.
Cheers,
Dave
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!