JTA

Most applications using ORM tooling have need of a transaction management system.
One of these transaction managers is Atomikos. Atomikos provides several products. One transaction essentials is an opensource variant.
However you cannot get it via a maven repository. You'll have to register for a download link.
Transaction essentials is easy embeddable within a jetty container or even within a spring context.

First here's how to implement transaction essentials within a jetty container configured with maven.

Lets start with the jetty configuration within a maven pom file:
          
            <!-- Run the application using "mvn jetty:run" -->
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.15</version>
                <configuration>
                    <!-- Log to the console. -->
                    <requestLog implementation="org.mortbay.jetty.NCSARequestLog">
                        <!--
                        This doesn't do anything for Jetty, but is a workaround for a
                        Maven bug that prevents the requestLog from being set.
                        -->
                        <append>true</append>
                    </requestLog>
                   
                    <scanIntervalSeconds>0</scanIntervalSeconds>
                    <webAppConfig>
                      <contextPath>/rbudisplay</contextPath>
                      <tempDirectory>${project.build.directory}/work</tempDirectory>
                    </webAppConfig>
                    <jettyConfig>src/etc/jetty/tx-jetty.xml</jettyConfig>
                    <jettyEnvXml>src/etc/jetty/jetty-env.xml</jettyEnvXml>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>javax.transaction</groupId>
                        <artifactId>jta</artifactId>
                        <version>1.1</version>
                    </dependency>
                    <dependency>
                        <groupId>com.atomikos</groupId>
                        <artifactId>atomikos-util</artifactId>
                        <version>3.5.4</version>
                    </dependency>
                    <dependency>
                        <groupId>com.atomikos</groupId>
                        <artifactId>transactions</artifactId>
                        <version>3.5.4</version>
                    </dependency>
                    <dependency>
                        <groupId>com.atomikos</groupId>
                        <artifactId>transactions-api</artifactId>
                        <version>3.5.4</version>
                    </dependency>
                    <dependency>
                        <groupId>com.atomikos</groupId>
                        <artifactId>transactions-jta</artifactId>
                        <version>3.5.4</version>
                    </dependency>
                    <dependency>
                        <groupId>com.atomikos</groupId>
                        <artifactId>transactions-hibernate3</artifactId>
                        <version>3.5.4</version>
                    </dependency>
                    <dependency>
                        <groupId>postgresql</groupId>
                        <artifactId>postgresql</artifactId>
                        <version>8.3-603.jdbc4</version>
                    </dependency>
                    <dependency>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                    <version>1.2.14</version>
                    </dependency>
                </dependencies>
            </plugin>

the jettyConfig tx-jetty contains the configuration for the atomikos usertransactionmanager and will be applied before other setting set in the maven pom:

    <Call class="java.lang.System" name="setProperty">
        <Arg>com.atomikos.icatch.file</Arg>
        <Arg>src/etc/jetty/jta.properties</Arg>
    </Call>

    <!-- Atomikos         -->

    <New id="tx" class="org.mortbay.jetty.plus.naming.Transaction">
        <Arg>
            <New class="com.atomikos.icatch.jta.UserTransactionImp" />
        </Arg>
    </New>

the jetty-env is contains configuration for a specif webapplication in jetty and contains the datasources for the webapp:
  <Set name="configurationClasses">
    <Array type="java.lang.String">
      <Item>org.mortbay.jetty.webapp.WebInfConfiguration</Item>
      <Item>org.mortbay.jetty.plus.webapp.EnvConfiguration</Item>
      <Item>org.mortbay.jetty.annotations.Configuration</Item>
      <Item>org.mortbay.jetty.webapp.JettyWebXmlConfiguration</Item>
      <Item>org.mortbay.jetty.webapp.TagLibConfiguration</Item>
    </Array>
  </Set>
 
  <!-- Add a mapping from name in web.xml to the environment -->
  <New id="map1" class="org.mortbay.jetty.plus.naming.Link">
    <Arg><Ref id='rbudisplay'/></Arg>
    <Arg>jdbc/rbuconverter</Arg> <!-- name in web.xml -->
    <Arg>jdbc/rbu</Arg>  <!-- name in environment -->
  </New>
 
  <New id="rbuconverter" class="org.mortbay.jetty.plus.naming.Resource">
     <Arg><Ref id="rbudisplay"></Ref></Arg>
     <Arg>jdbc/rbu</Arg>
     <Arg>
      <New class="com.atomikos.jdbc.AtomikosDataSourceBean">
       <Set name="minPoolSize">2</Set>
       <Set name="maxPoolSize">50</Set>
       <Set name="xaDataSourceClassName">org.postgresql.xa.PGXADataSource</Set>
       <Set name="UniqueResourceName">rbuconverter</Set>
       <Get name="xaProperties">
         <Call name="setProperty">
            <Arg>databaseName</Arg>
            <Arg>rbuconverter</Arg>
         </Call>
         <Call name="setProperty">
           <Arg>serverName</Arg>
           <Arg>localhost</Arg>
         </Call>
         <Call name="setProperty">
            <Arg>portNumber</Arg>
            <Arg>5432</Arg>
         </Call>
         <Call name="setProperty">
           <Arg>user</Arg>
           <Arg>postgres</Arg>
         </Call>
         <Call name="setProperty">
           <Arg>password</Arg>
           <Arg>BlahBlah</Arg>
         </Call>
       </Get>
      </New>
     </Arg>
   </New>

