java - How to properly use standalone TransactionManager in web application? -
i'd write next tiny java webapp using 1 of micro-frameworks. e.g. spark or jooby. problem both of them doesn't support jta, need use 1 of third-party libraries. i've googled open-source jta implementations , found two: atomikos , bitronix. seems latter project abandoned i've decided go atomikos. unfortunately documentation scarce can't find answers questions.
here use case. suppose have 2 dao classes methods should executed under single transaction:
class someservice { // both injected guice private foodao foodao; private bardao bardao; public void somemethod() { // both methods should executed in single transaction foodao.insert(new foo()); bardao.insert(new bar()); } }
i've never used javax.transaction
api directly/manually before (you know declarative transactions simple spring) i'm bit confused. jta provides 2 general abstractions(usertransaction
, transactionmanager
) , both have methods handle jdbc transaction. far understand transactionmanager operates usertransaction objects using threadlocal variables. therefore usertransaction should thread-confined , transactionmanager supposed thread-safe. correct?
so there several approaches possible:
- i can inject transactionmanager service (via guice) , use directly.
- i can share transactionmanager via public static variable.
- i can create usertransaction provider/factory (via guice) , objects it.
which correct/best-practice?
another question if familiar atomikos provides 2 transaction manager implementations (maybe more): j2eetransactionmanager , usertransactionmanager. again documentation scarce don't see difference except jndi. suppose usertransactionmanager should enough purposes description states following
j2eetransactionmanager
an implementation of transactionmanager should used j2ee applications.
.. without explanation.
ps. sorry english isn't know it.
Ок, since no-one answered me, after digging source code, think i'm ready answer own question:
- both atomikos , bitronix tms implemented singletons [1], [2]
- both atomikos , bitronix tms thread-safe. particularly, atomikos uses intrinsic synchronization , bitronix doesn't share state between threads.
- both atomikos , bitronix tms implement both (oops) transactionmanager , usertransaction interfaces in doesn't matter 1 you'll use. if @ atomikos usertransactionimp class you'll see internally instantiates , uses corresponding transactionmanager implementation.
so safe have 1 tm-per-webapp instance, can share way like. since atomikos tm supposed instantiated it's better share through dependency injection. bitronix tm can used through public static method other singleton.
additionally can manage transactions via jndi. can find example in java persistence hibernate book source code.
Comments
Post a Comment