In software engineering, a connection pool is a cache of database
connections maintained so that the connections can be reused when future
requests to the database are required. Connection pools are used to
enhance the performance of executing commands on a database. Opening and
maintaining a database connection for each user, especially requests
made to a dynamic database-driven website application, is costly and
wastes resources. In connection pooling, after a connection is created,
it is placed in the pool and it is used over again so that a new
connection does not have to be established. If all the connections are
being used, a new connection is made and is added to the pool.
Connection pooling also cuts down on the amount of time a user must wait
to establish a connection to the database. (Wikipedia)
In this tutorial, we shall create a connection pool to a MySQL
database in Glassfish web server, then create a simple web application
that makes use of the connection pool.

We now configure the connection pool in Glassfish

Copy the MySQL connector jar file into /lib folder
Start Glassfish server (or restart it if it was already running).
Then we navigate to the admin console. On the home page, click on Create
New JDBC Connection Pool
Give your connection pool a name (I called mine Tutorial). Select the
Resource Type as javax.sql.DataSource and the Database Driver Vendor as
MySQL then click Next
On the next page, scroll down to the section labeled “Additional Properties (199)”
From here, we set the values of the MySQL database connection for
User, Password and URL. Note that you need to change the value for URL
and not Url (case sensitive). For the URL, type in a JDBC connection
value. I set mine as jdbc:mysql://localhost:3306/connpool.
Click finish and you should be able to see the connection you have just created on the JDBC Connection Pools list:

We now test whether this connection is working.

Click on Tutorial and it should open such an interface:
Click on the button labelled “Ping”. If everything is OK, you should get a “Ping Succeeded” message:
Once succeeded, scroll to the bottom of the page and click to select the check-box and enable Non Transactional Connections:
Click Save. That is it for that section.

Next we create a JDBC Resource.

On the left pane, click on Resources → JDBC → JDBC Resources
Click on the “New...” button to add a new resource. In the resulting
form, type in TutorialJDBCResource for JNDI Name. For the Pool Name,
select Tutorial. Leave the rest as default and click OK
You should now see your resource as having been created.

We now create our web application that makes use of the connection pool.

On NetBeans IDE, click File → New Project... Select Java Web → Web Application:
Click Next and give the project the name tutorial. Click Next:
Choose the server as Glassfish Server 3.1 (The one we just created
the connection pool in) and since we are not going to use any
frameworks, click finish.
The project will be created and the start page, index.jsp, opened for us on the IDE.

Edit index.jsp:

Add this code just after the initial coments but before <%@page contentType=...

As you can see, the database in use is connpool, which is the connection
pool that we used. It has 8 active connections, which is the minimum
that Glassfish offered when we were creating the connection pool: