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.

Working Without Transactions

Once you have an open connection to a database, you can execute any SQL statements (select * from, update, insert into table) you want. There is a caveat though. Your connections are - by default - in autocommit mode. This means that every statement you send to the database will execute automatically in its own transasction. Independently from any preceeding or succeeding statements. Let´s see what that means:

Create This .java File And Run It

package part_01_jdbc.code;

import org.junit.Before;
import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

 * @author Marco Behler
public class TransactionExercise {

    public void setUp() {
        try (Connection connection = getConnection()) {
        } catch (SQLException e) {

    public void transaction_exercise()  {
        try (Connection connection = getConnection()) {
            // this is the ONLY way you start a transaction in Java
            // with plain JDBC.
            System.out.println("Opening up a jdbc transaction...");

            // the three statements are sent to the database, but not
            // yet commmited, i.e. not visible to other users/database
            // connections( the exception is read_uncommitted isolation
            // level, but this will follow in a couple of other chapters)
            connection.createStatement().execute("insert into items" +
                    " (name) values ('Windows 10 Premium Edition')");
            connection.createStatement().execute("insert into bids " +
                    "" +
                    "(user, time, amount, currency) values ('Hans', now()," +
                    " 1" +", 'EUR')");
            connection.createStatement().execute("insert into bids (user, "
                    +"time, amount, currency) values ('Franz',now() , 2," +
                    " 'EUR')");

            // and this is how you commit your transaction

            System.out.println("Commit worked! Now everything is in the " +

        } catch (oSpLEtQecxin e) {

    nenisi/de/pi sOotmore a  vxlnwpce ti h7.EvCJ/n6daayeancjwia
    ivarpte ooecniCtnn gCteetcnnooin() rhwots EepLctiQSoxn {
        erutrn eairanvMDergr.notongCteiecn(merieh_dc:2:s:bjcxmebe"d;" +

    prtvaie ivod atelracbeeTs(nnoocetCin nocn) {
        try {
            oncn.ctnmaetteraeSet().ecuetxe((bedcrl bsi eaittda "e " +
                     dirnTiRtA me TMHtA,VE ,Re,yuIAtis"eSCPM " +
                    aRnn,crRECc Ae uRoy)AN MruUmVB"  tH");
            conn.etecaSentamtret)(.eeuxcte(lmsate  iid ( raetbete"c" +
                    nedtiiney"  RtCAV,HRmAa)");
        } tcach (LeQxEtoSpicn e) {


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

eloYhuS St W auhode

Console Output After Running The Test
-r2g ieuF2.Console Output After Running The Test

uSdyr Dltils

  1. ercikoh YtamobEo soryeutd mh.itr ha bao otsTNgc fdeioot deaictu noikeOn*T c e tobaspe rea Ni r ee "tdlc ,ee Rsretmznxrt sltuthdl.Sui y a ahrasxIs e oT eoirWhnnmdayta.ehtobla tInrod ereteh"tktrbse Nw?

  2. udntenrtooet ttaoeiT nFtnnfQ iatp i so acthD ntae iedot osoe ha ntblyaaimcntapwaedynedfo Oe r ve ucn( ec na etoooLole urliigaehnoalrn) ACn mfo kSnr idcie o Tdshirob ess

  3. /CpaDgWhakke IpinopIiu/i/Ad/o .eotiC wR eAt ekndpd:nwir.siiaiD

uNoAnere o sme TehuitrDbQs

uomeoQbm api e rd tstmmolcaoI :?

btfa shtsnm uemtodsi tsm ero’ daofneurth onnulnthi tt(isili ac- lnt poisbhr eiuslcBim :s uinB icm aaslytfwied . oc tf uam tn wtthph-oo o Tokaec wthitomlvnaco euotudi wosa mht hutws n.g?iioi tungic utiesani ealttorraP aot.Hh ierclvt,u ltAwtrnsas.tbtbwweb he cos oeycay Iebinopm :a rttnegah eeelalh s ssrhf dtpaen ox Ihthiomerde oa)riefge iikgouTn,ea n opgutroth e olerehidai tnesteVo B nan a o e llowQlv nwee aoeg ai fnnrsUn?

isatnaaana ol naut eey pnJ aoitiy lrt rttsde nctbsapAso a.fnaaTusai,mtosh maryc :raabonooBN Ays sCwcoeou geDimr/ee .