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 both XA and non-XA DataSources, and
using UserTransactions to bracket increments to an integer value
stored in the database.
The demo's web.xml file contains the following environment entries:
|
<web-app>
<!-- Set up the resource-env-ref for the XA DataSource -->
<resource-env-ref>
<description>
XA 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 the resource-env-ref for the non-XA DataSource -->
<resource-env-ref>
<description>
non-XA DB Connection
</description>
<resource-env-ref-name>
jdbc/myNonXADataSource
</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 xadatasources -->
<!-- =============================================================== -->
<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>
<!--If your JDBC driver does connection pooling for you then use the one arg addDataSource() method instead: -->
<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>
<!-- =============================================================== -->
<!-- Set up a DataSourceService for non-XA types of DataSources -->
<!-- Any number of DataSources can be configured by calling -->
<!-- addDataSource(). This example uses the XAPool -->
<!-- StandardDataSource class because the xapool jar is included in -->
<!-- the JettyPlus download, but you can configure any type of -->
<!-- DataSource impl. NOTE also that this service does not provide -->
<!-- automatic support for connection pooling. See instead the -->
<!-- DefaultPoolingDataSourceService (however of course, the -->
<!-- the DataSource impl you use may do it's own pooling. -->
<!-- =============================================================== -->
<Call name="addService">
<Arg>
<New class="org.mortbay.jetty.plus.DataSourceService">
<Set name="Name">DataSourceService</Set>
<Call name="addDataSource">
<Arg>jdbc/myNonXADataSource</Arg> <!-- client lookup jndi name of datasource -->
<!-- set up the datasource -->
<Arg>
<New class="org.enhydra.jdbc.standard.StandardDataSource">
<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>
<!-- a bug in StandardDataSource makes it necessary to explicitly set a Logger -->
<Set name="Logger">
<New class="org.enhydra.jdbc.util.Logger">
<Arg>
<Call class="org.apache.commons.logging.LogFactory" name="getLog">
<Arg>org.enhydra.jdbc.xapool</Arg>
</Call>
</Arg>
</New>
</Set>
<!-- Uncomment to setup isolation level as required -->
<!--
<Set name="TransactionIsolation"><Get class="java.sql.Connection" name="TRANSACTION_SERIALIZABLE"/></Set>
-->
</New>
</Arg>
</Call>
<!-- add other DataSources here -->
</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/resources/log4j.properties
.
Now, you're ready to try the demo.