# JettyPlus Transaction, Datasource and JNDI Test

Skip to the demo

## Background to the demo

JettyPlus integrates a number of valuable J2EE features with the core Jetty server: UserTransactions, Datasources and JNDI lookups. This demonstration illustrates programmatic JNDI lookups of `env` and `resource-env` values specified in the `web.xml` descriptor, connecting to a a DataSource, and using UserTransactions to bracket increments to an integer value stored in the DataSource.

The demo's web.xml file contains the following environment entries:

    <web-app>

      <!-- Set up the resource-env-ref for the DataSource -->

      <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>

      <!-- Set up a couple of env-entries  -->

      <env-entry>
        <env-entry-name>
           select
        </env-entry-name>
        <env-entry-value>
           select id, foo from testdata
        </env-entry-value>
        <env-entry-type>
           java.lang.String
        </env-entry-type>
      </env-entry>
      <env-entry>
        <env-entry-name>
           update
        </env-entry-name>
        <env-entry-value>
           update testdata set foo=? where id=1
        </env-entry-value>
        <env-entry-type>
           java.lang.String
        </env-entry-type>
      </env-entry>
    </web-app>

Application web.xml descriptor

The relevant sections of the jetty xml configuration file are:

    <!-- =============================================================== -->
    <!-- Configure the JettyPlus Server                                  -->
    <!-- =============================================================== -->

    <Configure class="org.mortbay.jetty.plus.Server">

      <!-- =============================================================== -->
      <!-- Configure a Log4J log sink                                      -->
      <!-- =============================================================== -->

      <Call name="instance" class="org.mortbay.util.Log">
        <Call name="disableLog"/>
        <Call name="add">
          <Arg>
            <New class="org.mortbay.util.log4j.Log4jSink">
              <Call name="start"/>
            </New>
          </Arg>
        </Call>
      </Call>

      <!-- =============================================================== -->
      <!-- Add a transaction manager and datasources                       -->
      <!-- =============================================================== -->

      <Call name="addService">
        <Arg>
          <New class="org.mortbay.jetty.plus.JotmService">
            <Set name="Name">TransactionMgr</Set>
             <!-- set up a pooled DataSource -->
             <Call name="addPooledDataSource">
                 <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>
         </New>
       </Arg>
      </Call>

    </Configure>

jettyplus.xml configuration file

Notice particularly that it is the JettyPlus server class org.mortbay.jetty.plus.Server being configured, not the normal Jetty server (org.mortbay.jetty.Server).

Note also that a log4J log sink must be configured. You must specify the location of the log4j configuration file on the run line. For this demo, this is located in the `org.mortbay.jetty.plus.resource.jar` file, the source of which is in `$jetty.home/extra/plus/resources/log4j.xml`.

Now, you're ready to try the demo.