TomcatPool
En primer lugar, necesitamos un conector para mysql que puede descargarse:
http://dev.mysql.com/downloads/connector/j/5.1.html
Descomprimimos y lo copiamos en common/lib
$ cp mysql-connector-java-5.1.6-bin.jar /usr/local/tomcat/common/lib/
y reiniciamos el servidor Tomcat.
Para el ejemplo de pool de conexiones necesitamos instalar mysql-server-5.0:
$ aptitude install mysql-server-5.0
El usuario en el ejemplo va a ser root y no va a tener contraseña.
Creamos el esquema authority con las siguientes tablas:
$ vim users.sql CREATE TABLE users ( username varchar(15) NOT NULL PRIMARY KEY, password varchar(15) NOT NULL ); CREATE TABLE user_roles ( username varchar(15) NOT NULL, role varchar(15) NOT NULL, PRIMARY KEY (username, role) );
Importamos la tabla:
$ mysql -u root
> create database authority;
> use authority;
> source users.sql
> insert into user_roles (username,role) values ('tomcat', 'tomcat');
> insert into user_roles (username,role) values ('tomcat', 'manager');
> insert into user_roles (username,role) values ('tomcat', 'admin');
Creamos un archivo de contexto en conf/Catalina/localhost/ y definimos el recurso que utiliza el driver de conexión a una base de datos mysql.
$ cd conf/Catalina/localhost/
$ vim test-pool.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/authority"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/authority?autoReconnect=true"
username="root"
password=""
maxActive="100"
maxIdle="30"
maxWait="10000"
/>
</Context>
Creamos el archivo WEB-INF/web.xml dentro de la carpeta webapps/test-pool
$ cd webapps
$ mkdir -p test-pool/WEB-INF
$ vim WEB-INF/web.xml
<web-app xmlns="http ://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http ://java.sun.com/xml/ns/j2ee
http ://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
<description>MySQL Test App</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/authority</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
Y finalmente, una prueba de concepto de un JSP que realiza una select en el pool definido:
$ vim test-pool/test.jsp
<%@ taglib uri="http ://java.sun.com/jsp/jstl/sql" prefix="sql" %>
<%@ taglib uri="http ://java.sun.com/jsp/jstl/core" prefix="c" %>
<sql:query var="rs" dataSource="jdbc/authority">
select username, role from user_roles
</sql:query>
<html>
<head>
<title>DB Test</title>
</head>
<body>
<h2>Usuarios y roles</h2>
<table border="1">
<tr>
<th>Usuario</th><th>Rol</th>
</tr>
<c:forEach var="row" items="${rs.rows}">
<tr>
<td>${row.username}</td><td>${row.role}</td>
</tr>
</c:forEach>
</table>
</html>
Es necesario instalar además la versión 1.1.x de las librerías jstl.jar y standard.jar en el directorio WEB-INF/lib (Jakarta Taglib Standard 1.1)
http://jakarta.apache.org/site/downloads/downloads_taglibs-standard.cgi