### Transactions and XADataSources

Thanks to the integration of the JOTM and XAPool packages from ObjectWeb, it is now possible to easily incorporate distributed transactional semantics in your servlets and JSPs. JOTM provides a JTA transaction manager, and the XAPool integration provides the ability to configure `<resource-ref>`s that refer to DataSources that can participate in distributed transactions.

- Step 1: Set up a transaction manager and related XADataSources in your JettyPlus xml configuration file. By default, JettyPlus uses the JOTM transaction manager and XAPool XADataSources, although you can develop your own service and plug it in instead. The XAPool integration offers you the choice of using pooled connections or non-pooled (although the JDBC driver itself may provide connection pooling internally). To configure the default transaction manager service (org.mortbay.jetty.plus.JotmService): `

        <Call name="addService">
          <Arg>
            <New class="org.mortbay.jetty.plus.JotmService">
              <Set name="Name">TransactionMgr</Set>
               <!-- set up a pooled DataSource -->
               <Call name="addDataSource">
                   <Arg>jdbc/myDB</Arg>
                   <!-- set up the datasource -->
                   <Arg>

                     <!-- Uncomment one of the following types of XADataSource        -->
                     <!-- according to your type of database:                         -->
                     <!-- New class="org.enhydra.jdbc.sybase.SybaseXADataSource"      -->
                     <!-- New class="org.enhydra.jdbc.informix.InformixXADataSource"  -->
                     <!-- New class="org.enhydra.jdbc.oracle.OracleXADataSource"      -->

                     <New class="org.enhydra.jdbc.standard.StandardXADataSource">
                       <Set name="DriverName">org.hsqldb.jdbcDriver</Set>
                       <Set name="Url">jdbc:hsqldb:extra/etc/tmtest</Set>
                       <Set name="User">sa</Set>
                       <Set name="Password"></Set>

                       <!-- Uncomment to setup isolation level as required            -->

                       <!--
                       <Set name="TransactionIsolation"><Get class="java.sql.Connection" name="TRANSACTION_SERIALIZABLE"/></Set>
                       -->
                     </New>
                   </Arg>
                   <!-- set up a pool for the datasource -->
                   <Arg>
                     <New class="org.enhydra.jdbc.pool.StandardXAPoolDataSource">
                       <Arg type="Integer">4</Arg>          <!-- initial size of pool -->
                       <Set name="MinSize">4</Set>
                       <Set name="MaxSize">15</Set>
                       <!-- Uncomment to setup other pool params as required          -->
                       <!--
                       <Set name="SleepTime">10</Set>
                       <Set name="LifeTime">10</Set>
                       <Set name="DeadLockMaxWait">10</Set>
                       <Set name="DeadLockRetryWait">10</Set>
                       <Set name="LoginTimeout">10</Set>
                       <Set name="Debug" type="boolean">true</Set>
                       -->
                     </New>
                   </Arg>
              </Call>

              Note: if your JDBC driver does connection pooling for you, then instead of
              configuring both a StandardXADataSource and a StandardXAPoolDataSource,
              you can just configure the StandardXADataSource instead:

              <!-- set up a DataSource where the driver does the pooling                 -->
              <Call name="addDataSource">
                   <Arg>jdbc/otherDB</Arg>    <!-- client lookup jndi name of datasource  -->
                   <Arg>
                     <New class="org.enhydra.jdbc.standard.StandardXADataSource">
                       <Set name="DriverName">com.mysql.jdbc.Driver</Set>
                       <Set name="Url">jdbc:mysql://localhost:3306/oln </Set>
                       <Set name="User"></Set>
                       <Set name="Password"></Set>
                     </New>
                   </Arg>
              </Call>

           </New>
         </Arg>
        </Call>

  `
- Step 2: reference the DataSources as `reference`s in your `web.xml` descriptor. Note that your web.xml and code should always reference a javax.sql.DataSource, even though you have configured an XADataSource in your JettyPlus descriptor. This is because the JettyPlus infrastructure abstracts away the interface differences for you. If the underlying DataSource is XA capable, then calls to it will participate in an established UserTransaction. If the DataSource is not XA capable (see Non-XA DataSources below), then it cannot participate in distributed transactions. An example of a web.xml reference to the DataSource configured in Step 1 is. `

        <resource-env-ref>
          <description>
            DB Connection
          </description>
          <resource-env-ref-name>
            jdbc/myDB
          </resource-env-ref-name>
          <resource-env-ref-type>
            javax.sql.DataSource
          </resource-env-ref-type>
        </resource-env-ref>

  `
- Step 3: code your application to access the DataSource (and any other resources you wish) in the context of a UserTransaction: `

          try
          {
            UserTransaction usertransaction =
                            (UserTransaction)context.lookup("java:comp/UserTransaction");
            //do some stuff
            usertransaction.begin();
            javax.sql.DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/myDB");
            java.sq.Connection con = ds.getConnection();
            //access some resources and make changes
            usertransaction.commit();
            con.close();
          }
          catch (Exception e)
          {
            usertransaction.rollback();
          }

  `