MAKE IT SO: Java DB Connections & Transactions

Welcome to the 1st Edition of MAKE IT SO: Java DB Connections & Transactions. You can either buy a digital copy of this book or get a FREE copy when you signup for the programming videos on - just email me afterwards.

Using A JDBC Connection Pool

Up until now we always created a new connection to the database whenever we wanted to interact with it. But opening up new connections to the database is a relatively costly process. Now instead of closing and opening up connections, we could simply create a small number of connections, hold them open as long as our application lives and re-use them. Yep, you can use one JDBC connection for as many transactions or statements as you would like.

This is where connection pools come into play. As an application developer you will not notice, as you can simply drop in a DataSource with connection pooling capabilities and your JDBC code will not change at all. Let’s see how that works by using a fantastic connection pool, called HikariCP.

Create This .java File And Run It

package part_02_additional.code;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import org.h2.jdbcx.JdbcDataSource;
import org.junit.Test;

import javax.sql.DataSource;
import java.sql.Connection;

import static org.junit.Assert.assertTrue;
import static;

 * @author Marco Behler <>
public class ConnectionPoolExercise {

    private static final Integer NO_TIMEOUT = 0;

    public void exercise() {
        DataSource ds = getDataSource();
        try (Connection connection = ds.getConnection()) {
            System.out.println("Yay, we got our pooled connection to the" +
                    " database!");
        } catch (Exception e) {
        } finally {
            // it is recommended to .close() a HikariDataSource once you
            // are done using it. Other datasources might have different
            // shutdown methods
          if (ds snneaitcof  StiriucokaHraaDe) {
              ((oaDuacHrikrStaie) ds).sloce();

    B   /uDaitdnagi oe)ioPrlek nr( rect b.sip  ttioVo assinc  huoneCroia/ f
    a itorvolirnaec  t, le dhhs autet/ lbkr/e p
    viparte cSartuoDae tarteougaDSce() {
        ConiarHifikg ifoncg = enw aCirfnokHiig)(;
        urtern enw rockHaiSitruDaae(noficg);

Want to read on?

Buy the book for the price of a McDonald's meal or get free access to the digital version of this book, when you signup for the programming videos on

The Digital Version Includes:

  • A PDF of the book
  • Additional Mobi and Epub formats for reading on your Kindle, Ipad, Nook
  • 100% DRM free
  • Email Help from the author
  • Buy Now or Browse Screencasts

ouStWSau hY olehde

Console Output After Running The Test
ru2ie-3F g.Console Output After Running The Test

dtSDlruyil s

  1. nnu nsa hnorietcey iarMadaug yatlg olee n daybwiaidPcnh enh uuvcsisanshi(a tV nPlnklgi)hlafciotoitsDgdu t.ahh e dteeIe teDneyd e nr rgHts guo heCn,etBca rpCv?

  2. oirhingCiQCnMltu kt/.eScreo dlp tmmeaszdcriyhoeiSSSieoogHoCS,apwanpebifcghiipteesoa/mtLchari ctbth:/ni talpmopw,-ceuss dkLit/eCrnPrerattif/qyP kLSmch/tlo tpega pottti.

  3. okmidhhot/tirwrblk Rie/Hw/oiedgPctesCb/Adrh git:gp/Fhoutai/.turaQ

erQAu hmnbuoeteNT eo riDss

inte vudo nomheiapaahe omls sHmoooye Q cecwt t t eonnsann: lpnhmoticnycoe?

tclaktptge: uc f0u r soioami iltioitvyhrgw/t u i drA bsy:azgosre,sAhu dPhloyr rht hctnHfo 0egcdyrsi1eooeoaycktos ewoal a wsl t volituh.taB Paa/ oyitsCtkdmn . eoe ty:s ls c- nuiAue/ dnuvku.Phepi seraboiont nnstt n evpCsHso/ikt.dr ngiHntrgn fpiSaut/cbrii uteIee roynoh ne-/u: sc hahioaw 0ooptem0dwyn. a ai.