Another Cage Match: DriverManager vs MysqlDataSource vs BasicDataSource!

Who knew MySQL Java Connector came with a DataSource?!

My directory layout is as follows..

HelloWorld.java
package biz.tugay.bambum;
 
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
import org.apache.commons.dbcp2.BasicDataSource;
 
import java.sql.*;
 
/* User: koray@tugay.biz Date: 2017/05/20 */
public class HelloWorld {
 
    private static final String DATB_URL = "jdbc:mysql://localhost:3306/test";
    private static final String USERNAME = "test";
    private static final String PASSWORD = "test";
 
    private static final MysqlDataSource mysqlDataSource = new MysqlDataSource();
    private static final BasicDataSource basicDataSource = new BasicDataSource();
 
    static {
        mysqlDataSource.setURL(DATB_URL);
        mysqlDataSource.setUser(USERNAME);
        mysqlDataSource.setPassword(PASSWORD);
    }
 
    static {
        basicDataSource.setUrl(DATB_URL);
        basicDataSource.setUsername(USERNAME);
        basicDataSource.setPassword(PASSWORD);
    }
 
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        // Warm up..
        for (int i = 0; i < 1000; i++) {
            useDriverManager();
            useMysqlDataSource();
            useBasicDataSource();
        }
 
        final long driverManagerStart = System.currentTimeMillis();
        for (int i = 0; i < 2000; i++) {
            useDriverManager();
        }
        final long driverManagerEnd = System.currentTimeMillis();
 
        final long dataSourceStart = System.currentTimeMillis();
        for (int i = 0; i < 2000; i++) {
            useMysqlDataSource();
        }
        final long dataSourceEnd = System.currentTimeMillis();
 
        final long basicDataSourceStart = System.currentTimeMillis();
        for (int i = 0; i < 2000; i++) {
            useBasicDataSource();
        }
        final long basicDataSourceEnd = System.currentTimeMillis();
 
 
        System.out.println("DriverManager   : " + (driverManagerEnd - driverManagerStart));
        System.out.println("MysqlDataSource : " + (dataSourceEnd - dataSourceStart));
        System.out.println("BasicDataSource : " + (basicDataSourceEnd - basicDataSourceStart));
    }
 
    private static void useBasicDataSource() throws SQLException {
        final Connection connection = basicDataSource.getConnection();
        queryUsingConnection(connection);
    }
 
    private static void useMysqlDataSource() throws SQLException {
        final Connection connection = mysqlDataSource.getConnection();
        queryUsingConnection(connection);
    }
 
    private static void useDriverManager() throws SQLException {
        final Connection connection = DriverManager.getConnection(DATB_URL, USERNAME, PASSWORD);
        queryUsingConnection(connection);
    }
 
    private static int queryUsingConnection(final Connection connection) throws SQLException {
        final String query = "SELECT COUNT(*) FROM app_user";
        final PreparedStatement preparedStatement = connection.prepareStatement(query);
        final ResultSet resultSet = preparedStatement.executeQuery();
 
        resultSet.next();
        final int anInt = resultSet.getInt(1);
 
        resultSet.close();
        preparedStatement.close();
        connection.close();
 
        return anInt;
    }
}

Output
DriverManager   : 2777
MysqlDataSource : 2931
BasicDataSource : 1048

Conclusions

Driver Manager is faster than MysqlDataSource? Bravo Driver Manager!