• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Is TransactionSynchronizationRegistry safe to use?

 
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy Ranchers!

I wanted to see if my CMT Transaction was reused in the following EJB invocation.
You can easily achieve it using BMT and accessing UserTransaction. As the UserTransaction is not available for CMT, there is only one method which can give you some information about the transaction status - SessionContext#getRollbackOnly().

However I wanted to see if the following EJB was reusing the Tx, not just to see if it is maked for rollback, so I've made some tests with TransactionSynchronizationRegistry.

It works fine and shows proper values, but my doubts are:

- Is accessing this object and getting the Tx status safe or it's possible that in some cases it would fail?
- I know it's intended to be used by system-level components, so I guess it's a little hacky to obtain it here, right?
- Is it mandatory for the container to publish the resource at java:comp/TransactionSynchronizationRegistry (the Javadoc talks only about standard place).
- If it's safe to use, why do you think this object is not able to be injected using @Resource?

Thanks in advance,
Cheers!
 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As per Java EE6 spec:

Section EE.5.11
The JTA TransactionSynchronizationRegistry interface may be used by system level components such as persistence managers that may be packaged with EJB or web application components. Such components can find an appropriate object implementing the TransactionSynchronizationRegistry interface by looking up the JNDI name java:comp/TransactionSynchronizationRegistry or by requesting injection of a TransactionSynchronizationRegistry object using the Resource annotation.



So it's valid to use it and you should be able to get it with @Resource injection. Which application server is this and where are you trying to inject it?
 
Piotr Nowicki
Ranch Hand
Posts: 623
1
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jaikiran for prompt answer

You were right, the injection:
@Resource TransactionSynchronizationRegistry
is successful and the returned values are valid (tested on Glassfish 3.1.1).
I couldn't find information about TransactionSynchronizationRegistry injection in EJB specification, but I guess it's at least the second time when I got punished for not reading the Java EE umbrella specification :-)

Once again thanks!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic