Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8556275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:18:38+00:00 2026-06-11T15:18:38+00:00

There are sometimes errors like this when using SQL Server: Transaction (Process ID 54)

  • 0

There are sometimes errors like this when using SQL Server:

Transaction (Process ID 54) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

For more background, check out what Jeff Atwood has blogged about this issue.

I’d like to create an SQL Server deadlock programmatically with a small test using plain-old JDBC. The test should immediately create a deadlock so that I can test some retry logic.

My understanding, from reading Jeff’s analysis, is that I need only have some data and read it a lot, and write it a little.

I currently have a short Java program (below) that creates a table and writes some test data to the table. The program them launches several hundred threads. Each thread either does an update, or a read of the test data. I have varied the ratio of update to read operations, but regardless of the ratio, I cannot seem to programmatically create a deadlock. This version of the test program does not have my retry logic, I’ll add that once I can reliably get SQL Server deadlocks happening.

I wondered whether having all of the threads running in a single process might somehow serialize operations at the JDBC driver level, so I tried running several processes concurrently (on the same machine), but still no deadlocks.

import java.sql.*;
import java.util.*;
import java.util.concurrent.*;
import static java.util.concurrent.TimeUnit.*;

public class Deadlock {
    static final int QUERY_THREAD_COUNT = 300, MAX_OPERATIONS_ITERATION = 5000;
    static String server, db, user, pw;
    static CountDownLatch latch = new CountDownLatch(QUERY_THREAD_COUNT);

    public static void main(String... args) throws Exception {
        server = args[0];
        db     = args[1];
        user   = args[2];
        pw     = args[3];
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection connection = getConnection();
        Statement statement = connection.createStatement();
        statement.execute("CREATE TABLE TESTTABLE (BAR INTEGER, BAZ VARCHAR(32))");
        statement.execute("DELETE FROM TESTTABLE");
        statement.execute("INSERT INTO TESTTABLE VALUES (1, 'FOOBARBAZ')");
        connection.setAutoCommit(false);
        connection.commit();
        connection.close();
        ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
        for (int i = 0; i < QUERY_THREAD_COUNT; ++i) {
            scheduledExecutorService.scheduleWithFixedDelay(new Operation(), 0, 1, MILLISECONDS);
        }
        latch.await();
        System.exit(0);
    }

    static class Operation implements Runnable {
        Connection connection = getConnection();
        Statement statement = getStatement(connection);
        int iteration;

        @Override
        public void run() {
            if (++iteration > MAX_OPERATIONS_ITERATION) {
                latch.countDown();
                return;
            }
            try {
                double  random = Math.random();
                boolean update = (random < 0.01);
                if (update) {
                    statement.executeUpdate("UPDATE TESTTABLE SET BAR=" + ((int) (random * 100)) + " WHERE BAZ='FOOBARBAZ'");
                } else {
                    ResultSet rs = statement.executeQuery("SELECT BAR, BAZ FROM TESTTABLE");
                    if (! rs.next()) {
                        return;
                    }
                    int    bar = rs.getInt(1);
                    String baz = rs.getString(2);
                    if (bar > 100) {
                        System.err.println("int is greater than 100");
                    }
                    if (! baz.equals("FOOBARBAZ")) {
                        System.err.println("string is not FOOBARBAZ");
                    }
                }
                connection.commit();
            } catch (SQLException sqle) { // <-- looking for a deadlock exception here!
                System.err.println(sqle);
            }
        }
    }

    static Connection getConnection() {
        try {
            return DriverManager.getConnection("jdbc:sqlserver://" + server + ";databaseName=" + db + ";", user, pw);
        } catch (Exception e) {
            System.err.println(e);
            throw new RuntimeException(e);
        }
    }

