JBoss 4.x and DataSource Configuration

On JBoss 4.x, a MySQL datasource looks something like:

<?xml version=”1.0″ encoding=”UTF-8″?>
<datasources>
<local-tx-datasource>
<!– The jndi name of the DataSource, it is prefixed with java:/ –>
<!– Datasources are not available outside the virtual machine –>
<jndi-name>RCBJ0001DataSource</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/rcbj0001</connection-url>
<!– The driver class –>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<!– The login and password –>
<user-name>rcbj0001</user-name>
<password>rcbj0001</password>
<min-pool-size>5</min-pool-size>
<!– The maximum connections in a pool/sub-pool –>
<max-pool-size>20</max-pool-size>
<!– The time before an unused connection is destroyed –>
<!– NOTE: This is the check period. It will be destroyed somewhere between 1x and 2x this timeout after last use –>
<!– TEMPORARY FIX! – Disable idle connection removal, HSQLDB has a problem with not reaping threads on closed connections –>
<idle-timeout-minutes>0</idle-timeout-minutes>
</local-tx-datasource>
</datasources>

For production environments, the password should use some type of encryption.

To actually use this JDBC datasource in a web app, the highlighted stanza below must be present in web.xml:

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns=”http://java.sun.com/xml/ns/javaee” xmlns:web=”http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd” id=”WebApp_ID” version=”2.5″>
<display-name>RCBJ0001Web</display-name>
<resource-ref>
<description>MySQL DataSource Reference</description>
<res-ref-name>jdbc/RCBJ0001DataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

<servlet>
<description></description>
<display-name>JDBCServlet</display-name>
<servlet-name>JDBCServlet</servlet-name>
<servlet-class>com.rcbj.servlets.JDBCServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JDBCServlet</servlet-name>
<url-pattern>/JDBCServlet</url-pattern>
</servlet-mapping>
</web-app>

The jboss-web.xml file must have the following:

<jboss-web>
<security-domain>java:/jaas/subject1</security-domain>

<resource-ref>
<res-ref-name>jdbc/RCBJ0001DataSource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<jndi-name>java:/RCBJ0001DataSource</jndi-name>
</resource-ref>

</jboss-web>

The following servlet demonstrates using the Datsource via a JNDI lookup:

package com.rcbj.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.naming.InitialContext;
import javax.naming.Context;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.ResultSet;

/**
* Servlet implementation class JDBCServlet
*/
public class JDBCServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

/**
* @see HttpServlet#HttpServlet()
*/
public JDBCServlet() {
super();
// TODO Auto-generated constructor stub
}

/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
PrintWriter pw = response.getWriter();
pw.println(“<HTML>”);
pw.println(“<HEAD>”);
pw.println(“<TITLE>JDBC Servlet</TITLE>”);
pw.println(“</HEAD>”);
pw.println(“<BODY>”);
String jdbcQueryResponse = jdbcCall();
pw.println(“<P>” + jdbcQueryResponse + “</P>”);
pw.println(“</BODY>”);
pw.println(“</HTML>”);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}

String jdbcCall()
{
ResultSet rs = null;
PreparedStatement pstmt = null;
Connection con = null;
String result = null;
try
{
ResourceBundle rb = ResourceBundle.getBundle(“com.rcbj.servlets.JDBCServlet”);
Context initCtx = new InitialContext();
Context ctx = (Context)initCtx.lookup(“java:/comp/env”);
DataSource ds = (DataSource)ctx.lookup(rb.getString(“dsJndiName”));
con = ds.getConnection();
con.setAutoCommit(false);
pstmt = con.prepareStatement(rb.getString(“sqlStatement”));
//If a variable, ‘?’, had to be set, this is how you would do it.
//pstmt.setString(1, “tester”);
//Make SQL Call.
rs = pstmt.executeQuery();
//read & return response
rs.next();
result = new String(rs.getString(1) + ” ” + rs.getString(2) + ” ” + rs.getString(3));
return result;
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
try
{
rs.close();
pstmt.close();
con.close();
return result;
}
catch (Exception e)
{
e.printStackTrace();
}
}
return null;
}
}

This code uses the following properties file:

#JNDI Name of resource reference…
dsJndiName=jdbc/RCBJ0001DataSource
#SQL Statement
sqlStatement=SELECT NOW(), CURDATE(), CURTIME()

It is unlikely that a modern web app will use Datasources directly like this.