The first part sets up the jetty plus environment for jndi.
The second part sets up the reference for the jetty-env configuration and to bind the datasource to jndi so the web.xml can make a refernce to the configured datasource.
The third part sets up the datasource itself. Here an XADataSource is configured for PostgreSQL Note that the com.atomikos.jdbc.AtomikosDataSourceBean is the prefered DataSource since Atomikos 3.4.x.

The web.xml needs to have these lines in place for the above configured datasource:
    <resource-ref>
        <res-ref-name>jdbc/rbuconverter</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>

It is also possible to configure Atomikos transaction Essentials completely in a spring context. In this case do not use the jettyConfig and jettyEnvXml in the pom file and omit the resource-ref within the web.xml:
I have a tx-context.xml:
    <bean id="dataSource" class="com.atomikos.jdbc.AtomikosDataSourceBean"
        init-method="init" destroy-method="close">
        <property name="uniqueResourceName" value="rbudatasource" />
        <property name="xaDataSourceClassName" value="org.postgresql.xa.PGXADataSource" />
        <property name="xaProperties">
              <props>
              <prop key="databaseName">rbuconverter</prop>
              <prop key="serverName">localhost</prop>
              <prop key="portNumber">5432</prop>
              <prop key="user">postgres</prop>
              <prop key="password">BlahBlah</prop>
              </props>
        </property>
        <property name="minPoolSize" value="5" />
        <property name="maxPoolSize" value="50"/>
    </bean>

    <bean id="userTransactionService" class="com.atomikos.icatch.config.UserTransactionServiceImp"
        destroy-method="shutdownForce">
        <constructor-arg>
            <!-- IMPORTANT: specify all Atomikos jta properties here -->
            <props>
                <prop key="com.atomikos.icatch.service">
                    com.atomikos.icatch.standalone.UserTransactionServiceFactory
                </prop>
                <prop key="com.atomikos.icatch.output_dir">/tmp</prop>
                <prop key="com.atomikos.icatch.output_dir">/tmp</prop>
            </props>
        </constructor-arg>
    </bean>

    <!--  Construct Atomikos UserTransactionManager, needed to configure Spring  -->
    <bean id="AtomikosTransactionManager" class="com.atomikos.icatch.jta.UserTransactionManager"
        init-method="init" destroy-method="close" depends-on="userTransactionService">
        <!--
            when close is called, should we force transactions to terminate or not?
        -->
        <property name="forceShutdown" value="false" />
        <property name="transactionTimeout" value="300"></property>
    </bean>

    <!--  Also use Atomikos UserTransactionImp, needed to configure Spring   -->
    <bean id="AtomikosUserTransaction" class="com.atomikos.icatch.jta.UserTransactionImp">
        <property name="transactionTimeout">
            <value>300</value>
        </property>
    </bean>

    <!--  Configure the Spring framework to use JTA transactions from Atomikos   -->
    <bean id="transactionManager"
        class="org.springframework.transaction.jta.JtaTransactionManager"
        depends-on="userTransactionService">
        <property name="transactionManager" ref="AtomikosTransactionManager" />
        <property name="userTransaction" ref="AtomikosUserTransaction" />
    </bean>



Comments