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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:42:56+00:00 2026-05-23T10:42:56+00:00

I have a file that holds an integer ID value. Currently reading the file

  • 0

I have a file that holds an integer ID value. Currently reading the file is protected with ReaderWriterLockSlim as such:

    public int GetId()
    {
        _fileLock.EnterUpgradeableReadLock();
        int id = 0;
        try {
            if(!File.Exists(_filePath))
                CreateIdentityFile();

            FileStream readStream = new FileStream(_filePath, FileMode.Open, FileAccess.Read);
            StreamReader sr = new StreamReader(readStream);
            string line = sr.ReadLine();
            sr.Close();
            readStream.Close();
            id = int.Parse(line);
            return int.Parse(line);
        }
        finally {
            SaveNextId(id);     // increment the id 
            _fileLock.ExitUpgradeableReadLock();
        }
    }

The problem is that subsequent actions after GetId() might fail. As you can see the GetId() method increments the ID every single time, disregarding what happens after it has issued an ID. The issued ID might be left hanging (as said, exceptions might occur). As the ID is incremented, some IDs might be left unused.

So I was thinking of moving the SaveNextId(id) out, remove it (the SaveNextId() actually uses the lock too, except that it’s EnterWriteLock). And call it manually from outside after all the required methods have executed. That brings out another problem – multiple threads might enter the GetId() method before the SaveNextId() gets executed and they might all receive the same ID.

I don’t want any solutions where I have to alter the IDs after the operation, correcting them in any way because that’s not nice and might lead to more problems.

I need a solution where I can somehow callback into the FileIdentityManager (that’s the class that handles these IDs) and let the manager know that it can perform the saving of the next ID and then release the read lock on the file containing the ID.

Essentialy I want to replicate the relational databases autoincrement behaviour – if anything goes wrong during row insertion, the ID is not used, it is still available for use but it also never happens that the same ID is issued. Hopefully the question is understandable enough for you to provide some solutions..

UPDATE: Please see the comments to the answers for more details about the behaviour I want

  • 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-23T10:42:57+00:00Added an answer on May 23, 2026 at 10:42 am

    Essentialy I want to replicate the
    relational databases autoincrement
    behaviour – if anything goes wrong
    during row insertion, the ID is not
    used, it is still available for use

    but it also never happens that the
    same ID is issued. Hopefully the
    question is understandable enough for
    you to provide some solutions.

    Generally speaking that is not the behavior that I’ve observed. When you insert a row into table with an autoincrement inside a transaction and its rolled back you’ve lost the ID.

    So in my opinion the way you’ve implemented this is the correct behavior.

    Update
    The only way you can ensure that you “don’t want to waste them on unsuccesful file saves, unsuccesful type casts, etc.” Is to change the scope of your blocking code to block from the moment you request a new ID until your save is complete and on failure to rollback the increment to the ID.

    This will drastically reduce the level of parallelism you can achieve.

    If you want to keep the potential for parallelism higher, you should check everything you can before you request an ID e.g. check types and format errors.

    Obviously some things like external errors (IO exceptions) you simply cannot do anything about.

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

Sidebar

Related Questions

I have a CSV file that holds about 200,000 - 300,000 records. Most of
Suppose I have two lists that holds the list of source file names and
We have a file that has a 64 bit integer as a string in
i have an index.html file that holds: document.addEventListener(deviceready,onDeviceReady,false); function onDeviceReady(){ var deviceuid = device.uuid;
I have a selectedArray defined as a property in my file that holds which
I have an autoupdater C# program. It will download a rar file that holds
I have a JTextField in my Swing application that holds the file path of
I have a small class that holds two strings as follows: public class ReportType
I have a StringUtilities.cs file in the CommonFunctions Project that holds a UppercaseFirst function
I have a numeric.xml file in my project's values directory that holds most of

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.