views:

1205

answers:

1

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"/>
    
    
    
    &lt;bean id="projectService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"&gt;
        &lt;property name="transactionManager"
                  ref="name_transactionmanager"/&gt;
        &lt;property name="target"&gt;
            &lt;bean
              class="com.company.project.company.services.ServiceImpl"
              init-method="init"&gt;
                 &lt;property
                   name="HRappsdao"
                   ref="HRappsdao"/&gt;
                 &lt;property
                   name="projectdao"
                   ref="projectdao"/&gt;
            &lt;/bean&gt;
        &lt;/property&gt;
        &lt;property name="transactionAttributes"&gt;
            &lt;props&gt;
                &lt;prop key="store*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
                &lt;prop key="update*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
                &lt;prop key="remove*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
                &lt;prop key="bulkUpdate*"&gt;PROPAGATION_REQUIRED&lt;/prop&gt;
                &lt;prop key="*"&gt;PROPAGATION_SUPPORTS,readOnly&lt;/prop&gt;
            &lt;/props&gt;
        &lt;/property&gt;
    &lt;/bean&gt;
    

For question 2

    <bean id="transactionManager"
      class="org.springframework.transaction.jta.JtaTransactionManager">
        <property name="userTransaction">
            <bean class="org.springframework.transaction.jta.JotmFactoryBean"/>
        </property>
    </bean>
+2  A: 

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.

Pascal Thivent