The problem is that one wants to access the transaction from different parts of your application (different DAOs for example), so it's usually done in this way to avoid having to pass the transaction object around the application (and leaking persistence logic into your app).
Also, transactions are usually related to the thread that takes a request (or jms message), so a ThreadLocal is a convenient place to put it. Most frameworks do this (Spring as an example).
If you don't set the transaction on a thread local two things can happen
- Each DB access needs to use a different transaction.
- All the transactions are mixed up, as a commit on one request, affects the changes on a different thread.
Do you see any better way of doing this Adala?