    static Statement getStatement(Connection connection) {
        try {
            return connection.createStatement();
        } catch (Exception e) {
            System.err.println(e);
            throw new RuntimeException(e);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:18:39+00:00Added an answer on June 11, 2026 at 3:18 pm

    I think this does it:

    import java.sql.*;
    import java.util.*;
    import java.util.concurrent.*;
    
    /**
     * Creates an SQL Server deadlock.
     *
     * <pre>
       javac SQLServerDeadlock.java && java -cp ".:sqljdbc.jar" SQLServerDeadlock <server> <db-name> <username> <password>
     * </pre>
     */
    public class SQLServerDeadlock {
        static String server, db, user, pw;
        static String TABLE_A = "TABLE_A", TABLE_B = "TABLE_B";
        static CountDownLatch latch = new CountDownLatch(2);
    
        public static void main(String... args) throws SQLException {
            server = args[0];
            db     = args[1];
            user   = args[2];
            pw     = args[3];
            Connection connection = null;
            try {
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                connection = getConnection();
                init(connection);
                Thread t1 = new Thread(new Update(TABLE_A, TABLE_B), "A-THEN-B");
                Thread t2 = new Thread(new Update(TABLE_B, TABLE_A), "B-THEN-A");
                if (Math.random() < .5) {
                    t1.start();
                    t2.start();
                } else {
                    t2.start();
                    t1.start();
                }
                t1.join();
                t2.join();
            } catch (Exception e) {
                System.err.println(e);
            } finally {
                cleanup(connection);
            }
        }
    
        static class Update implements Runnable {
            String table1;
            String table2;
    
            Update(String table1, String table2) {
                this.table1 = table1;
                this.table2 = table2;
            }
    
            @Override
            public void run() {
                Connection connection = null;
                try {
                    connection = getConnection();
                    Statement statement = connection.createStatement();
                    statement.executeUpdate("UPDATE " + table1 + " SET FOO=1");
                    latch.countDown();
                    latch.await();
                    statement.executeUpdate("UPDATE " + table2 + " SET FOO=1");
                    connection.commit();
                    System.err.println(Thread.currentThread().getName() + ": SUCCESS!");
                } catch (SQLException sqle) {
                    if (sqle.getMessage().contains("Rerun the transaction")) {
                        System.err.println(Thread.currentThread().getName() + ": DEADLOCK VICTIM!");
                    }
                    System.err.println(sqle);
                } catch (InterruptedException ie) {
                    System.err.println(ie);
                } finally {
                    try {
                        connection.close();
                    } catch (SQLException sqle) {
                        System.err.println(sqle);
                    }
                }
            }
        }
    
        static void init(Connection connection) throws SQLException {
            Statement statement = null;
            try {
                statement = connection.createStatement();
                for (String tableName : Arrays.asList(TABLE_A, TABLE_B)) {
                    if (tableExists(connection, tableName)) {
                        statement.execute("DROP TABLE " + tableName);
                    }
                    statement.execute("CREATE TABLE " + tableName + " (FOO INTEGER)");
                    statement.execute("INSERT INTO  " + tableName + " VALUES (0)");
                }
                connection.commit();
            } finally {
                statement.close();
            }
        }
    
        static void cleanup(Connection connection) throws SQLException {
            if (connection == null) {
                return;
            }
            Statement statement = null;
            try {
                statement = connection.createStatement();
                for (String tableName : Arrays.asList(TABLE_A, TABLE_B)) {
                    if (tableExists(connection, tableName)) {
                        statement.execute("DROP TABLE " + tableName);
                    }
                }
                connection.commit();
            } finally {
                statement.close();
            }
        }
    
        static boolean tableExists(Connection connection, String tableName) throws SQLException {
            Statement statement = null;
            try {
                statement = connection.createStatement();
                String sql =
                    " SELECT TABLE_NAME                         " +
                    "   FROM INFORMATION_SCHEMA.TABLES          " +
                    "  WHERE TABLE_CATALOG = '" + db        + "'" +
                    "    AND TABLE_NAME    = '" + tableName + "'";
                ResultSet rs = statement.executeQuery(sql);
                return rs.next();
            } finally {
                statement.close();
            }
        }
    
        static Connection getConnection() throws SQLException {
            Connection connection = DriverManager.getConnection("jdbc:sqlserver://" + server + ";databaseName=" + db + ";", user, pw);
            connection.setAutoCommit(false);
            return connection;
        }
    }
    

    The randomization of thread starts isn’t necessary, but doesn’t affect correctness. The thread scheduler should interleave thread execution arbitrarily. However, in my environment I observed that the second thread to start was almost, but not quite always, the deadlock victim.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my Page has many hover images, sometimes there are backgrounds from div-elements or src
I connect my windows mobile app directly to ms-sql server using the sqlclient dll
I have this C++ project which compiles using a Makefile, and sometimes when (
I am using SQL Server Management studio and keep getting the same error, and
I was using something like this: $(document).ready(function() { $('#my-img').load(function() { // do something });
I know, I know... there are many others posts like this. I didn't find
I have a column called post_tags where there is sometimes one tag entry and
I program in C++. Sometimes there are 1000 ways to do something, and depending
I am importing data from csv file, sometimes there are column headers and some
I download a png file from link. But sometimes there is no file. And

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.