Menu

Bug in transactions with thread?

Help
2005-07-12
2013-04-25
  • f crivellaro

    f crivellaro - 2005-07-12

    Hi,
    in my program I have this steps:
    - beginTransaction
    - start MyThread
    - other operations
    - endTransaction

    In MyThread I can't obtain a connection not null from Manager. Without the transactions it works fine.
    What is the problem?
    Thank's
    --
    Federico

     
    • nr

      nr - 2005-07-12

      This should work...
      Could you send us a code snapshot ?

      Thanks.

       
      • f crivellaro

        f crivellaro - 2005-07-12

        In MainProgram (it's a thread):

        Manager.getInstance().beginTransaction();
        ...
        ThreadAncestor ta = new MyThread();
        Thread processThread = new Thread(ta);
        processThread.start();
        ...
        Manager.getInstance().endTransaction(commit);

        In MyThread (run() method):

        MyTableManager m = MyTableManager.getInstance();
        m.loadByMyPK(id);

        THe method loadByMyPK(id) cannot retrieve a connection.

        What is wrong?
        Thank's.

         
        • Kelvin

          Kelvin - 2005-07-12

          If you look at the MyTableManager class you'll notice that the connection for the transaction is held on an InheritableThreadLocal variable. When you call m.loadByMyKP(id) this method calls getConnection which relies on the InheritableThreadLocal variable (if you have started a transaction, which you have). Since you are on a different thread at this point, your connection is null or you get a different connection than the one you were working with on your main thread. My recommendation would be to begin and end the transaction on the worker thread to avoid this conflict.

          Alternatively, you could manage all the connections yourself, but that'd get messy fast.

           
          • f crivellaro

            f crivellaro - 2005-07-13

            I want to manage two different transaction, in MainProgram and in MyThread. MainProgram continue his execution afterMyThread.start. So, I think this is a bug. Are you agree?

             
        • nr

          nr - 2005-07-12

          > processThread.start();
          > ...
          > Manager.getInstance().endTransaction(commit);

          Do you wait for the thread to complete before ending the transaction ?

          Also, which version of sql2java are you using ?

          Regards,
          Nicolas.

           
        • nr

          nr - 2005-07-13

          You should be careful: when you start a transaction (beginTransaction()), the current thread and all its child threads will work in this same transaction.

          If you need to run 2 differents transaction in parallel, you need first to create 2 threads and
          then  within each thread call beginTransaction.

          I highly recommend that you read
          http://java.sun.com/j2se/1.4.2/docs/api/java/lang/InheritableThreadLocal.html
          and understand how Manager handles transactions.
          This represent less than 40 lines of code :-)

          Please let us know it it helps
          Regards,

          Nicolas.

           

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.