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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:23:44+00:00 2026-05-18T00:23:44+00:00

I have a simple table with a primary key. Most of the read operations

  • 0

I have a simple table with a primary key. Most of the read operations fetch one row by the exact value of the key.

The data in each row maintains some relationship with rows before and after it in the key order. So when I insert a new row I need to read the 2 rows between which it is going to enter, make some computation and then to insert.

The concern, clearly, is that at the same time another connection may add a row with a key value in the same interval. I am covered if it is exactly the same value of the key as the second insert would fail, but if the key value is different but in the same interval the relationship may be broken.

The solution seems to be to lock the whole table for writing when I decide to add a new row, or (if possible, which I doubt) to lock an interval of key values. Yet I’d prefer that read-only transactions would not be blocked at that time.

I am using ODBC with libodbc++ wrapper for C++ in the client program and IBM DB2 free edition (although the DB choice may still change). This is what I thought of doing:

  • start the connection in the auto-commit and default isolation mode
  • when need to add a new row, set auto-commit to false and isolation mode to serialized
  • read the rows before and after the new key value
  • compute and insert the new row
  • commit
  • return back to the auto-commit and default isolation mode

Will this do the job? Will other transactions be allowed to read at the same time? Are there other/better ways to do it?

BTW, I don’t see in the libodbc++ i/f a way to specify a read-only transaction. Is it possible in odbc?

EDIT: thanks for the very useful answers, I had trouble selecting one.

  • 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-18T00:23:44+00:00Added an answer on May 18, 2026 at 12:23 am

    If your database is in SERIALIZABLE mode, you won’t have any issues at all. Given a key K, to get the previous and next keys you have to run the following queries:

    select key from keys where key > K order by key limit 1;      # M?
    select key from keys where key < K order by key desc limit 1; # I?
    

    The above works in MySQL. This equivalent query works in DB2 (from the comments):

    select key from keys where key = (select min(key) from keys where key > K);
    select key from keys where key = (select max(key) from keys where key < K);
    

    The first query sets up a range lock that prevents other transactions from inserting a key greater than K and less than or equal to M.

    The second query sets up a range lock that prevents other transactions from inserting a key less than K and greater than or equal to I.

    The unique index on the primary key prevents K from being inserted twice. So you’re completely covered.

    This is what transactions are about; so you can write your code as if the entire database is locked.

    Note: This requires a database that supports true serializability. Fortunately, DB2 does. Other DBMS’s that support true serializability: SQLServer, and MySQL/InnoDB. DBMS’s that don’t: Oracle, PostgreSQL!

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

Sidebar

Related Questions

I have a simple table with autoincrementing ID's and one text column. id(int), value(nvarchar)
Imagine I have this simple table: Table Name: Table1 Columns: Col1 NUMBER (Primary Key)
I have a simple table with 2 columns - name and the primary key,
I have got a simple MySQL table and primary index (id) is not numbered
If I have a simple table where the data is such that the rows
I have a simple hierarchy of categories in an SQLite3 database, each row storing
I have a myisam table with a primary key spanning 5 columns. I do
I have a table, schema is very simple, an ID column as unique primary
Does the JPA specification allow simple references to non-primary key columns? I have a
Given the following very simple table: Create Table tblUserLogins ( LoginNumber int PRIMARY KEY

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.