Install and Enable JDBC Data Sources
JDBC Data Sources 0. Introduction Many web applications need to access a database via a JDBC driver, to support the functionality required by that application. The J2EE Platform Specification requires J2EE Application Servers to make available a DataSource implementation (that is, a connection pool for JDBC connections) for this purpose. JBoss Web offers exactly the same support, so that database-based applications you develop on JBoss Web using this service will run unchanged on any J2EE server. For information about JDBC, you should consult the following: http://java.sun.com/products/jdbc/ - Home page for information about Java Database Connectivity. http://java.sun.com/j2se/1.3/docs/guide/jdbc/spec2/jdbc2.1.frame.html - The JDBC 2.1 API Specification. http://java.sun.com/products/jdbc/jdbc20.stdext.pdf - The JDBC 2.0 Standard Extension API (including the javax.sql.DataSource API). This package is now known as the "JDBC Optional Package". http://java.sun.com/j2ee/download.html - The J2EE Platform Specification (covers the JDBC facilities that all J2EE platforms must provide to applications). NOTE - The default data source support in JBoss Web is based on the DBCP connection pool from the Jakarta Commons subproject. However, it is possible to use any other connection pool that implements javax.sql.DataSource, by writing your own custom resource factory, as described below. 1. Install Your JDBC Driver Use of the JDBC Data Sources JNDI Resource Factory requires that you make an appropriate JDBC driver available to both JBoss Web internal classes and to your web application. This is most easily accomplished by installing the driver's JAR file(s) into the $CATALINA_HOME/lib directory, which makes the driver available both to the resource factory and to your application. 2. Declare Your Resource Requirements Next, modify the web application deployment descriptor (/WEB-INF/web.xml) to declare the JNDI name under which you will look up preconfigured data source. By convention, all such names should resolve to the jdbc subcontext (relative to the standard java:comp/env naming context that is the root of all provided resource factories. A typical web.xml entry might look like this: Resource reference to a factory for java.sql.Connection instances that may be used for talking to a particular database that is configured in the server.xml file. jdbc/EmployeeDB javax.sql.DataSource Container WARNING - Be sure you respect the element ordering that is required by the DTD for web application deployment descriptors! See the Servlet Specification for details. 3. Code Your Application's Use Of This Resource A typical use of this resource reference might look like this: Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); DataSource ds = (DataSource) envCtx.lookup("jdbc/EmployeeDB"); Connection conn = ds.getConnection(); ... use this connection to access the database ... conn.close(); Note that the application uses the same resource reference name that was declared in the web application deployment descriptor. This is matched up against the resource factory that is configured in $CATALINA_HOME/conf/server.xml, as described below. 4. Configure JBoss Web's Resource Factory To configure JBoss Web's resource factory, add an element like this to the /META-INF/context.xml file in the web application. ... ... Note that the resource name (here, jdbc/EmployeeDB) must match the value specified in the web application deployment descriptor. This example assumes that you are using the HypersonicSQL database JDBC driver. Customize the driverClassName and driverName parameters to match your actual database's JDBC driver and connection URL. The configuration properties for JBoss Web's standard data source resource factory (org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory) are as follows: driverClassName - Fully qualified Java class name of the JDBC driver to be used. maxActive - The maximum number of active instances that can be allocated from this pool at the same time. maxIdle - The maximum number of connections that can sit idle in this pool at the same time. maxWait - The maximum number of milliseconds that the pool will wait (when there are no available connections) for a connection to be returned before throwing an exception. password - Database password to be passed to our JDBC driver. url - Connection URL to be passed to our JDBC driver. (For backwards compatibility, the property driverName is also recognized.) user - Database username to be passed to our JDBC driver. validationQuery - SQL query that can be used by the pool to validate connections before they are returned to the application. If specified, this query MUST be an SQL SELECT statement that returns at least one row. For more details, please refer to the commons-dbcp documentation.