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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:39:25+00:00 2026-05-28T23:39:25+00:00

I came across some legacy code that is supposed to keep an incrementing sequence

  • 0

I came across some legacy code that is supposed to keep an incrementing sequence number in a Sequences table. This sequence number will be used as an ID for new records in another table (the Orders table).

What I think it is supposed to be doing is:

  1. If there is a record for this sequence, get the value and return that number + 1.
  2. If there is no record, scan the actual table to find the current max, round it up to the nearest 1000 and record that max in the Sequences table.

Here’s the code:

private static final long SEQUENCE_BLOCK_SIZE = 1000;
private static final String ID_FIELD_NAME = "Order_ID";
private static final String TABLE_NAME = "Orders";
private static long lastID = 0;
String init = null;

public long newID() throws Exception {
  Connection c = null;
  long id = 0;

  try {
    c = Connections.getConnection(init);
    id = nextID(c);
  } catch(Exception e) {
    try {
      c.close();
    } catch(Exception ignore) {
    }
    throw e;
  } finally {
    if ( c != null ) {
      Connections.putConnection(c);
    }
  }

  return id;
}

/**
 * Returns a new unique id for the account.
 */
protected static synchronized long nextID(Connection c) throws Exception {
  // Only update the table occasionally.
  if(lastID % SEQUENCE_BLOCK_SIZE == 0) {
    Statement s = null;
    ResultSet r = null;

    try {
      lastID = 0;

      s = c.createStatement();

      // Lock the row. +++ EH??? +++
      s.executeUpdate("UPDATE sequences SET sequence_value=sequence_value WHERE sequence_name='" + ID_FIELD_NAME + "'");

      // Get the current value.
      r = s.executeQuery("SELECT sequence_value FROM sequences WHERE sequence_name='" + ID_FIELD_NAME + "'");
      if(r.next()) {
        lastID = r.getLong(1);
      }
      r.close();

      s.close();

      if(lastID == 0) {
        // Get the current max value from the table.
        s = c.createStatement();
        r = s.executeQuery("SELECT MAX(" + ID_FIELD_NAME + ") FROM " + TABLE_NAME + "");
        if(r.next()) {
          lastID = ((r.getLong(1) + SEQUENCE_BLOCK_SIZE) / SEQUENCE_BLOCK_SIZE) * SEQUENCE_BLOCK_SIZE;
        }
        r.close();
        s.close();

        // Insert the new row.
        s = c.createStatement();
        s.executeUpdate("INSERT INTO sequences(sequence_value,sequence_name) VALUES(" + (lastID + SEQUENCE_BLOCK_SIZE) + ",'" + ID_FIELD_NAME + "')");
        s.close();
      }else {
        // Update the row.
        s = c.createStatement();
        s.executeUpdate("UPDATE sequences SET sequence_value=" + (lastID + SEQUENCE_BLOCK_SIZE) + " WHERE sequence_name='" + ID_FIELD_NAME + "'");
        s.close();
      }
    } catch(Exception e) {
      throw e;
    } finally {
      try {
        r.close();
      } catch(Exception e) {
      }
      try {
        s.close();
      } catch(Exception e) {
      }
    }
  }

  return lastID++;
}

My problem is that when there is no record in the Sequences table it is not adding a new record, although it IS executing the INSERT. I have tested the INSERT separately and it seems to work fine. I believe it is something to do with the //Lock the row statement. I cannot find any documentation that implies that the statement would indeed lock that row or even what effect it would have.

I am testing against SQL Server 2008 but this same mechanism is supposed to work against 2000+ and Oracle.

Added In response to comments.

I accept that it would be better/more efficient to use the native database mechanism for unique sequence numbers. Sadly this app is designed to drive any of about six different database systems and certainly both Oracle and MS SQL so sticking with this technique would be preferable.

We run our sessions in autocommit mode. Why is the INSERT not creating a new record? Is it something to do with the attempt at locking?

  • 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-28T23:39:25+00:00Added an answer on May 28, 2026 at 11:39 pm

    The issue was that I was not comitting the transaction. I was wrong about running our sessions in autocommit mode.

    The record lock acts as @Sérgio said in his comment.

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

Sidebar

Related Questions

I came across some legacy code that contains a function like this: LPCTSTR returnString()
I came across some tests that included code like this: if (this != null)
A colleague came across some code that looked like this and couldn't understand how
I came across some code written in C that looks like this: if (file
So I came across some code this morning that looked like this: try {
Whilst refactoring some code I came across some getter methods that returns a std::string.
Context I came across some code, like this: if( Some_Condition ) throw 0; I
A while ago, I came across some code that marked a data member of
I came across some code today that surprised me. A variable was defined (outside
I came across some Java code that has a method containing the following: static

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.