Sophie

Sophie

distrib > Mandriva > 2010.0 > x86_64 > by-pkgid > c27466c2a3fa3cf6008c3a485d00ce04 > files > 152

jetty5-manual-5.1.15-1.5.2mdv2010.0.noarch.rpm

<P>
<H3>Transactions and XADataSources</H3>
Thanks to the integration of the <A HREF="http://jotm.objectweb.org">JOTM</A> and <A
HREF="http://xapool.experlog.com/">XAPool</A> packages
from <A HREF="http://www.objectweb.org">ObjectWeb</A>,
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 <code>&lt;resource-ref&gt;</code>s that refer to DataSources that
can participate in distributed transactions.

<ul>
 <li><B>Step 1:</B> 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):
<code><pre>
  &lt;Call name="addService"&gt;
    &lt;Arg&gt;
      &lt;New class="org.mortbay.jetty.plus.JotmService"&gt;
        &lt;Set name="Name"&gt;TransactionMgr&lt;/Set&gt;
        <font color="blue"> &lt;!-- set up a pooled DataSource --&gt;</font>
         &lt;Call name="addDataSource"&gt;
             &lt;Arg&gt;jdbc/myDB&lt;/Arg&gt;   <font color="blue"&lt;!-- client lookup jndi name of datasource  --&gt;</font>
             <font color="blue">&lt;!-- set up the datasource --&gt;</font>
             &lt;Arg&gt;
               <font color="blue">
               &lt;!-- Uncomment one of the following types of XADataSource        --&gt;
               &lt;!-- according to your type of database:                         --&gt;
               &lt;!-- New class="org.enhydra.jdbc.sybase.SybaseXADataSource"      --&gt;
               &lt;!-- New class="org.enhydra.jdbc.informix.InformixXADataSource"  --&gt;
               &lt;!-- New class="org.enhydra.jdbc.oracle.OracleXADataSource"      --&gt;
               </font>
               &lt;New class="org.enhydra.jdbc.standard.StandardXADataSource"&gt;
                 &lt;Set name="DriverName"&gt;org.hsqldb.jdbcDriver&lt;/Set&gt;
                 &lt;Set name="Url"&gt;jdbc:hsqldb:extra/etc/tmtest&lt;/Set&gt;
                 &lt;Set name="User"&gt;sa&lt;/Set&gt;
                 &lt;Set name="Password"&gt;&lt;/Set&gt;
                 <font color="blue">
                 &lt;!-- Uncomment to setup isolation level as required            --&gt;
                 </font>
                 &lt;!--
                 &lt;Set name="TransactionIsolation"&gt;&lt;Get class="java.sql.Connection" name="TRANSACTION_SERIALIZABLE"/&gt;&lt;/Set&gt; 
                 --&gt;
               &lt;/New&gt;
             &lt;/Arg&gt;
             <font color="blue">&lt;!-- set up a pool for the datasource --&gt;</font>
             &lt;Arg&gt;
               &lt;New class="org.enhydra.jdbc.pool.StandardXAPoolDataSource"&gt;
                 &lt;Arg type="Integer"&gt;4&lt;/Arg&gt;          <font color="blue">&lt;!-- initial size of pool --&gt;</font>     
                 &lt;Set name="MinSize"&gt;4&lt;/Set&gt;                
                 &lt;Set name="MaxSize"&gt;15&lt;/Set&gt; 
                 <font color="blue">&lt;!-- Uncomment to setup other pool params as required          --&gt;</font>
                 &lt;!--
                 &lt;Set name="SleepTime"&gt;10&lt;/Set&gt;
                 &lt;Set name="LifeTime"&gt;10&lt;/Set&gt;
                 &lt;Set name="DeadLockMaxWait"&gt;10&lt;/Set&gt;
                 &lt;Set name="DeadLockRetryWait"&gt;10&lt;/Set&gt;
                 &lt;Set name="LoginTimeout"&gt;10&lt;/Set&gt;
                 &lt;Set name="Debug" type="boolean"&gt;true&lt;/Set&gt;
                 --&gt;
               &lt;/New&gt;
             &lt;/Arg&gt;               
        &lt;/Call&gt;

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

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

 <li><B>Step 2:</B> reference the DataSources as <code>reference</code>s in your <code>web.xml</code>
 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.
<code><pre>
  &lt;resource-env-ref&gt;
    &lt;description&gt;
      DB Connection
    &lt;/description&gt;
    &lt;resource-env-ref-name&gt;
      jdbc/myDB
    &lt;/resource-env-ref-name&gt;
    &lt;resource-env-ref-type&gt;
      javax.sql.DataSource
    &lt;/resource-env-ref-type&gt;
  &lt;/resource-env-ref&gt;
</pre></code>
 </li>

 <li><B>Step 3:</B> code your application to access the DataSource (and any other resources you wish) in the context of a UserTransaction:
<code><pre>
    try
    {
      UserTransaction usertransaction =
                      (UserTransaction)context.lookup("java:comp/UserTransaction");
      <font color="silver">//do some stuff</font>
      usertransaction.begin();
      javax.sql.DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/myDB");
      java.sq.Connection con = ds.getConnection();
      <font color="silver">//access some resources and make changes</font>
      usertransaction.commit();
      con.close();
    }
    catch (Exception e)
    {
      usertransaction.rollback();
    }
</code></pre>
</li>
 </ul>