Connection Pool in JSF with @Resource Injection

Hey guys in this post, we will discuss setting up connection pooling in JavaServer faces application with full code example

Complete example

We will create this example step by step, follow this tutorial till the end

Read More:

Create Dynamic Web Project

To create a dynamic web project, you can read the following tutorial

Add the JDBC driver Jar file

Download and add the Jar file to the WEB-INF/lib folder of the JSF application.


Create database

Run the following queries in MySQL database


USE jsfwebapp;

CREATE TABLE tbl_employees
    name VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL,
    department VARCHAR(255) NOT NULL

INSERT INTO tbl_employees (name, email, department)
VALUES ("Sharath", "[email protected]", "IT");
INSERT INTO tbl_employees (name, email, department)
VALUES ("Praveen", "[email protected]", "Sales");
INSERT INTO tbl_employees (name, email, department)
VALUES ("Ramesh", "[email protected]", "Marketing");

Define the connection pool

Create META_INF/context.xml file and add the following content


name="jdbc/employee_tracker", this is just a name of the connection pool. Later we will use the same name in the configuration file to reference the datasource.

Make sure to the change the database username and password as per your installation

Configure the reference

Open web.xml file and add the following content

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xsi:schemaLocation="" id="WebApp_ID" version="3.1">
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-name>Faces Servlet</servlet-name>
  	<description>Employee Tracker</description>

Here <res-ref-name> is the same name that we given in creating the connection pool

Create a Servlet

Create file inside the in.bushansirgur.jsf.util package and add the following content

package in.bushansirgur.jsf.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

public class TestConnection extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private DataSource dataSource;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		try {
			connection = dataSource.getConnection();
			statement = connection.createStatement();
			resultSet = statement.executeQuery("select * from tbl_employees");
			while( {
				String name = resultSet.getString("name");
				System.out.println("Name: "+name);
		catch (SQLException ex) {

Run the app

To run the Servlet, right click on the Servlet, select Run As -> choose Run on Server

Aug 02, 2021 4:28:37 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deployment of web application directory [C:\Softwares\apache-tomcat-9.0.50\apache-tomcat-9.0.50\webapps\ROOT] has finished in [23] ms
Aug 02, 2021 4:28:37 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Aug 02, 2021 4:28:37 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in [2992] milliseconds
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
Name: Sharath
Name: Praveen
Name: Ramesh

That’s it for this post. If you like this post, then please share this with your friends and collogues. Also share this post on your social media profiles as well.

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply