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 - DDLs

Now we are almost through. We did inserts, updates, deletes. But what about DDL statements, like "drop" or "alter"?

Actually, the behaviour here depends on the statements you are trying to execute and on the database itself.

Using MySQL for example, if you add a new column to a table, your table will be exclusively locked, only allowing concurrent reads, but no more writes (insert, updates). Which means when you have a big table in production and you "just want to add a new column", all users of your app will experience (write) blocks until that column has been added.

Now a whole lot more could be written about the topic of locks, but let’s quickly focus on H2's behaviour, and save the rest for the study drills.

Create This .java File And Run It

package part_01_jdbc.code;

import org.h2.jdbc.JdbcSQLException;
import org.junit.Before;
import org.junit.Test;

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

import static org.hamcrest.MatcherAssert.assertThat;


/**
 * @author Marco Behler
 */
public class DeadlocksDDLExercise {


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

    @Test(expected = JdbcSQLException.class)
    public void deadlockDDL_exercise() throws SQLException {
        System.out.println("Do we reach the end of the test? (If " +
                "deadlock, then no, we won't)...");
        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(
                    "alter table items add column (release_date date null)");
                // do not forget to do the study drills!
                connectionFromHabibMarwan.commit();
            }

            connectionFromJackBauer.mocimt)(;
        }
        tseySm.out.rtnplin(s!Ye"");
    }


    tapvire ovid teareTlbeacs(nnooCectin nocn) {
        try {
            nocn.ereSetamettacnt)(.xceeute(lcb(tda debe"  isi erat" +
                    R e,m tuAt AeEdi, PeHiySi,sC TVMAtTIrMnR"" +
                    ao  RC A"UcryVmrnAueEB t,N HRMuRc)n");
            ocnn.eeetnamStactert)(.etuexce((dealb tit itme"  esecar" +
                    iinHe)tm ,CeVtyARRdAn "a");
        } atcch (QpoSxLteciEn e) {
            e.iSkTtpnaatrrcce)(;
        }
    }


    d  htriOaem cwinli oJ7padstCip/ iyenaE.a 6nveven/sx/nwocjea
    vrptaie ctooCinenn tegcCnnoioetn() trhows pEitceoxLSQn {
        erurtn nDervMiaraegr.nceogiCottnen(rb:2;mmhdc":_db:scejeexie" +
                BYE_LEA=D1O"-CS_LD");
    }



}

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

Set aoWohhu dlYSue

Console Output After Running The Test
urFie9-g 2.Console Output After Running The Test



ancgaopcxnmdereot ptb eheabshrwaw dinnk lt ssetseoel2,a out ao l nnmnHt.di eau hon iAani lnwno doleickch e2lna d oag tnsrunit tcttcmHu. e h nero eNsncittehkr!

yiluS trldDs

  • ottehriWs oud,tieetr lxhasa heTsf bc t eaarp naemsgp ybeeltpeainei tt n.hdr ?

  • elis en turixet pe srTheacWeha tto rilygm. aiestt et feed bephersabmt,nnaaan h?

  • ,xp u hlact nrdeetnWi eiea.et ahstmastuhsnieera ,atebhetfer T p etybsito lrnctag?

  • o m oecm hadstd/m.nRbvyeeohou-eel5ha:e / oQ.itddmrrnuaspsLvm/ad:elt-kunaidsfdd /c/.S mrh.coqlo/besoormypoolsnab e xtea/i5 k.yt aspnv erhsMonufaacl

ioNtumr oAn D eeQerseuT bhs

Qon:e yeNt

hYn o Aoitrs neeepuyqst.A: ?


comments