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

  • SEARCH
  • Home
  • 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 8997139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:50:53+00:00 2026-06-15T23:50:53+00:00

I need to do the following in four zillion threads at the same time

  • 0

I need to do the following in four zillion threads at the same time (well, probably closer to 2 threads, but either way…): If such-and-such does not exist, insert it.

Currently, the application says:

select rows from the db
if row count == 0,
    do some stuff
    insert a row

I have a situation where this is run in 2 separate threads at the same time. This way, the 2 threads may (and in fact do every time so far) each check for existing rows before either thread inserts rows; therefore we have duplicate rows. This is bad.

All the algorithms I can think of fall short one way or another.

For example, if I do this:

open tx
insert a row
select rows
if row count > 2, rollback
else commit

when the transaction uses READ_COMMITTED isolation, then one thread won’t see another thread’s inserted rows, and duplicates are possible. With READ_UNCOMMITTED isolation, each thread may see the other thread’s rows and both will rollback. I think I’ll have the same problem if I use a MERGE statement instead of inserting then selecting (or vice versa).

Is there an algorithm to use to guarantee that exactly 1 row will be inserted when the above algorithm is executed concurrently? FWIW, I’m using DB2, mybatis, and xml-based tx management with Spring, but I’ll gladly translate from something else if possible.

I’m a newbie when it comes to concurrency, so if this question reveals ignorance remedied by a book or article you know of, please share.

EDIT:

The insert statement above is to lazily grant users something iff they don’t have it. In this case, a uniqueness constraint would be appropriate. Elsewhere in the app, however, it would not be appropriate. 🙁

I’ll make the example a bit more concrete soon so it’s easier to understand.

  • 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-15T23:50:54+00:00Added an answer on June 15, 2026 at 11:50 pm

    The only solution we could find was to issue the “insert if no records meeting criteria exist” command in a serializable transaction. The SQL itself could be anything that inserts records if none meeting certain criteria exist (e.g. MERGE would work). In this scenario, once one thread checks for the existence of a row meeting the criteria of the selected statement, no other thread may insert anything meeting that criteria until the tx is completed. For performance, we opened a new tx just for the merge operation and committed immediately upon completion.

    We tested this solution with DB2 and Oracle with success.

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

Sidebar

Related Questions

Now I have four UITableViewControllers . A B1, B2 C I need the following
I need to solve the following problem. //pseudo algorithm you have four elements: elm1,
I need to compare four variables.if any two variables are same i need show
I need following function (from C++ dll) available in C++/CLI extern C _declspec(dllexport) void
I need following namespaces to use native wpf property grid however, VS 2010 doesn't
I need the following C function in Python: int func(Uint8 *bytRecvBuffer, int *iRecvLen); I
I need the following xml to be made in code: <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical android:gravity=right
I need the following authentication script finished. I am weak at php/pdo so I
I need the following logic. If array contains value , return it else return
I really need the following in magento. When a customer shows the config product

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.