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

The Archive Base Latest Questions

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

I need to keep track of a sequential Id, this is being returned to

  • 0

I need to keep track of a sequential Id, this is being returned to me via a SP which is doing a max(id) across 4 tables, there is no identifer/sequence in the db which is managing the sequence. This will obviously have concurrency issues so i created a helper class to ensure unique Id’s are always generated.

The helper is initialised via its repository, which initially calls the DB to find the current Id, all subsequent requests for an Id are serviced in memory via the helper. There will only ever be 1 app using the DB (mine) so i dont need to worry about someone else coming along and creating transactions & throwing the Id out of sync. I think ive got the basics of thread-saftey but im worried about a race condition when the helper is initialised, can someone please advise 🙂

private class TransactionIdProvider
{
private static readonly object Accesslock = new object();
private int _transactionId;
public int NextId
{
    get
    {
        lock (Accesslock)
        {
            if(!Initialised) throw new Exception("Must Initialise with id first!!");
            return _transactionId++;
        }
    }
}

public bool Initialised { get; private set; }

public void SetId(int id)
{
    lock (Accesslock)
    {
        if (Initialised) return;
        _transactionId = id;
        Initialised = true;
    }
}

public TransactionIdProvider()
{
    Initialised = false;
}
}

The helper class is initialised in a repository:

private static readonly TransactionIdProvider IdProvider = new TransactionIdProvider();

    public int GetNextTransactionId()
    {
        if(!IdProvider.Initialised)
        {
            // Ask the DB
            int? id = _context.GetNextTransactionId().First();
            if (!id.HasValue)
                throw new Exception("No transaction Id returned");
            IdProvider.SetId(id.Value);
        }

        return IdProvider.NextId;
    }
  • 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-26T09:09:28+00:00Added an answer on May 26, 2026 at 9:09 am

    It is thread-safe, but it’s unnecessarily slow.
    You don’t need a lock to just increment a number; instead, you can use atomic math.

    Also, you’re sharing the lock across all instances (it’s static), which is unnecessary. (There’s nothing wrong with having two different instances run at once)

    Finally, (IMHO) there is no point in having a separate uninitialized state.

    I would write it like this:

    class TransactionIdProvider {
        private int nextId;
        public TransactionIdProvider(int id) {
            nextId = value;
        }
    
        public int GetId() {
            return Interlocked.Increment(ref nextId);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my code, I need to keep track of some pages which are being
I need to keep track of std::set element by saving the iterator returned by
I need to keep track of which user has visited which page how many
I need to keep track of rows in sections in my UITableView. For this
I need to keep track of how many items are in a node of
I need to keep track of number of hits on a particular item in
I need to keep the arrow keys from being able to scroll through my
I need to keep a couple of Jena Models (OntModels, specifically) synchronized across a
I need to keep track of indexes/rows that are selected. When a row is
I need to keep track of the previously selected segment of a UISegmentControl. Is

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.