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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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.
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
This should work...
Could you send us a code snapshot ?
Thanks.
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.
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.
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?
> 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.
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.