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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:55:17+00:00 2026-05-20T04:55:17+00:00

Probably a trivial question, but I want to get the best possible solution. Problem:

  • 0

Probably a trivial question, but I want to get the best possible solution.

Problem:

I have two or more workers that insert keys into one or more tables. The problem arises when two or more workers try to insert the same key into one of those key tables at the same time.
Typical problem.

  1. Worker A reads the table if a key exists (SELECT). There is no key.
  2. Worker B reads the table if a key exists (SELECT). There is no key.
  3. Worker A inserts the key.
  4. Worker B inserts the key.
  5. Worker A commits.
  6. Worker B commits. Exception is throws as unique constraint is violated

The key tables are simple pairs. First column is autoincrement integer and the second is varchar key.

What is the best solution to such a concurrency problem? I believe it is a common problem. One way for sure is to handle the exceptions thrown, but somehow I don’t believe this is the best way to tackle this.

The database I use is Firebird 2.5

EDIT:

Some additional info to make things clear.

  1. Client side synchronization is not a good approach, because the inserts come from different processes (workers). And I could have workers across different machines someday, so even mutexes are a no-go.
  2. The primary key and the first columns of such a table is autoincrement field. No problem there. The varchar field is the problem as it is something that the client inserts.

Typical such table is a table of users. For instance:

1  2056
2  1044
3  1896
4  5966
...

Each worker check if user "xxxx" exists and if not inserts it.

EDIT 2:

Just for the reference if somebody will go the same route. IB/FB return pair of error codes (I am using InterBase Express components). Checking for duplicate value violation look like this:

except
  on E: EIBInterBaseError do
  begin
    if (E.SQLCode = -803) and (E.IBErrorCode = 335544349) then
    begin
      FKeysConnection.IBT.Rollback;
      EnteredKeys := False;
    end;
  end;
end;
  • 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-20T04:55:17+00:00Added an answer on May 20, 2026 at 4:55 am

    With Firebird you can use the following statement:

    UPDATE OR INSERT INTO MY_TABLE (MY_KEY) VALUES (:MY_KEY) MATCHING (MY_KEY) RETURNING MY_ID
    
    • assuming there is a BEFORE INSERT trigger which will generate the MY_ID if a NULL value is being inserted.

    Here is the documentation.

    Update: The above statement will avoid exceptions and cause every statement to succeed. However, in case of many duplicate key values it will also cause many unnecessary updates.
    This can be avoided by another approach: just handle the unique constraint exception on the client and ignore it. The details depend on which Delphi library you’re using to work with Firebird but it should be possible to examine the SQLCode returned by the server and ignore only the specific case of unique constraint violation.

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

Sidebar

Related Questions

Probably a long question for a simple solution, but here goes... I have a
This probably has a simple answer, but I must not have had enough coffee
This probably sounds really stupid but I have noo idea how to implement jquery's
Probably an easy one: Are there any rules of thumb or pointers that could
Probably not much more to elaborate on here - I'm using a NumericStepper control
This question would probably apply equally as well to other languages with C-like multi-line
As many of you probably know, online banks nowadays have a security system whereby
Probably a really simple one this - I'm starting out with C# and need
I probably spend far too much time trying to make my visual interfaces look
As probably many people around here I read a few webcomics. Drowtales is my

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.