Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

Data Anomoly

We have a J2EE application used for a fun run signup. We are using Worldpay payment, so when WP calls back to our servlet confirming the payment details, only at this stage is the applicant given a race number (although they already have an applicantID). The race number is generated via the code below.

            con = getEventCon();  
            stmt = con.prepareStatement("SELECT raceNumber FROM applicant ORDER BY raceNumber DESC LIMIT 1");

            synchronized(EventVariables.MUTEX){
                rs = stmt.executeQuery();
                rs.first();
                lastRaceNo = rs.getInt(1);

                // write the raceNumbers to the DB
                for (int i=0; i<applicants; i++){
                    stmt = con.prepareStatement("UPDATE applicant SET raceNumber=? WHERE applicantID=?");
                    stmt.setInt(1, lastRaceNo+i+1);
                    stmt.setInt(2, applicantIDArray[i]);
                    stmt.executeUpdate();
                }
            }

As you can see, the racenumber is generated by extracting the highest ()atest) race number, and adding 1 to it for that applicants race number.

But, somehow, after ~2000 people have signed up, we've ended up with an anomoly that has me completely stumped - we've got 2 race numbers that have been skipped. This shouldnt be able to happen, because of the way it works. I can also confirm that no records have been deleted (I would see a gap in the applicantID's if this was the case).

Anybody got any ideas?

colr__
0
colr__
Asked:
colr__
  • 8
  • 6
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
>> synchronized(EventVariables.MUTEX){

Is this inside a singleton class?
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Also - any other exceptions in the logs? Can you put a try/ catch inside the for loop and log a message there in case an exception occurs.
0
 
colr__Author Commented:
I'm not 100% sure if its a singleton (i wasnt familiar with this pattern, but ive read about it now and I think this class qualifies). The EventVarables calss is below:




package rtr_Servlets;



public class EventVariables {
   
   
   
    public static final float OWNER_ENTRY_FEE = 7f;
    public static final float OTHER_ENTRY_FEE = 7f;
    public static final String OWNER_NAME = "The Event Organisers";
   
    // Date changes must also be applied to the javascript files registration1.js and registration2.js, and also  participants.jsp.
    public static final java.util.Date UNDER_16_CUTOFF = new  java.util.Date("14/05/1990");
   
    public static final String MUTEX = "mutex";
   
    public static final int NO_OF_CYCLISTS = 1000; // max number of cyclists
    public static final int NO_OF_WALKERS = 3000; // max number of walkers
    public static final int NO_OF_RUNNERS = 3000; // max number of runners
    public static final int COUNTDOWN_TIMER = 200; // the number from which the countdown starts (exclusive)
   
}

As you cans see, there is no constructor defined and nothing other than static fields, so does this qualify as a singleton? If it doesnt, am I still setting up the mutual exlusion correctlty?

Anyways, even if concurrent updates did occur, this would result in multiple of the same race number, as opposed to skippign some altogether.

As far as I can see there have been no exceptions, if there weere i would have recieved a report on it.

At this late stage in the event, its not going to be feesible to change the code, ut I was just curious as to why this happened.

colr__
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Mayank SAssociate Director - Product EngineeringCommented:
No, I didn't mean about the EventVariables class. I meant the class inside which this method has been written (which has the code that you initially posted in your Q). Is it a singleton or not?

A singleton is a class which can have only one instance in the running process:

public class Singleton
{
  private static Singleton instance ; // the singleton instance  private Singleton () // given private access so that nobody can instantiate this class from outside and create multiple instances
  {
  }
  public static synchronized Singleton getInstance ()
  {
    if ( instance == null )
      instance = new Singleton () ; // instantiate only once
    return instance ;
  }
}
0
 
colr__Author Commented:
It is a servlet, and not a singleton.

colr__
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Instead of synchronizing over mutex which is a String defined in another class, you can just use:

synchronized ( this )
{
  // all the code inside your synchronized block as it is now
}

If the class is singleton, it will make sure that only one thread executes this block of code at one time.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
>> It is a servlet, and not a singleton.

Well the servlet container might maintain one or different instances depending upon how it is configured. Anyway, it still looks like it should be correct (because the variable on which you are synchronizing is publicly available).

Where do you have try/ catch blocks in the code you posted? If an exception happens, where/ when will it get logged.
0
 
colr__Author Commented:
On this particular script, if an exception is thrown, the user is told that there registration has failed and they are not registered. This doesnt make any difference though, as the next person that registered should be given the correct race number, based on the last entered race number, regardless of any erros that have previously occured.

I dont think Im going to get to the bottom of this, but its really bugging me as I cant see how it could have happened.

colr__
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You update multiple application details at one time, right? So let's say the array has got 10 of them, and the 5th one fails, the others are also not updated?

>> At this late stage in the event, its not going to be feesible to change the code,

Ok, I was wondering if it was possible to use batch-updates for the entire array instead of looping and shooting individual executeUpdate () commands.
0
 
colr__Author Commented:
>>You update multiple application details at one time, right? So let's say the array has got 10 of them, and the 5th one fails, the others are also not updated?

Correct, but even if they did fail, the next correctly registered applicant gets ther race number by adding one to the last race number enetered, so we should be gueneteed that it will always be sequential. Thats what I really dont understand about it.

Cheers for the input btw.

colr__
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Yes, that is correct. If no exceptions are shown in the logs, it is difficult to figure out where the errors could be.

>> I would see a gap in the applicantID's if this was the case).

Is it a mandatory field in the table? Which were the 2 applicant IDs for which the racenumber were skipped?
0
 
colr__Author Commented:
applicantID is the table key, and is auto incremental. There are no gaps in it, so I know there has been no deletions.

colr__
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Difficult one to crack ;) not sure if there is a solution to this. Sorry I could not help more.
0
 
colr__Author Commented:
This problem unfortunately wsnt resolved. Id like to give mayankeagle the points for the effort, although it shouldnt be PAQ's as there is no solution.

Accept moderators decision on this one. Thanks for the help with this mayankeagle.

colr__
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 8
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now