Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyan D. Brooks2016-08-15 23:50:48 +0000
committerRyan D. Brooks2016-08-31 16:18:24 +0000
commit4d32910430d7b1475d519bd674a7caec5cb91b5b (patch)
tree0060fcb4f2f8f6972117249686448de269584f4f /plugins
parent433b3c31d4cde1e950b1b32e3e5e4d056c654470 (diff)
downloadorg.eclipse.osee-4d32910430d7b1475d519bd674a7caec5cb91b5b.tar.gz
org.eclipse.osee-4d32910430d7b1475d519bd674a7caec5cb91b5b.tar.xz
org.eclipse.osee-4d32910430d7b1475d519bd674a7caec5cb91b5b.zip
refinement[ats_ATS309927]: Remove race condition in id generation
Diffstat (limited to 'plugins')
-rw-r--r--plugins/org.eclipse.osee.jdbc/src/org/eclipse/osee/jdbc/internal/JdbcSequenceProvider.java122
1 files changed, 41 insertions, 81 deletions
diff --git a/plugins/org.eclipse.osee.jdbc/src/org/eclipse/osee/jdbc/internal/JdbcSequenceProvider.java b/plugins/org.eclipse.osee.jdbc/src/org/eclipse/osee/jdbc/internal/JdbcSequenceProvider.java
index 05ac30acbca..b824bb7d54e 100644
--- a/plugins/org.eclipse.osee.jdbc/src/org/eclipse/osee/jdbc/internal/JdbcSequenceProvider.java
+++ b/plugins/org.eclipse.osee.jdbc/src/org/eclipse/osee/jdbc/internal/JdbcSequenceProvider.java
@@ -14,15 +14,15 @@ import java.util.HashMap;
import org.eclipse.osee.framework.core.data.OseeData;
import org.eclipse.osee.framework.jdk.core.type.OseeCoreException;
import org.eclipse.osee.jdbc.JdbcClient;
-import org.eclipse.osee.jdbc.JdbcConnection;
-import org.eclipse.osee.jdbc.JdbcTransaction;
/**
- * This should be modified to use native database sequences
- *
* @author Ryan D. Brooks
*/
public class JdbcSequenceProvider {
+ private static final String QUERY_SEQUENCE = "SELECT last_sequence FROM osee_sequence WHERE sequence_name = ?";
+ public static final String INSERT_SEQUENCE = "INSERT INTO osee_sequence (last_sequence, sequence_name) VALUES (?,?)";
+ private static final String UPDATE_SEQUENCE =
+ "UPDATE osee_sequence SET last_sequence = ? WHERE sequence_name = ? AND last_sequence = ?";
private final HashMap<String, SequenceRange> sequences = new HashMap<>(30);
@@ -31,98 +31,59 @@ public class JdbcSequenceProvider {
}
public synchronized long getNextSequence(JdbcClient client, String sequenceName, boolean aggressiveFetch) throws OseeCoreException {
- JdbcSequenceTx sequenceTx = new JdbcSequenceTx(client, sequenceName, aggressiveFetch);
- client.runTransaction(sequenceTx);
- return sequenceTx.getNextSequence();
- }
-
- private final class JdbcSequenceTx extends JdbcTransaction {
-
- private static final String QUERY_SEQUENCE = "SELECT last_sequence FROM osee_sequence WHERE sequence_name = ?";
- public static final String INSERT_SEQUENCE =
- "INSERT INTO osee_sequence (last_sequence, sequence_name) VALUES (?,?)";
- private static final String UPDATE_SEQUENCE =
- "UPDATE osee_sequence SET last_sequence = ? WHERE sequence_name = ? AND last_sequence = ?";
-
- private final JdbcClient client;
- private final String sequenceName;
- private long nextSequence;
- private final boolean aggressiveFetch;
-
- public JdbcSequenceTx(JdbcClient client, String sequenceName, boolean aggressiveFetch) {
- super();
- this.client = client;
- this.sequenceName = sequenceName;
- this.aggressiveFetch = aggressiveFetch;
- }
-
- public long getNextSequence() {
- return nextSequence;
- }
-
- public boolean isAggressiveFetch() {
- return aggressiveFetch;
- }
-
- private SequenceRange getRange(String sequenceName) {
- SequenceRange range = sequences.get(sequenceName);
- if (range == null) {
- range = new SequenceRange(isAggressiveFetch());
- sequences.put(sequenceName, range);
+ SequenceRange range = getRange(sequenceName);
+ if (range.lastAvailable == 0) {
+ long lastValue = -1L;
+ boolean gotSequence = false;
+ while (!gotSequence) {
+ long currentValue = client.fetch(lastValue, QUERY_SEQUENCE, sequenceName);
+ if (currentValue == lastValue) {
+ internalInitializeSequence(client, sequenceName);
+ lastValue = 0;
+ } else {
+ lastValue = currentValue;
+ }
+ gotSequence =
+ client.runPreparedUpdate(UPDATE_SEQUENCE, lastValue + range.prefetchSize, sequenceName, lastValue) == 1;
}
- return range;
+ range.updateRange(lastValue, aggressiveFetch);
+ }
+ range.currentValue++;
+ if (range.currentValue == range.lastAvailable) {
+ range.lastAvailable = 0;
}
+ return range.currentValue;
+ }
- @Override
- public void handleTxWork(JdbcConnection connection) {
- SequenceRange range = getRange(sequenceName);
- if (range.lastAvailable == 0) {
- long lastValue = -1L;
- boolean gotSequence = false;
- while (!gotSequence) {
- long currentValue = client.fetch(connection, lastValue, QUERY_SEQUENCE, sequenceName);
- if (currentValue == lastValue) {
- internalInitializeSequence(connection, sequenceName);
- lastValue = 0;
- } else {
- lastValue = currentValue;
- }
- gotSequence = client.runPreparedUpdate(connection, UPDATE_SEQUENCE, lastValue + range.prefetchSize,
- sequenceName, lastValue) == 1;
- }
- range.updateRange(lastValue);
- }
- range.currentValue++;
- if (range.currentValue == range.lastAvailable) {
- range.lastAvailable = 0;
- }
- nextSequence = range.currentValue;
+ private SequenceRange getRange(String sequenceName) {
+ SequenceRange range = sequences.get(sequenceName);
+ if (range == null) {
+ range = new SequenceRange();
+ sequences.put(sequenceName, range);
}
+ return range;
+ }
- private void internalInitializeSequence(JdbcConnection connection, String sequenceName) throws OseeCoreException {
- SequenceRange range = getRange(sequenceName);
- range.lastAvailable = 0;
- int initalValue = 0;
- if (sequenceName.equals(OseeData.ART_ID_SEQ)) {
- initalValue = 200000;
- }
- client.runPreparedUpdate(connection, INSERT_SEQUENCE, initalValue, sequenceName);
+ private void internalInitializeSequence(JdbcClient client, String sequenceName) throws OseeCoreException {
+ SequenceRange range = getRange(sequenceName);
+ range.lastAvailable = 0;
+ int initalValue = 0;
+ if (sequenceName.equals(OseeData.ART_ID_SEQ)) {
+ initalValue = 200000;
}
+ client.runPreparedUpdate(INSERT_SEQUENCE, initalValue, sequenceName);
}
private static final class SequenceRange {
private long currentValue;
private long lastAvailable;
private int prefetchSize;
- private final boolean aggressiveFetch;
- public SequenceRange(boolean aggressiveFetch) {
- super();
+ public SequenceRange() {
this.prefetchSize = 1;
- this.aggressiveFetch = aggressiveFetch;
}
- public void updateRange(long lastValue) {
+ public void updateRange(long lastValue, boolean aggressiveFetch) {
currentValue = lastValue;
lastAvailable = lastValue + prefetchSize;
@@ -131,5 +92,4 @@ public class JdbcSequenceProvider {
}
}
}
-
} \ No newline at end of file

Back to the top