Cage match!
Checkout the DriverManager version first:
And the DBCP DataSource version:
so, what do you think the difference will be? Here are the results:
Checkout the DriverManager version first:
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; /** * User: Koray Tugay (koray@tugay.biz) * Date: 7/30/2016 * Time: 3:56 PM */ public class DriverManagerTest { public static void main(String[] args) throws Exception { Class.forName("org.h2.Driver"); long maxHeapSize = 0; final long start = System.currentTimeMillis(); for (int i = 0; i < 20000; i++) { final Connection connection = DriverManager.getConnection("jdbc:h2:tcp://localhost:9092/~/h2dbs/studentform", "sa", ""); final Statement statement = connection.createStatement(); final ResultSet resultSet = statement.executeQuery("SELECT * FROM STUDENT"); while (resultSet.next()) { System.out.println(resultSet.getString("FULLNAME")); } connection.close(); long heapSize = Runtime.getRuntime().totalMemory(); if (heapSize > maxHeapSize) { maxHeapSize = heapSize; } } final long end = System.currentTimeMillis(); System.out.println("Took milliseconds: " + (end - start)); System.out.println("Maximum heap size: " + maxHeapSize); } }
And the DBCP DataSource version:
package biz.tugay; import org.apache.commons.dbcp2.BasicDataSource; import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; /** * User: Koray Tugay (koray@tugay.biz) * Date: 7/30/2016 * Time: 4:01 PM */ public class DataSourceTest { public static void main(String[] args) throws Exception { final BasicDataSource ds = new BasicDataSource(); ds.setUrl("jdbc:h2:tcp://localhost:9092/~/h2dbs/studentform"); ds.setUsername("sa"); ds.setPassword(""); ds.setDriverClassName("org.h2.Driver"); long maxHeapSize = 0; final long start = System.currentTimeMillis(); for (int i = 0; i < 20000; i++) { final Connection connection = ds.getConnection(); final Statement statement = connection.createStatement(); final ResultSet resultSet = statement.executeQuery("SELECT * FROM STUDENT"); while (resultSet.next()) { System.out.println(resultSet.getString("FULLNAME")); } connection.close(); long heapSize = Runtime.getRuntime().totalMemory(); if (heapSize > maxHeapSize) { maxHeapSize = heapSize; } } final long end = System.currentTimeMillis(); System.out.println("Took milliseconds: " + (end - start)); System.out.println("Maximum heap size: " + maxHeapSize); } }
so, what do you think the difference will be? Here are the results:
DriverManagerTest Took milliseconds: 13908 Maximum heap size: 229638144 DataSourceTest Took milliseconds: 5174 Maximum heap size: 127401984