Spring jta-transaction-manager

18,599

Can tx:jta-transaction-manager use id as name so that I can pass it as REF to my service layer like below?

The <tx:jta-transaction-manager> exposes the transaction manager as a Bean in the Spring context with the name "transactionManager".

Can tx:jta-transaction-manager only be used with a J2EE container?

Quoting the Chapter 9. Transaction management from the Spring documentation:

Is an application server needed for transaction management?

The Spring Framework's transaction management support significantly changes traditional thinking as to when a J2EE application requires an application server.

In particular, you don't need an application server just to have declarative transactions via EJB. In fact, even if you have an application server with powerful JTA capabilities, you may well decide that the Spring Framework's declarative transactions offer more power and a much more productive programming model than EJB CMT.

Typically you need an application server's JTA capability only if you need to enlist multiple transactional resources, and for many applications being able to handle transactions across multiple resources isn't a requirement. For example, many high-end applications use a single, highly scalable database (such as Oracle 9i RAC). Standalone transaction managers such as Atomikos Transactions and JOTM are other options. (Of course you may need other application server capabilities such as JMS and JCA.)

The most important point is that with the Spring Framework you can choose when to scale your application up to a full-blown application server. Gone are the days when the only alternative to using EJB CMT or JTA was to write code using local transactions such as those on JDBC connections, and face a hefty rework if you ever needed that code to run within global, container-managed transactions. With the Spring Framework, only configuration needs to change so that your code doesn't have to.

So, as explained in the third paragraph, if you want to work with multiple transactional resources, you'll need global transactions which involve a JTA capable application server. And JTA capable application server means a real J2EE container or a non J2EE container (like Tomcat) with a standalone transaction manager like Atomikos, JOTM, Bitronix, SimpleJTA, JBossTS or GeronimoTM/Jencks.

FWIW, I've seen lots of complains about JOTM, I think that GeronimoTM/Jencks lacks of documentation, I can't really say anything about JBossTSArjunaTS (except that it's a rock solid product), SimpleJTA and Bitronix have both good documentation and Atomikos is an impressive product greatly documented too. Personally, I'd choose Bitronix or Atomikos.

PS: Honestly, if this sounds like Chinese to you, you should maybe consider using a single database (if this is an option, go for it!) or consider using a real J2EE container like JBoss or GlassFish as I wrote in a previous answer. No offense but all this JTA stuff is not trivial and taking the JOTM path is not that simple if you don't really understand why you need it.

Share:
18,599

Related videos on Youtube

cometta
Author by

cometta

Updated on April 19, 2022

Comments

  • cometta
    cometta about 2 years

    Using Spring:

    1. can jta-transaction-manager use id as name so that I can pass it as REF to my service layer like below?

    2. is tx:jta-transaction-manager can only be used for je22 container? I mean for Tomcat, I need to do it manually, like below:

      <tx:jta-transaction-manager id="name_transactionmanager"/>
      
          <bean id="projectService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
              <property name="transactionManager"
                        ref="name_transactionmanager"/>
              <property name="target">
                  <bean
                    class="com.company.project.company.services.ServiceImpl"
                    init-method="init">
                       <property
                         name="HRappsdao"
                         ref="HRappsdao"/>
                       <property
                         name="projectdao"
                         ref="projectdao"/>
                  </bean>
              </property>
              <property name="transactionAttributes">
                  <props>
                      <prop key="store*">PROPAGATION_REQUIRED</prop>
                      <prop key="update*">PROPAGATION_REQUIRED</prop>
                      <prop key="remove*">PROPAGATION_REQUIRED</prop>
                      <prop key="bulkUpdate*">PROPAGATION_REQUIRED</prop>
                      <prop key="*">PROPAGATION_SUPPORTS,readOnly</prop>
                  </props>
              </property>
          </bean>
      

    For question 2

        <bean id="transactionManager"
          class="org.springframework.transaction.jta.JtaTransactionManager">
            <property name="userTransaction">
                <bean class="org.springframework.transaction.jta.JotmFactoryBean"/>
            </property>
        </bean>
    
  • Dchucks
    Dchucks about 12 years
    Your answer is excellent, I came to this thread looking for a way to Unit test my DAOs (that are EJBs themselves and use other EJBs, as well as need Transaction context) outside the container but with a live Database. I was able to mock most of other EJB logic in POJOs however due to lack of transaction context some of the other code fails while running the tests. I can not use Spring, I guess it would at least need a webserver. Using a standalone transaction manager seems an overkill just for Unit Tests, or what do you think?