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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:04:28+00:00 2026-05-24T09:04:28+00:00

Background Given: a set of threads each thread has its own data source objects

  • 0

Background

Given:

  • a set of threads
  • each thread has its own data source
  • objects in each data source references objects in other data sources
  • there is a possibility for duplicate objects across various data sources
  • the threads are writing to a database with an engine that enforces the foreign key constraint
  • each type of object gets its own table and references the other objects through a foreign key
  • each thread has its own connection to the database
  • Proposed Solution

    A register class which tracks the ID of the objects that have been written. The inteface of the register class has public methods, thus (represented in Java):

    public interface Register
    {
        synchronized boolean requestObjectLock(int id);
        synchronized boolean saveFinalized(int id);
        synchronized boolean checkSaved(int id);
    }
    

    The method requestObjectLock checks to see if the object has been locked by another thread yet, and returns false it has. Otherwise, it locks that ID and returns true. It is then the responsibility of the calling thread to call saveFinalized when it has been successfully written to the database, and the responsibility of all other threads to check to see whether it has been written already with checkSaved before writing an object that references it. In other words, there are three states an object can be in: unregistered, locked (registered but unwritten), and saved (registered and written).

    Reasoning

    As far as I know there is no way to guarentee that one SQL query will finish before another when called by different threads. Thus, if an object was only registered or unregistered, it seems possible that a thread could check to see if an object was written, start writing an object that referenced it, and have its query complete (and fail) before the query that actually wrote the referenced object did.

    Questions

    Is it possible to guarantee the sequence of execution of queries being executed by different threads? And therefore, is this solution overengineered? Is there a simpler solution? On the other hand, is it safe?

    • 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-24T09:04:31+00:00Added an answer on May 24, 2026 at 9:04 am

      The terms you need to research on the database side are “transaction isolation level” and “concurrency control”. DBMS platform support varies. Some platforms implement a subset of the isolation levels defined in the SQL standards. (The SQL standards allow this. They’re written in terms of what behavior isn’t allowed.) And different platforms approach concurrency control in different ways.

      Wikipedia, although not authoritative, has a good introduction to isolation levels, and also a good introduction to concurrency control.

      As far as I know there is no way to guarentee that one SQL query will
      finish before another when called by different threads.

      That’s kind of true. It’s also kind of not true. In SQL standards, transaction isolation levels aren’t concerned with who finishes first. They’re concerned with behavior that’s not allowed.

      dirty read: Transaction A can read data written by concurrent, uncommitted transaction B.

      nonrepeatable read: Transaction A reads data twice. A concurrent transaction, B, commits between the two reads. The data transaction A read first is different from the data it read second, because of transaction B. (Some people describe transaction A as seeing “same rows, different column values”.)

      phantom read: Transaction A reads data twice. A concurrent transaction, B, commits between the two reads. Transaction A’s two reads return two different sets of rows, because transaction B has affected the evaluation of transaction A’s WHERE clause. (Some people describe transaction A as seeing “same column values, different rows”.)

      You control transaction behavior in SQL using SET TRANSACTION. So SET TRANSACTION SERIALIZABLE means dirty reads, nonrepeatable reads, and phantom reads are impossible. SET TRANSACTION REPEATABLE READ allows phantom reads, but dirty reads and nonrepeatable reads are impossible.

      You’ll have to check your platform’s documentation to find out what it supports. PostgreSQL, for example, supports all four isolation levels syntactically. But internally it only has two levels: read committed and serializable. That means you can SET TRANSACTION READ UNCOMMITTED, but you’ll get “read committed” behavior.

      Important for you: The effect of a serializable isolation level is to guarantee that transactions appear to have been issued one at a time by a single client. But that’s not quite the same thing as saying that if transaction A starts before transaction B, it will commit before transaction B. If they don’t affect each other, the dbms is allowed to commit transaction B first without violating the serializable isolation level semantics.

      When I have questions myself about how these work, I test them by opening two command-line clients connected to the same database.

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

    Sidebar

    Related Questions

    I have set the background for a given view using the following code, however,
    Background: My application has a main window which contains a few smaller NSView objects,
    Background My program is storing a series of objects, a set of tags, and
    Given an object on a plain white background, does anybody know if OpenCV provides
    I have given a table background image using css background-image property. The cells are
    Given IE6, an UL-LI list and a background image for the UL container. <style>
    First, the background: I'm working in Tapestry 4, so the HTML for any given
    I've been trying to set up a custom background for the whole of my
    if (reader.is_lazy()) goto tldr; I have a background thread that does some I/O-intensive background
    Given the following html: <body> <div style=float: left; background: red>Hi</div> <div style=float: left; background:

    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.