<P> <H1>Non-XA DataSources</H1> Whilst the <code>org.mortbay.jetty.plus.JotmService</code> provides support for JTA and DataSources that can participate in distributed transactions, JettyPlus also supports non-XA DataSources. The <code>org.mortbay.jetty.plus.AbstractDataSourceService</code> provides the basis for pluggable DataSource and connection pooling implementations. A default implementation is provided that allows you to use any <code>javax.sql.DataSource</code> for non-pooled connections and that uses XAPool internally to provide the pool for pooled connections. By subclassing <code>AbstractDataSourceService</code>, you may <a href="#alternate">add other types of connection pooling</a> implementations to JettyPlus. <P> The general method of configuring the <code>org.mortbay.jetty.plus.DefaultDataSourceService</code> in your JettyPlus xml configuration file is as follows: <code><pre> <Call name="addService"> <Arg> <New class="org.mortbay.jetty.plus.DefaultDataSourceService"> <Set name="Name">DataSourceService</Set> <font color="blue"><!-- name of the service --></font> <font color="blue"><!-- repeat this section to add more DataSource definitions --></font> <Call name="addDataSource"> <Arg>jdbc/xyz</Arg><font color="blue"> <!-- name for app to lookup relative to java:comp/env --></font> <New class=......><font color="blue"> <!-- class for javax.sql.DataSource implementation --></font> <font color="blue"><!-- configure the particular DataSource implementation --></font> </New> </Call> <font color="blue"><!-- end of DataSource definition --></font> <font color="blue"> <!-- repeat this section to add more pooled datasources --></font> <Call name="addConnectionPoolDataSource"> <Arg>jdbc/postgres</Arg><font color="blue"><!-- name for app to lookup relative to java:comp/env --></font> <New class=......><font color="blue"> <!-- class for javax.sql.ConnectionPoolDataSource implementation --></font> <font color="blue"><!-- configure the particular javax.sql.ConnectionPoolDataSource implementation --></font> </New> </Call> <font color="blue"><!-- end of ConnectionPolDataSource definition --></font> </New> </Arg> </Call> </pre></code> </ul> <P> As this mechanism is extremely flexible, let's now consider some of the different ways in which you can configure DataSource resources: </P> <H2>Configuration Scenarios</H2> <H3>1 Using a vendor-supplied javax.sql.DataSource that does internal connection pooling, or pooling not required</H3> The following example configures a Postgres DataSource that does its own connection pooling. You can configure any javax.sql.DataSource implementation. <code><pre> <Call name="addDataSource"> <Arg>jdbc/mydatabase</Arg> <Arg> <New class="org.postgresql.jdbc3.Jdbc3PoolingDataSource"> <Set name="DataSourceName">Database on localhost</Set> <Set name="ServerName">localhost</Set> <Set name="DatabaseName">database</Set> <Set name="PortNumber">5432</Set> <Set name="User">postgres</Set> <Set name="Password">password</Set> <Set name="MaxConnections">50</Set> <Set name="InitialConnections">5</Set> </New> </Arg> </Call> </pre></code> <H3>2 No vendor-supplied DataSource available, pooling not required</H3> If you only have a JDBC driver class, you can use the XAPool <code>org.enhydra.jdbc.standard.StandardDataSource</code> class to provide a non-pooling javax.sql.DataSource. Eg: <code><pre> <Call name="addDataSource"> <Arg>jdbc/hsqldb</Arg> <font color="blue"><!-- Configure the DataSource impl --></font> <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> <font color="blue"><!-- bug work around only for StandardDataSource: --></font> <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> </New> </Arg> </Call> </pre></code> <H3>3 Using JettyPlus connection pooling where vendor supports JDBC2 javax.sql.ConnectionPoolDataSource</H3> If your JDBC provider does not do connection pooling internally, you can use JettyPlus's default connection pooling implementation (or <a href="#alternate">write your own replacement connection pool</a>). If your JDBC vendor implements the JDBC2 specification, ie implements the <code>javax.sql.ConnectionPoolDataSource</code> interface, you can configure connection pooling by using the <code>addConnectionPoolDataSource()</code> method. The following example uses Postgres: <code><pre> <Call name="addConnectionPoolDataSource"> <Arg>jdbc/mydatabase</Arg> <Arg> <New class="org.postgresql.jdbc3.Jdbc3ConnectionPool"> <Set name="DataSourceName">Database on localhost</Set> <Set name="ServerName">localhost</Set> <Set name="DatabaseName">database</Set> <Set name="PortNumber">5432</Set> <Set name="User">postgres</Set> <Set name="Password">password</Set> </New> </Arg> <font color="blue"><!-- configure the pool: --></font> <Set name="user">postgress</Set> <Set name="password">password</Set> <Set name="minSize">1</Set> <Set name="maxSize">5</Set> </Call> </pre></code> <H3>4 Using JettyPlus connection pooling without JDBC2 support</H3> If your JDBC provider does <b>not</b> support the JDBC2 specification, you can use the <code>org.enhydra.jdbc.standard.StandardConnectionPoolDataSource</code> class to create a DataSource that pools connections: <code><pre> <Call name="addConnectionPoolDataSource"> <Arg>jdbc/myPooledDataSource</Arg> <Arg> <New class="org.enhydra.jdbc.standard.StandardConnectionPoolDataSource"> <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> <!-- workaround for XAPool bug: --> <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> </New> </Arg> <font color="blue"><!-- configure the pool: --></font> <Set name="user">sa</Set> <Set name="password"></Set> <Set name="minSize">1</Set> <Set name="maxSize">5</Set> </Call> </pre></code> <a name="alternate"><H1>Plugging in an alternate DataSourceService</H1></a> If you would like to use a different connection pooling mechanism other than the default supplied with the JettyPlus distribution, you can code your own DataSourceService. <P> You will need to subclass the <code>org.mortbay.jetty.plus.AbstractDataSourceService</code> and provide an implementation for the following method: <P> <code><pre> public DataSource createPooledDataSource (String jndiName, javax.sql.ConnectionPoolDataSource cpds) throws Exception; </pre></code> <P> Notice that the method returns a <code>javax.sql.DataSource</code> object. This is the object will be bound into JNDI for application code to lookup in <code>java:comp/env</code>, and must work with your pooling mechanism. The abstract base class ensures that the DataSource impl returned by this method will be available for linking to webapplication's <code><resource-ref></code> and <code><resource-env-ref></code> elements. </P> <P>Of course, you are also free to add and expose other methods in order to configure your pooling mechanism.</P> <P>You must then make your class available on to the JettyPlus classpath. Perhaps the easiest way to do this is to create a jar of your implemenation class, and place it in <code>$JETTY_HOME/extra/lib</code>. Put any auxiliary jars into <code>$JETTY_HOME/extra/ext</code>. </P> <P>Now all you have to do is call your service implementation in your JettyPlus xml configuration file, and you're all done!</P>