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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:46:29+00:00 2026-06-17T03:46:29+00:00

Background and System View We have implemented a Billing system in a distributed environment.

  • 0

Background and System View

We have implemented a Billing system in a distributed environment. There are 4 terminals that generate around 2 bills per minute per terminal. We use Mysql as backend and C#, winforms as our client tech.

The most important constraint in any billing system is that the invoice number must be sequential. To do that I run a query similar to

In pseudo-code

let x ="SELECT count(*) from Orders where IsInvoiceGenerated=1 and FinancialYear=val

new invoicenum = x + 1;

The Problem
Everything was running ok till 411th invoice, after which the system suddenly skipped 2 invoices and generated Invoice 414. We investigated the system state and found that system was not tampered externally and we also inferred that nobody accessed database from workbench. This is a major issue since it also has legal ramifications.

Can you please suggest the best way to ensure that billing number always remains sequential.?

  • 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-06-17T03:46:30+00:00Added an answer on June 17, 2026 at 3:46 am

    Before I start, I’d just like to apologize to @Grumbler85 – you were right. This question bugged me for a while and I’ll try to answer it as best to my knowledge as I can.

    Both transactions and locking are insufficient solutions.

    Reasons: Locks aren’t good because once you lock a table, you have to unlock it. Unlock might fail, we all know the unstable nature of networks and computers in general. Bottom line is – you’d have to use your C# application to issue locks and unlocks.
    Every time you generate an invoice, you’d have to lock the table that is being used as a counter, forcing every other MySQL session to wait until you release the lock. From my experience, within a few days you’d have to hire an administrator whose job would be to release the locks.

    Transactions aren’t good enough because each transaction operates on a snapshot of the data (simplified explanation, transaction isolation level can be modified). That means that 1 transaction can calculate that invoice number must be 6, while another transaction would calculate also that invoice number must be 6.

    What you could do is make the invoice_number unique so if 2 (or more) transactions try to insert the same number, you’d get an exception for at least 1 of them, thus preventing gaps but failing the invoice creation.

    Using auto_increment is also not an option. Auto_increment is just a simple counter. That means that auto_increment doesn’t “reuse” numbers dropped for some reason – reason being that an error occurred and transaction couldn’t be saved, effectively making the auto_increment calculated for that record to be lost.

    So what options are there? Personally, I would create a simple service which would run at predefined time-intervals which would update invoices that haven’t got invoice_number set. The service wouldn’t offer concurrent access, and there would always be one connection active which would work on a set of already inserted invoices.

    It is true that there are laws in place (in certain countries, such as England) which specify that there MUST be a sequenced invoice numbering, I was wrong about that as well. Source: http://www.hmrc.gov.uk/vat/managing/charging/vat-invoices.htm and excerpt from the source:

    an invoice number which is unique and follows on from the number of
    the previous invoice – if you spoil or cancel a serially numbered
    invoice, you must keep it to show to a VAT officer at your next VAT
    inspection

    The final option is that you are satisfied with invoice creation failure if two or more transactions acquire the same invoice number, which means you’d have to implement a way of re-running the failed transaction (which is everything but simple).

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

Sidebar

Related Questions

Some background: we have a windows application (c#) that locate in the system try.
Background of project -> I have to upgrade the Linux embedded system remotely. This
I have a css rule defining the background color as the window system color
I have a View that works but I can not figure out how to
Background: I have a built-in-house custom SharePoint 2010 web part, inheriting from System.Web.UI.WebControls.WebParts.WebPart ,
The question: Is there a way to view the XML that would be created
I'v built a application using codeigniter and I have a message system that displays
I have a Linux driver running in the background that is able to return
I have a view that is made to be displayed in landscape mode only,
we have here some problems with our Magento system. First the background : We

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.