4 Replies Latest reply on Apr 17, 2007 10:35 AM by ypsilon

    TransactionLocal: How to use it?

    ypsilon

      Hi.

      Sorry to ask such a trivial question here but is there anywhere any more documentation on the TransactionLocal object? I could not find anything more than the API doc. No example or anything.

      Actually I am trying to use that object to obtain a reference to an Object within a transaction no matter where I am in the current call stack. From my observations so far I found out that it would not work for that pupose as you have to have the same TransactionLocal Object in order to get your data back. Is that the way it is supposed to work?

      Thanks in advance again.

      Greetings
      PeeR

        • 1. Re: TransactionLocal: How to use it?

          TransactionLocal works the same way as ThreadLocal,
          just replace the notion of thread with transaction.

          Like thread local, if you want to pass references under the wire of an interface
          you need a singleton (or some way to get the same instance).

          public MySingleton
          {
           public static final TransactionLocal singleton = new TransactionLocal();
          }
          


          Such singletons have a number of problems:

          1) Unless you implement something a lot less naive than the above
          the information in the singleton is public and so could cause security issues
          if the information is sensistive
          or just broken behaviour if somebody modifies the data unexpectedly/maliciously.

          2) They lead to breakage of the type system (ClassCastException)
          in more complicated classloading schemes when not deployed correctly.

          3) Singletons that store state usually lead to memory leaks unless handled correctly.
          At least with the TransactionLocal, the data is "tidied up" when the tranasction ends.

          • 2. Re: TransactionLocal: How to use it?
            ypsilon

            Hi adrian

            thanks a lot for your reply (unfortunatly I wasn't watching this thread so I stumbled upon your reply so late).

            Meanwhile I implemented a similar functionality by using an XAResource. Since this is kind of ugly (but works fine ;-) ) I consider removing that in order to slim the transaction processing.

            What I actually did not understand is your second concern. (I am not too familiar with the internals of classloading.) Can you point me to a scenario where the classloader would lead to a ClassCastException? Does this only occur upon redeployments?

            Thanks again
            Greetings
            PeeR

            • 3. Re: TransactionLocal: How to use it?

               

              "ypsilon" wrote:
              Can you point me to a scenario where the classloader would lead to a ClassCastException? Does this only occur upon redeployments?


              Correct, in general
              public class DoesntGetRedeployed
              {
               public static Collection staticCollection = ...;
              }
              
              public class CodeThatGetsRedeployed
              {
               public void doSomethingBeforeRedeploy()
               {
               DoesntGetRedeployed.add(this);
               }
              
               public void doSomethingAfterRedeploy()
               {
               Object x = DoesntGetRedeployed.get();
               // x instanceof CodeThatGetsRedeployed == false
               }
              }
              


              Although, I'd be impressed if you could come up with such a situation
              WITHIN A TRANSACTION! :-)

              • 4. Re: TransactionLocal: How to use it?
                ypsilon

                 

                "adrian@jboss.org" wrote:

                Although, I'd be impressed if you could come up with such a situation
                WITHIN A TRANSACTION! :-)


                I sure did some fancy stuff, but no I would rather not come up with such a situation.

                Again, thanks for the helpful information.

                Greetings
                PeeR