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:
- If there is a record for this sequence, get the value and return that number + 1.
- 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
Sequencestable.
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?
The issue was that I was not comitting the transaction. I was wrong about running our sessions in
autocommitmode.The record lock acts as @Sérgio said in his comment.