8 Replies Latest reply: Jun 19, 2012 5:51 AM by 546634 RSS

    Usertransaction

    546634
      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
        • 1. Re: Usertransaction
          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.
          • 2. Re: Usertransaction
            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
            • 3. Re: Usertransaction
              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?
              • 4. Re: Usertransaction
                Nigeldeakin-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
                • 5. Re: Usertransaction
                  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.
                  • 6. Re: Usertransaction
                    Nigeldeakin-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
                    • 7. Re: Usertransaction
                      r035198x
                      Schooled. Yes, static here removes all bets.
                      • 8. Re: Usertransaction
                        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