Data base deadlocks. How to deal with them (retries)
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Thu, 9 Oct 2014 16:31:53 +0000 (18:31 +0200)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Thu, 9 Oct 2014 16:31:53 +0000 (18:31 +0200)
SpringJava/DeadLocksSQL/src/main/java/de/example/sql/deadlocks/Main.java
SpringJava/DeadLocksSQL/src/main/java/de/example/sql/deadlocks/SpringContextLocator.java
SpringJava/DeadLocksSQL/src/main/java/de/example/sql/deadlocks/example/FirstTransaction.java

index 80ef633..7ba468c 100644 (file)
@@ -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<Void>[] tasks = new FutureTask[2];
 
                logger.info("Starting application");
 
-               final FutureTask<Void> taskFirst = new FutureTask<Void>
+               tasks[0] = new FutureTask<Void>
                (
                        new Runnable(){
 
@@ -34,7 +35,7 @@ public class Main {
                        },
                        null
                );
-               final FutureTask<Void> taskSecond = new FutureTask<Void>
+               tasks[1] = new FutureTask<Void>
                (
                        new Runnable(){
 
@@ -49,20 +50,24 @@ public class Main {
                        null
                );
 
-               new Thread(taskFirst).start();
-               new Thread(taskSecond).start();
+               for (final FutureTask<Void> 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<Void> 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");
        }
 }
index 36bdda1..0c8a8a8 100644 (file)
@@ -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();
+       }
 }
index 08472ec..23d72a5 100644 (file)
@@ -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");
     }