From: gu.martinm@gmail.com Date: Thu, 9 Oct 2014 16:31:53 +0000 (+0200) Subject: Data base deadlocks. How to deal with them (retries) X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=6305efd94dddbfff9108b9d17ef812d4bcbe23ee;p=JavaForFun Data base deadlocks. How to deal with them (retries) --- diff --git a/SpringJava/DeadLocksSQL/src/main/java/de/example/sql/deadlocks/Main.java b/SpringJava/DeadLocksSQL/src/main/java/de/example/sql/deadlocks/Main.java index 80ef633..7ba468c 100644 --- a/SpringJava/DeadLocksSQL/src/main/java/de/example/sql/deadlocks/Main.java +++ b/SpringJava/DeadLocksSQL/src/main/java/de/example/sql/deadlocks/Main.java @@ -17,10 +17,11 @@ public class Main { public static void main(String[] args) { final ThreadGate trx1Gate = new ThreadGate(); final ThreadGate trx2Gate = new ThreadGate(); + final FutureTask[] tasks = new FutureTask[2]; logger.info("Starting application"); - final FutureTask taskFirst = new FutureTask + tasks[0] = new FutureTask ( new Runnable(){ @@ -34,7 +35,7 @@ public class Main { }, null ); - final FutureTask taskSecond = new FutureTask + tasks[1] = new FutureTask ( new Runnable(){ @@ -49,20 +50,24 @@ public class Main { null ); - new Thread(taskFirst).start(); - new Thread(taskSecond).start(); + for (final FutureTask task : tasks) { + new Thread(task).start(); + } // Wait for end. - try { - taskFirst.get(); - taskSecond.get(); - } catch (final InterruptedException e) { - logger.error("Error", e); - } catch (final ExecutionException e) { - logger.error("Error", e); + for (final FutureTask task : tasks) { + try { + task.get(); + } catch (final InterruptedException e) { + logger.error("Error", e); + } catch (final ExecutionException e) { + logger.error("Error", e); + } finally { + task.cancel(true); + } } - + SpringContextLocator.getInstance().close(); logger.info("End application"); } } diff --git a/SpringJava/DeadLocksSQL/src/main/java/de/example/sql/deadlocks/SpringContextLocator.java b/SpringJava/DeadLocksSQL/src/main/java/de/example/sql/deadlocks/SpringContextLocator.java index 36bdda1..0c8a8a8 100644 --- a/SpringJava/DeadLocksSQL/src/main/java/de/example/sql/deadlocks/SpringContextLocator.java +++ b/SpringJava/DeadLocksSQL/src/main/java/de/example/sql/deadlocks/SpringContextLocator.java @@ -2,7 +2,6 @@ package de.example.sql.deadlocks; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; @@ -11,7 +10,7 @@ public final class SpringContextLocator { // Spring Context private static final String SPRING_CONFIG_CONTEXT="/spring-config.xml"; // Spring ApplicationContext - private final ApplicationContext context; + private final ClassPathXmlApplicationContext context; private SpringContextLocator() { @@ -34,4 +33,8 @@ public final class SpringContextLocator { public Object getBean(final String name) { return context.getBean(name); } + + public void close() { + context.close(); + } } diff --git a/SpringJava/DeadLocksSQL/src/main/java/de/example/sql/deadlocks/example/FirstTransaction.java b/SpringJava/DeadLocksSQL/src/main/java/de/example/sql/deadlocks/example/FirstTransaction.java index 08472ec..23d72a5 100644 --- a/SpringJava/DeadLocksSQL/src/main/java/de/example/sql/deadlocks/example/FirstTransaction.java +++ b/SpringJava/DeadLocksSQL/src/main/java/de/example/sql/deadlocks/example/FirstTransaction.java @@ -30,7 +30,7 @@ public class FirstTransaction { } } - public void doFirstStepWithGate() { + private void doFirstStepWithGate() { logger.info("Start doFirstStepWithGate"); logger.info("doFirstStepWithGate UPDATING"); @@ -56,7 +56,7 @@ public class FirstTransaction { logger.info("End doFirstStepWithGate"); } - public void doFirstStepWithoutGate() { + private void doFirstStepWithoutGate() { logger.info("Start doFirstStepWithoutGate"); logger.info("doFirstStepWithoutGate UPDATING"); @@ -77,8 +77,6 @@ public class FirstTransaction { final JdbcOperations jdbcTemplate = new JdbcTemplate(dataSource); jdbcTemplate.execute("UPDATE children SET name='Bob', parent_id='1' WHERE id='2'"); - // trx2 continues (fourth step) - logger.info("End doThirdStep"); }