You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ofbiz.apache.org by do...@apache.org on 2009/03/27 17:57:00 UTC
svn commit: r759234 [3/3] - in
/ofbiz/trunk/framework/entity/src/org/ofbiz/entity: ./ cache/ condition/
config/ connection/ datasource/ eca/ finder/ jdbc/ model/ test/
transaction/ util/
Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/SequenceUtil.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/SequenceUtil.java?rev=759234&r1=759233&r2=759234&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/SequenceUtil.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/util/SequenceUtil.java Fri Mar 27 16:56:58 2009
@@ -79,17 +79,17 @@
SequenceBank bank = this.getBank(seqName, seqModelEntity);
return bank.getNextSeqId(staggerMax);
}
-
+
public void forceBankRefresh(String seqName, long staggerMax) {
// don't use the get method because we don't want to create if it fails
SequenceBank bank = sequences.get(seqName);
if (bank == null) {
return;
}
-
+
bank.refresh(staggerMax);
}
-
+
private SequenceBank getBank(String seqName, ModelEntity seqModelEntity) {
SequenceBank bank = sequences.get(seqName);
@@ -102,7 +102,7 @@
}
}
}
-
+
return bank;
}
@@ -135,7 +135,7 @@
stagger = Math.round(Math.random() * staggerMax);
if (stagger == 0) stagger = 1;
}
-
+
if ((curSeqId + stagger) <= maxSeqId) {
Long retSeqId = Long.valueOf(curSeqId);
curSeqId += stagger;
@@ -152,7 +152,7 @@
}
}
}
-
+
public void refresh(long staggerMax) {
this.curSeqId = this.maxSeqId;
this.fillBank(staggerMax, this.seqModelEntity);
@@ -163,7 +163,7 @@
// no need to get a new bank, SeqIds available
if ((curSeqId + stagger) <= maxSeqId) return;
-
+
long bankSize = defaultBankSize;
if (seqModelEntity != null && seqModelEntity.getSequenceBankSize() != null) {
bankSize = seqModelEntity.getSequenceBankSize().longValue();
@@ -172,9 +172,9 @@
// NOTE: could use staggerMax for this, but if that is done it would be easier to guess a valid next id without a brute force attack
bankSize = stagger * defaultBankSize;
}
-
+
if (bankSize > maxBankSize) bankSize = maxBankSize;
-
+
long val1 = 0;
long val2 = 0;
@@ -184,7 +184,7 @@
while (val1 + bankSize != val2) {
if (Debug.verboseOn()) Debug.logVerbose("[SequenceUtil.SequenceBank.fillBank] Trying to get a bank of sequenced ids for " +
this.seqName + "; start of loop val1=" + val1 + ", val2=" + val2 + ", bankSize=" + bankSize, module);
-
+
// not sure if this synchronized block is totally necessary, the method is synchronized but it does do a wait/sleep
//outside of this block, and this is the really sensitive block, so making sure it is isolated; there is some overhead
//to this, but very bad things can happen if we try to do too many of these at once for a single sequencer
@@ -193,15 +193,15 @@
try {
//if we can suspend the transaction, we'll try to do this in a local manual transaction
suspendedTransaction = TransactionUtil.suspend();
-
+
boolean beganTransaction = false;
try {
beganTransaction = TransactionUtil.begin();
-
+
Connection connection = null;
Statement stmt = null;
ResultSet rs = null;
-
+
try {
connection = ConnectionFactory.getConnection(parentUtil.helperName);
} catch (SQLException sqle) {
@@ -211,18 +211,18 @@
Debug.logWarning("[SequenceUtil.SequenceBank.fillBank]: Unable to esablish a connection with the database... Error was: " + e.toString(), module);
throw e;
}
-
+
if (connection == null) {
throw new GenericEntityException("[SequenceUtil.SequenceBank.fillBank]: Unable to esablish a connection with the database, connection was null...");
}
-
+
String sql = null;
-
+
try {
// we shouldn't need this, and some TX managers complain about it, so not including it: connection.setAutoCommit(false);
-
+
stmt = connection.createStatement();
-
+
sql = "SELECT " + parentUtil.idColName + " FROM " + parentUtil.tableName + " WHERE " + parentUtil.nameColName + "='" + this.seqName + "'";
rs = stmt.executeQuery(sql);
boolean gotVal1 = false;
@@ -231,7 +231,7 @@
gotVal1 = true;
}
rs.close();
-
+
if (!gotVal1) {
Debug.logWarning("[SequenceUtil.SequenceBank.fillBank] first select failed: will try to add new row, result set was empty for sequence [" + seqName + "] \nUsed SQL: " + sql + " \n Thread Name is: " + Thread.currentThread().getName() + ":" + Thread.currentThread().toString(), module);
sql = "INSERT INTO " + parentUtil.tableName + " (" + parentUtil.nameColName + ", " + parentUtil.idColName + ") VALUES ('" + this.seqName + "', " + startSeqId + ")";
@@ -240,12 +240,12 @@
}
continue;
}
-
+
sql = "UPDATE " + parentUtil.tableName + " SET " + parentUtil.idColName + "=" + parentUtil.idColName + "+" + bankSize + " WHERE " + parentUtil.nameColName + "='" + this.seqName + "'";
if (stmt.executeUpdate(sql) <= 0) {
throw new GenericEntityException("[SequenceUtil.SequenceBank.fillBank] update failed, no rows changes for seqName: " + seqName);
}
-
+
sql = "SELECT " + parentUtil.idColName + " FROM " + parentUtil.tableName + " WHERE " + parentUtil.nameColName + "='" + this.seqName + "'";
rs = stmt.executeQuery(sql);
boolean gotVal2 = false;
@@ -253,13 +253,13 @@
val2 = rs.getLong(parentUtil.idColName);
gotVal2 = true;
}
-
+
rs.close();
-
+
if (!gotVal2) {
throw new GenericEntityException("[SequenceUtil.SequenceBank.fillBank] second select failed: aborting, result set was empty for sequence: " + seqName);
}
-
+
// got val1 and val2 at this point, if we don't have the right difference between them, force a rollback (with
//setRollbackOnly and NOT with an exception because we don't want to break from the loop, just err out and
//continue), then flow out to allow the wait and loop thing to happen
@@ -289,7 +289,7 @@
} catch (GenericTransactionException gte2) {
Debug.logError(gte2, "Unable to rollback transaction", module);
}
-
+
// error, break out of the loop to not try to continue forever
break;
} finally {
@@ -311,14 +311,14 @@
}
}
}
-
+
if (val1 + bankSize != val2) {
if (numTries >= maxTries) {
String errMsg = "[SequenceUtil.SequenceBank.fillBank] maxTries (" + maxTries + ") reached for seqName [" + this.seqName + "], giving up.";
Debug.logError(errMsg, module);
return;
}
-
+
// collision happened, wait a bounded random amount of time then continue
int waitTime = (new Double(Math.random() * (maxWaitMillis - minWaitMillis))).intValue() + minWaitMillis;