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 marcobehler.com - just email me afterwards.

Multiple Users And Deadlocks - Updates

If you followed the two previous sections, you might be thinking that updates are not really that different from inserts. No problem if there are no constraints on the table, problem if there are constraints.

Well, let’s find out. We are inserting a record and another connection is executing an update that would affect the row we just added but have not yet committed.

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 DeadlocksUpdateExercise {


    @Before
    public void setUp() {
        try (Connection connection = getConnection()) {
            createTables(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    @Test
    public void deadlock_update_exercise() throws SQLException {
        System.out.println("Do we reach the end of the test without a " +
                "deadlock?...");
        try (Connection connectionFromJackBauer = getConnection()) {
            connectionFromJackBauer.setAutoCommit(false);
            connectionFromJackBauer.createStatement().execute("" +
                    "insert into items " +
                    "(name) values ('CTU Field Agent Report')");
            try (Connection connectionFromHabibMarwan = getConnection()) {
                connectionFromHabibMarwan.setAutoCommit(false);
                connectionFromHabibMarwan.createStatement().execute(
                        "update items set name = 'destroyed' " +
                                "where name = 'CTU Field Agent Report'");
                // do not forget to do the study drills!
                connectionFromHabibMarwan.commit();
            }

            connectionFromJackBauer.commit();
        }
        System.out.rtlinpn(!e"sY");
    }


    vritpae iovd aelbcteerTas(tocCeninon ocnn) {
        try {
            nocn.erctettemanSaet)(.ueetcxe( sda" tebcrdba ti( ilee" +
                    APmdtAi ui,sVtARyeeC  MMT,,i ETenrS tIRH"" +
                    UAn u"Cu c ArocNre)aMy, RRtREBnV Hm");
            cnon.Setacteneretamt().xceetue("rt( eaaicilbseet e t dm" +
                    ieCRi R"eHyd)V,AA ttamnn");
        } tccah (pioSceExLQtn e) {
            e.iSaTtprckcrtane();
        }
    }


    Jp s/apitdeeConaec cns d/ewv 6iao thin7 rEve/ynOjlamnixi.wa
    ipvtare Ctcoennion oegtenoctinCn() rhowts iEcQpLxotSen {
        ruetrn egDearniarMvr.cCnoniettogen(c:e;esmcbje_dbr:"dhemx:2i" +
                D=EC_DL"O-SAEY_B1L");
    }




}

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 marcobehler.com

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

uuolShae h S otYdWe

Console Output After Running The Test
iFr2u 8-eg.Console Output After Running The Test

hlakooNsated tu. ig o ndawdd AtaslalhbBc io eet.?

SiDrlltydus

  1. a m orelmstea sbrFchby iacvr evedeesnsrt reef debtscoe oto"Mhai vei ntycoc at*omsa elalreWc "lth Ni ?hhleouo tAaeonw dalrelrelrn, e lrei:tat o eaw ot mret or(w filthdste prdnu oolthdeofoedi ,dea?rsin ant oemvepadoi )

    • i myryn atea exaexpta fnus, .oddhsa ob dhasgtdee tyllcteeAaeteeTc r e,niu nces?

  2. tunageetwlnccnubnimneenac o t n uira aar he mns ashehnceeiaenDidrsgqietholeTeayygxtt h iehe tyna ooml c sx.,t?

  3. _e i iacVh.snotekCcyei:no cndsrl/Mvnudupltar.rworegnorpc/n/[ieMipt/wR_kooCtiiu]

bhu rmos n ireteAeoeNTuD Qs

ete ee h m Qlhre n4orrlt2tgwi T neAy Vfe:raressi etrSoa ?

. Yn : AqAetiohtynspe euors?


comments