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 718943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:35:09+00:00 2026-05-14T05:35:09+00:00

I am trying to create a database deadlock and I am using JUnit. I

  • 0

I am trying to create a database deadlock and I am using JUnit. I have two concurrent tests running which are both updating the same row in a table over and over again in a loop.

My idea is that you update say row A in Table A and then row B in Table B over and over again in one test. Then at the same time you update row B table B and then row A Table A over and over again. From my understanding this should eventually result in a deadlock.

Here is the code For the first test.

public static void testEditCC()
{
    try{
        int rows = 0;
        int counter = 0;
        int large=10000000;
        Connection c=DataBase.getConnection();
        while(counter<large)
        {
            int pid = 87855;
            int cCode = 655;
            String newCountry="Egypt";              
            int bpl = 0;
            stmt = c.createStatement();

            rows = stmt.executeUpdate("UPDATE main " +              //create lock on main table
                                                  "SET BPL="+cCode+
                                                  "WHERE ID="+pid);
            rows = stmt.executeUpdate("UPDATE BPL SET DESCRIPTION='SomeWhere' WHERE ID=602"); //create lock on bpl table
            counter++;
        }

        assertTrue(rows == 1);
        //rows = stmt.executeUpdate("Insert into BPL (ID, DESCRIPTION) VALUES ("+cCode+", '"+newCountry+"')");

    }
    catch(SQLException ex)
    {
        ex.printStackTrace();
        //ex.getMessage();
    }
}

And here is the code for the second test.

public static void testEditCC()
{
    try{
        int rows = 0;
        int counter = 0;
        int large=10000000;
        Connection c=DataBase.getConnection();
        while(counter<large)
        {
            int pid = 87855;
            int cCode = 655;
            String newCountry="Jordan";         
            int bpl = 0;
            stmt = c.createStatement();
            //stmt.close();
            rows = stmt.executeUpdate("UPDATE BPL SET DESCRIPTION='SomeWhere' WHERE ID=602"); //create lock on bpl table
            rows = stmt.executeUpdate("UPDATE main " +          //create lock on main table
                                                  "SET BPL="+cCode+
                                                  "WHERE ID="+pid);
            counter++;
        }

        assertTrue(rows == 1);
        //rows = stmt.executeUpdate("Insert into BPL (ID, DESCRIPTION) VALUES ("+cCode+", '"+newCountry+"')");

    }
    catch(SQLException ex)
    {
        ex.printStackTrace();
    }
}

I am running these two separate JUnit tests at the same time and am connecting to an apache Derby database that I am running in network mode within Eclipse. Can anyone help me figure out why a deadlock is not occurring? Perhaps I am using JUnit wrong.

  • 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-05-14T05:35:09+00:00Added an answer on May 14, 2026 at 5:35 am

    You should check the transaction isolation level, as it determines whether or not the DB locks rows touched by a transaction. If the isolation level is too low, no locking occurs, so no deadlock either.

    Update: according to this page, the default tx isolation level for Derby is read committed, which should be OK. The page is worth reading btw, as it explains tx isolation and its different levels, and what problems it solves.

    Next question then: what is DataBase in your code? This seems to be a nonstandard way to get a connection.

    Update2: I think I got it. Quote from the API doc:

    Note: By default a Connection object is in auto-commit mode, which means that it automatically commits changes after executing each statement. If auto-commit mode has been disabled, the method commit must be called explicitly in order to commit changes; otherwise, database changes will not be saved.

    In other words, rows are not locked because your effective transactions last only for the lifetime of individual updates. You should switch off autocommit before starting to work with your connection:

    Connection c=DataBase.getConnection();
    c.setAutoCommit(false);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 473k
  • Answers 473k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It sounds like you need to create a webcrawler. Web… May 16, 2026 at 3:55 am
  • Editorial Team
    Editorial Team added an answer Where are you adding the textbox? In the detail group?… May 16, 2026 at 3:55 am
  • Editorial Team
    Editorial Team added an answer Yes, you'll have to create some new providers though. Here… May 16, 2026 at 3:55 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.