JBoss 4.x and DataSource Configuration

Robert Broeckelmann

Data SecurityJ2EE / Jakarta EEJBossJava

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

RCBJ0001DataSource jdbc:mysql://localhost:3306/rcbj0001 com.mysql.jdbc.Driver rcbj0001 rcbj0001 5 20 0

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:

<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xmlns=”http://java.sun.com/xml/ns/javaee&#8221; xmlns:web=”http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&#8221; xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&#8221; id=”WebApp_ID” version=”2.5″>
RCBJ0001Web

MySQL DataSource Reference
jdbc/RCBJ0001DataSource
javax.sql.DataSource
Container



JDBCServlet
JDBCServlet
com.rcbj.servlets.JDBCServlet


JDBCServlet
/JDBCServlet

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

java:/jaas/subject1 ** jdbc/RCBJ0001DataSource javax.sql.DataSource java:/RCBJ0001DataSource **

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(“”);
pw.println(“”);
pw.println(“JDBC Servlet”);
pw.println(“”);
pw.println(“”);
String jdbcQueryResponse = jdbcCall();
pw.println(“

” + jdbcQueryResponse + “

”);
pw.println(“”);
pw.println(“”);
} 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.

Read next: