Tomcat Connection Pool vs Apache Commons DBCP

Cage Match!

So it is one of these days where I am minding my own business, reading this page. And I said, let me try and see the difference for myself..

So I have context.xml as seen below:
<?xml version='1.0' encoding='utf-8'?>
<Context>
    <Resource name="jdbc/studentform"
              factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
              closeMethod="close"
              auth="Container"
              type="javax.sql.DataSource"
              maxActive="10"
              maxIdle="10"
              maxWait="1000000"
              driverClassName="org.h2.Driver"
              url="jdbc:h2:tcp://localhost:9092/~/h2dbs/studentform"
              username="sa"
              password=""/>
</Context>

I will also try with:
<?xml version='1.0' encoding='utf-8'?>
<Context>
    <Resource name="jdbc/studentform"
              closeMethod="close"
              auth="Container"
              type="javax.sql.DataSource"
              maxActive="10"
              maxIdle="10"
              maxWait="1000000"
              driverClassName="org.h2.Driver"
              url="jdbc:h2:tcp://localhost:9092/~/h2dbs/studentform"
              username="sa"
              password=""/>
</Context>

Please notice the difference between them: the factory attribute.

Let me show you the rest of the code.. BaseDao.java
package biz.tugay.db;
 
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 7/28/2016
 * Time: 10:48 PM
 */
public class BaseDao {
 
    private static DataSource dataSource;
 
    static {
        // entityManagerFactory = Persistence.createEntityManagerFactory("jdbc/studentform");
        try {
            final InitialContext initialContext = new InitialContext();
            dataSource = (DataSource) initialContext.lookup("java:comp/env/jdbc/studentform");
            System.out.println("Using Data Source: " + dataSource);
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
 
    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}

StudentDao.java
package biz.tugay.db;
 
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
 
/**
 * User: Koray Tugay (koray@tugay.biz)
 * Date: 7/28/2016
 * Time: 10:46 PM
 */
public class StudentDao {
    public List<Student> getAllStudents() {
        List<Student> allStudents = new ArrayList<Student>();
        final Connection connection = BaseDao.getConnection();
        final String sqlQuery = "SELECT * FROM STUDENT";
        try {
            final Statement statement;
            if (connection != null) {
                statement = connection.createStatement();
                final ResultSet resultSet = statement.executeQuery(sqlQuery);
                while (resultSet.next()) {
                    final String fullname = resultSet.getString("FULLNAME");
                    final Student student = new Student();
                    student.setFullname(fullname);
                    allStudents.add(student);
                }
            }
 
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (connection != null) {
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return allStudents;
    }
}

And finally the small / idiot benchmark I use:
public List<Student> allStudents() {
    if (allStudentsRequestScopedCache == null) {
        final StudentDao studentDao = new StudentDao();
        final long start = System.currentTimeMillis();
        for (int i = 0; i < 50000; i++) {
            allStudentsRequestScopedCache = studentDao.getAllStudents();
        }
        final long end = System.currentTimeMillis();
        System.out.println("Took: " + (end - start));
    }
    return allStudentsRequestScopedCache;
}

And lets see the results:
Using Data Source: org.apache.tomcat.jdbc.pool.DataSource
Took: 3472
 
Using Data Source: org.apache.tomcat.dbcp.dbcp2.BasicDataSource
Took: 4808

And the Winner is..

The JDBC Connection Pool: org.apache.tomcat.jdbc.pool!

Here you go org.apache.tomcat.jdbc.pool: