Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Usertransaction

546634Jun 16 2012 — edited Jun 19 2012
A short question regarding use of static when declaring a usertransaction variable, i have seen use of this

Private static UserTransaction ut = null;

If two users accsess the class at the same time, vold it be that one is overwriting the other?
Or is usertransaction threadaware, so no conflicts occure?
Regards, reZer

Comments

r035198x
UserTransaction doesn't of itself do any synchronization for you.
It's up to you to use it in a thread-safe way. e.g CMT EJB methods are thread-safe so you can use it safely in them.
546634
Thanks's for you reply.
So what you say is that if you use private static instead of just private in an Container Managed EJB there is no way two threads can conflict?

regard, Ketil
r035198x
If you use it in EJB methods only then yes. The container guarantees that methods run in EJB methods are thread safe. The worry here is that maybe the user transaction was made static so it could be accessed from some static method (non EJB method) so you still need to be careful with how how you use it. Do you know why it was made static in the first place?
Nigel Deakin-Oracle
reZer wrote:
A short question regarding use of static when declaring a usertransaction variable, i have seen use of this

Private static UserTransaction ut = null;

If two users accsess the class at the same time, vold it be that one is overwriting the other?
Or is usertransaction threadaware, so no conflicts occure?
If you use a static field then all instances of your object will use the same UserTransaction instance.

Is this a EJB? The idea of EJBs being "thread-safe" means that each instance will be used by only one thread at a time. However if you have more than one instance (which is likely), then these might be used by different threads at the same time, and since UserTransaction is static they will all be using the same static UserTransaction object.

Why do you think it needs to be static? Are you really trying to share the same transaction between multiple instances?

Nigel
gimbal2
r035198x wrote:
If you use it in EJB methods only then yes. The container guarantees that methods run in EJB methods are thread safe.
Only the container part is thread safe. If you stick an object in a local variable yourself in stead of using the container's injection mechanisms , you've made it unsafe again.

The container does not have any responsibility in this respect other than following the specifications; thread safety is and always will be up to the dev. You're the captain, not a piece of tech.
Nigel Deakin-Oracle
r035198x wrote:
If you use it in EJB methods only then yes. The container guarantees that methods run in EJB methods are thread safe.
The container is only required to guarantee that each bean instance will be used by one thread at a time. However making the field static means you've got one UserTransaction object shared between multiple instances, so you might well have two bean instances accessing the same UserTransaction in different threads at the same time.

Essentially, non-static fields are thread-safe, static fields are not.

Nigel
r035198x
Schooled. Yes, static here removes all bets.
546634
Thanks for replies!

This is an old web app and why it is static is unknown, the application is following the business delegate pattern JSP---> Struts Action --> BD on the web layer , and use EJB --> CMD --> DAO on the model layer. It seems static should have been prevented in this case.

regards, reZer
1 - 8
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 17 2012
Added on Jun 16 2012
8 comments
3,811 views