ejb-3.0ejb-jar.xml

Accessing <transaction-type> configration of ejb-jar.xml in EJB class


I have an EJB deployed with configurations in ejb-jar.xml and I would like to access

<transaction-type>

configurations in my EJB class during ejbCreate() i.e. Bean or Container as I have to pass this value to my framework for internal work.

Note: I do not want repeat and define an additional env-entry for the same as its already there and I would like to use it


Solution

  • There is no method to determine this information directly. However, you can get at the information indirectly by relying on the fact that EJBContext.getUserTransaction throws an exception for CMT:

    @Resource EJBContext ejbContext;
    
    private boolean isBeanManagedTransaction() {
      try {
        ejbContext.getUserTransaction();
        return true;
      } catch (IllegalStateException e) {
        return false;
      }
    }
    

    Note, the getUserTransaction method cannot be called from all container callbacks (see the table of allowed operations in the EJB spec), but fortunately, getUserTransaction can be called from ejbCreate/PostConstruct, so this method should work for your purposes.