Improved: General refactoring and code improvements, package

org.apache.ofbiz.entity.transaction.
(OFBIZ-9962)

I added some additional debug conditions.

Thanks Dennis Balkir for reporting and providing the patches.

git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1818441 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Brohl
2017-12-16 17:28:52 +00:00
parent 41c9269d25
commit b41f78ae1a
6 changed files with 93 additions and 117 deletions
@@ -39,13 +39,17 @@ public class DebugXaResource extends GenericXaResource {
@Override
public void commit(Xid xid, boolean onePhase) throws XAException {
TransactionUtil.debugResMap.remove(xid);
if (Debug.verboseOn()) Debug.logVerbose("Xid : " + xid.toString() + " cleared [commit]", module);
if (Debug.verboseOn()) {
Debug.logVerbose("Xid : " + xid.toString() + " cleared [commit]", module);
}
}
@Override
public void rollback(Xid xid) throws XAException {
TransactionUtil.debugResMap.remove(xid);
if (Debug.verboseOn()) Debug.logVerbose("Xid : " + xid.toString() + " cleared [rollback]", module);
if (Debug.verboseOn()) {
Debug.logVerbose("Xid : " + xid.toString() + " cleared [rollback]", module);
}
}
@Override
@@ -113,10 +113,9 @@ public class DumbTransactionFactory implements TransactionFactory {
if (datasourceInfo.getInlineJdbc() != null) {
Connection otherCon = ConnectionFactoryLoader.getInstance().getConnection(helperInfo, datasourceInfo.getInlineJdbc());
return TransactionUtil.getCursorConnection(helperInfo, otherCon);
} else {
Debug.logError("Dumb/Empty is the configured transaction manager but no inline-jdbc element was specified in the " + helperInfo.getHelperBaseName() + " datasource. Please check your configuration", module);
return null;
}
Debug.logError("Dumb/Empty is the configured transaction manager but no inline-jdbc element was specified in the " + helperInfo.getHelperBaseName() + " datasource. Please check your configuration", module);
return null;
}
public void shutdown() {}
@@ -86,9 +86,8 @@ public abstract class GenericXaResource extends Thread implements XAResource {
if (this.active) {
if (this.xid != null && this.xid.equals(xid)) {
throw new XAException(XAException.XAER_DUPID);
} else {
throw new XAException(XAException.XAER_PROTO);
}
throw new XAException(XAException.XAER_PROTO);
}
if (this.xid != null && !this.xid.equals(xid)) {
throw new XAException(XAException.XAER_NOTA);
@@ -145,9 +144,8 @@ public abstract class GenericXaResource extends Thread implements XAResource {
public Xid[] recover(int flag) throws XAException {
if (this.xid == null) {
return new Xid[0];
} else {
return new Xid[] {this.xid};
}
return new Xid[] {this.xid};
}
/**
@@ -77,19 +77,12 @@ public class GeronimoTransactionFactory implements TransactionFactory {
if (datasourceInfo != null && datasourceInfo.getInlineJdbc() != null) {
return ConnectionFactoryLoader.getInstance().getConnection(helperInfo, datasourceInfo.getInlineJdbc());
} else {
Debug.logError("Geronimo is the configured transaction manager but no inline-jdbc element was specified in the " + helperInfo.getHelperBaseName() + " datasource. Please check your configuration", module);
return null;
}
Debug.logError("Geronimo is the configured transaction manager but no inline-jdbc element was specified in the " + helperInfo.getHelperBaseName() + " datasource. Please check your configuration", module);
return null;
}
public void shutdown() {
ConnectionFactoryLoader.getInstance().closeAll();
/*
if (transactionContextManager != null) {
// TODO: need to do anything for this?
transactionContextManager = null;
}
*/
}
}
@@ -54,8 +54,7 @@ public class JNDITransactionFactory implements TransactionFactory {
volatile TransactionManager transactionManager = null;
volatile UserTransaction userTransaction = null;
// protected static UtilCache dsCache = new UtilCache("entity.JndiDataSources", 0, 0);
protected static final ConcurrentHashMap<String, DataSource> dsCache = new ConcurrentHashMap<String, DataSource>();
protected static final ConcurrentHashMap<String, DataSource> dsCache = new ConcurrentHashMap<>();
public TransactionManager getTransactionManager() {
if (transactionManager == null) {
@@ -67,8 +66,6 @@ public class JNDITransactionFactory implements TransactionFactory {
String jndiServerName = EntityConfig.getInstance().getTransactionFactory().getTransactionManagerJndi().getJndiServerName();
if (UtilValidate.isNotEmpty(jndiName)) {
// if (Debug.verboseOn()) Debug.logVerbose("[JNDITransactionFactory.getTransactionManager] Trying JNDI name " + jndiName, module);
try {
InitialContext ic = JNDIContextFactory.getInitialContext(jndiServerName);
@@ -140,35 +137,34 @@ public class JNDITransactionFactory implements TransactionFactory {
String jndiName = jndiJdbcElement.getJndiName();
String jndiServerName = jndiJdbcElement.getJndiServerName();
Connection con = getJndiConnection(jndiName, jndiServerName);
if (con != null) return TransactionUtil.getCursorConnection(helperInfo, con);
if (con != null) {
return TransactionUtil.getCursorConnection(helperInfo, con);
}
} else {
// Debug.logError("JNDI loaded is the configured transaction manager but no jndi-jdbc element was specified in the " + helperName + " datasource. Please check your configuration.", module);
}
if (datasourceInfo.getInlineJdbc() != null) {
Connection otherCon = ConnectionFactoryLoader.getInstance().getConnection(helperInfo, datasourceInfo.getInlineJdbc());
return TransactionUtil.getCursorConnection(helperInfo, otherCon);
} else {
//no real need to print an error here
return null;
}
//no real need to print an error here
return null;
}
public static Connection getJndiConnection(String jndiName, String jndiServerName) throws SQLException, GenericEntityException {
// if (Debug.verboseOn()) Debug.logVerbose("Trying JNDI name " + jndiName, module);
DataSource ds = dsCache.get(jndiName);
if (ds != null) {
if (ds instanceof XADataSource) {
XADataSource xads = (XADataSource) ds;
return TransactionUtil.enlistConnection(xads.getXAConnection());
} else {
return ds.getConnection();
}
return ds.getConnection();
}
try {
if (Debug.infoOn())
if (Debug.infoOn()) {
Debug.logInfo("Doing JNDI lookup for name " + jndiName, module);
}
InitialContext ic = JNDIContextFactory.getInitialContext(jndiServerName);
if (ic != null) {
@@ -178,29 +174,31 @@ public class JNDITransactionFactory implements TransactionFactory {
}
if (ds != null) {
if (Debug.verboseOn())
if (Debug.verboseOn()) Debug.logVerbose("Got a Datasource object.", module);
if (Debug.verboseOn()) {
Debug.logVerbose("Got a Datasource object.", module);
}
dsCache.putIfAbsent(jndiName, ds);
ds = dsCache.get(jndiName);
Connection con;
if (ds instanceof XADataSource) {
if (Debug.infoOn())
if (Debug.infoOn()) {
Debug.logInfo("Got XADataSource for name " + jndiName, module);
}
XADataSource xads = (XADataSource) ds;
XAConnection xac = xads.getXAConnection();
con = TransactionUtil.enlistConnection(xac);
} else {
if (Debug.infoOn())
if (Debug.infoOn()) {
Debug.logInfo("Got DataSource for name " + jndiName, module);
}
con = ds.getConnection();
}
return con;
} else {
Debug.logError("Datasource returned was NULL.", module);
}
Debug.logError("Datasource returned was NULL.", module);
} catch (NamingException ne) {
Debug.logWarning(ne, "Failed to find DataSource named " + jndiName + " in JNDI server with name " + jndiServerName + ". Trying normal database.", module);
} catch (GenericConfigException gce) {
@@ -64,14 +64,14 @@ public final class TransactionUtil implements Status {
// Debug module name
public static final String module = TransactionUtil.class.getName();
private static ThreadLocal<List<Transaction>> suspendedTxStack = new ThreadLocal<List<Transaction>>();
private static ThreadLocal<List<Exception>> suspendedTxLocationStack = new ThreadLocal<List<Exception>>();
private static ThreadLocal<Exception> transactionBeginStack = new ThreadLocal<Exception>();
private static ThreadLocal<List<Exception>> transactionBeginStackSave = new ThreadLocal<List<Exception>>();
private static ThreadLocal<RollbackOnlyCause> setRollbackOnlyCause = new ThreadLocal<RollbackOnlyCause>();
private static ThreadLocal<List<RollbackOnlyCause>> setRollbackOnlyCauseSave = new ThreadLocal<List<RollbackOnlyCause>>();
private static ThreadLocal<Timestamp> transactionStartStamp = new ThreadLocal<Timestamp>();
private static ThreadLocal<Timestamp> transactionLastNowStamp = new ThreadLocal<Timestamp>();
private static ThreadLocal<List<Transaction>> suspendedTxStack = new ThreadLocal<>();
private static ThreadLocal<List<Exception>> suspendedTxLocationStack = new ThreadLocal<>();
private static ThreadLocal<Exception> transactionBeginStack = new ThreadLocal<>();
private static ThreadLocal<List<Exception>> transactionBeginStackSave = new ThreadLocal<>();
private static ThreadLocal<RollbackOnlyCause> setRollbackOnlyCause = new ThreadLocal<>();
private static ThreadLocal<List<RollbackOnlyCause>> setRollbackOnlyCauseSave = new ThreadLocal<>();
private static ThreadLocal<Timestamp> transactionStartStamp = new ThreadLocal<>();
private static ThreadLocal<Timestamp> transactionLastNowStamp = new ThreadLocal<>();
private static final boolean debugResources = readDebugResources();
public static final Map<Xid, DebugXaResource> debugResMap = Collections.<Xid, DebugXaResource>synchronizedMap(new HashMap<Xid, DebugXaResource>());
@@ -89,17 +89,11 @@ public final class TransactionUtil implements Status {
}
public static <V> NoTransaction<V> noTransaction(Callable<V> callable) {
return new NoTransaction<V>(callable);
return new NoTransaction<>(callable);
}
// This syntax is groovy compatible, with the primary(callable) as the first arg.
// You could do:
// use (TransactionUtil) {
// Callable callable = ....
// Object result = callable.noTransaction().inTransaction(ifError, timeout, print).call()
// }
public static <V> InTransaction<V> inTransaction(Callable<V> callable, String ifErrorMessage, int timeout, boolean printException) {
return new InTransaction<V>(callable, ifErrorMessage, timeout, printException);
return new InTransaction<>(callable, ifErrorMessage, timeout, printException);
}
/** Begins a transaction in the current thread IF transactions are available; only
@@ -141,9 +135,8 @@ public final class TransactionUtil implements Status {
// do we have a cause? if so, throw special exception
if (UtilValidate.isNotEmpty(roc)) {
throw new GenericTransactionException("The current transaction is marked for rollback, not beginning a new transaction and aborting current operation; the rollbackOnly was caused by: " + roc.getCauseMessage(), roc.getCauseThrowable());
} else {
return false;
}
return false;
}
internalBegin(ut, timeout);
@@ -171,10 +164,11 @@ public final class TransactionUtil implements Status {
} catch (SystemException e) {
throw new GenericTransactionException("System error, could not begin transaction", e);
}
} else {
if (Debug.infoOn()) Debug.logInfo("No user transaction, so no transaction begun", module);
return false;
}
if (Debug.infoOn()) {
Debug.logInfo("No user transaction, so no transaction begun", module);
}
return false;
}
protected static void internalBegin(UserTransaction ut, int timeout) throws SystemException, NotSupportedException {
@@ -208,9 +202,8 @@ public final class TransactionUtil implements Status {
} catch (SystemException e) {
throw new GenericTransactionException("System error, could not get status", e);
}
} else {
return STATUS_NO_TRANSACTION;
}
return STATUS_NO_TRANSACTION;
}
public static String getStatusString() throws GenericTransactionException {
@@ -221,9 +214,8 @@ public final class TransactionUtil implements Status {
int status = getStatus();
if (status == STATUS_NO_TRANSACTION) {
return false;
} else {
return true;
}
return true;
}
@@ -269,10 +261,9 @@ public final class TransactionUtil implements Status {
Debug.logError(e, "Rollback Only was set when trying to commit transaction here; throwing rollbackOnly cause exception", module);
throw new GenericTransactionException("Roll back error, could not commit transaction, was rolled back instead because of: " + rollbackOnlyCause.getCauseMessage(), rollbackOnlyCause.getCauseThrowable());
} else {
Throwable t = e.getCause() == null ? e : e.getCause();
throw new GenericTransactionException("Roll back error (with no rollbackOnly cause found), could not commit transaction, was rolled back instead: " + t.toString(), t);
}
Throwable t = e.getCause() == null ? e : e.getCause();
throw new GenericTransactionException("Roll back error (with no rollbackOnly cause found), could not commit transaction, was rolled back instead: " + t.toString(), t);
} catch (IllegalStateException e) {
Throwable t = e.getCause() == null ? e : e.getCause();
throw new GenericTransactionException("Could not commit transaction, IllegalStateException exception: " + t.toString(), t);
@@ -315,10 +306,11 @@ public final class TransactionUtil implements Status {
if (ut != null) {
try {
int status = ut.getStatus();
if (Debug.verboseOn()) Debug.logVerbose("Current status : " + getTransactionStateString(status), module);
if (Debug.verboseOn()) {
Debug.logVerbose("Current status : " + getTransactionStateString(status), module);
}
if (status != STATUS_NO_TRANSACTION) {
//if (Debug.infoOn()) Thread.dumpStack();
if (causeThrowable == null && Debug.infoOn()) {
Exception newE = new Exception("Stack Trace");
Debug.logError(newE, "[TransactionUtil.rollback]", module);
@@ -331,7 +323,9 @@ public final class TransactionUtil implements Status {
clearSetRollbackOnlyCause();
ut.rollback();
Debug.logInfo("Transaction rolled back", module);
if (Debug.infoOn()) {
Debug.logInfo("Transaction rolled back", module);
}
} else {
Debug.logWarning("Transaction not rolled back, status is STATUS_NO_TRANSACTION", module);
}
@@ -343,7 +337,9 @@ public final class TransactionUtil implements Status {
throw new GenericTransactionException("System error, could not rollback transaction: " + t.toString(), t);
}
} else {
Debug.logInfo("No UserTransaction, transaction not rolled back", module);
if (Debug.infoOn()) {
Debug.logInfo("No UserTransaction, transaction not rolled back", module);
}
}
}
@@ -353,7 +349,9 @@ public final class TransactionUtil implements Status {
if (ut != null) {
try {
int status = ut.getStatus();
if (Debug.verboseOn()) Debug.logVerbose("Current code : " + getTransactionStateString(status), module);
if (Debug.verboseOn()) {
Debug.logVerbose("Current code : " + getTransactionStateString(status), module);
}
if (status != STATUS_NO_TRANSACTION) {
if (status != STATUS_MARKED_ROLLBACK) {
@@ -363,10 +361,14 @@ public final class TransactionUtil implements Status {
ut.setRollbackOnly();
setSetRollbackOnlyCause(causeMessage, causeThrowable);
} else {
Debug.logInfo("Transaction rollback only not set, rollback only is already set.", module);
if (Debug.infoOn()) {
Debug.logInfo("Transaction rollback only not set, rollback only is already set.", module);
}
}
} else {
Debug.logWarning("Transaction rollback only not set, status is STATUS_NO_TRANSACTION", module);
if (Debug.warningOn()) {
Debug.logWarning("Transaction rollback only not set, status is STATUS_NO_TRANSACTION", module);
}
}
} catch (IllegalStateException e) {
Throwable t = e.getCause() == null ? e : e.getCause();
@@ -376,7 +378,9 @@ public final class TransactionUtil implements Status {
throw new GenericTransactionException("System error, could not set rollback only on transaction: " + t.toString(), t);
}
} else {
Debug.logInfo("No UserTransaction, transaction rollback only not set", module);
if (Debug.infoOn()) {
Debug.logInfo("No UserTransaction, transaction rollback only not set", module);
}
}
}
@@ -390,13 +394,13 @@ public final class TransactionUtil implements Status {
Transaction trans = txMgr.suspend();
pushSuspendedTransaction(trans);
return trans;
} else {
return null;
}
} else {
Debug.logWarning("No transaction in place, so not suspending.", module);
return null;
}
if (Debug.warningOn()) {
Debug.logWarning("No transaction in place, so not suspending.", module);
}
return null;
} catch (SystemException e) {
throw new GenericTransactionException("System error, could not suspend transaction", e);
}
@@ -414,9 +418,7 @@ public final class TransactionUtil implements Status {
txMgr.resume(parentTx);
removeSuspendedTransaction(parentTx);
}
} catch (InvalidTransactionException e) {
throw new GenericTransactionException("System error, could not resume transaction", e);
} catch (SystemException e) {
} catch (InvalidTransactionException | SystemException e) {
throw new GenericTransactionException("System error, could not resume transaction", e);
}
}
@@ -577,7 +579,7 @@ public final class TransactionUtil implements Status {
public static List<Transaction> getSuspendedTxStack() {
List<Transaction> tl = suspendedTxStack.get();
if (tl == null) {
tl = new LinkedList<Transaction>();
tl = new LinkedList<>();
suspendedTxStack.set(tl);
}
return tl;
@@ -586,7 +588,7 @@ public final class TransactionUtil implements Status {
public static List<Exception> getSuspendedTxLocationsStack() {
List<Exception> tl = suspendedTxLocationStack.get();
if (tl == null) {
tl = new LinkedList<Exception>();
tl = new LinkedList<>();
suspendedTxLocationStack.set(tl);
}
return tl;
@@ -611,9 +613,8 @@ public final class TransactionUtil implements Status {
stls.remove(0);
}
return tl.remove(0);
} else {
return null;
}
return null;
}
protected static void removeSuspendedTransaction(Transaction t) {
@@ -635,7 +636,7 @@ public final class TransactionUtil implements Status {
// use the ThreadLocal one because it is more reliable than the all threads Map
List<Exception> el = transactionBeginStackSave.get();
if (el == null) {
el = new LinkedList<Exception>();
el = new LinkedList<>();
transactionBeginStackSave.set(el);
}
el.add(0, e);
@@ -644,7 +645,7 @@ public final class TransactionUtil implements Status {
Long curThreadId = Thread.currentThread().getId();
List<Exception> ctEl = allThreadsTransactionBeginStackSave.get(curThreadId);
if (ctEl == null) {
ctEl = new LinkedList<Exception>();
ctEl = new LinkedList<>();
allThreadsTransactionBeginStackSave.put(curThreadId, ctEl);
}
ctEl.add(0, e);
@@ -664,30 +665,28 @@ public final class TransactionUtil implements Status {
List<Exception> el = transactionBeginStackSave.get();
if (UtilValidate.isNotEmpty(el)) {
return el.remove(0);
} else {
return null;
}
return null;
}
public static int getTransactionBeginStackSaveSize() {
List<Exception> el = transactionBeginStackSave.get();
if (el != null) {
return el.size();
} else {
return 0;
}
return 0;
}
public static List<Exception> getTransactionBeginStackSave() {
List<Exception> el = transactionBeginStackSave.get();
List<Exception> elClone = new LinkedList<Exception>();
List<Exception> elClone = new LinkedList<>();
elClone.addAll(el);
return elClone;
}
public static Map<Long, List<Exception>> getAllThreadsTransactionBeginStackSave() {
Map<Long, List<Exception>> attbssMap = allThreadsTransactionBeginStackSave;
Map<Long, List<Exception>> attbssMapClone = new HashMap<Long, List<Exception>>();
Map<Long, List<Exception>> attbssMapClone = new HashMap<>();
attbssMapClone.putAll(attbssMap);
return attbssMapClone;
}
@@ -745,10 +744,9 @@ public final class TransactionUtil implements Status {
Exception e2 = new Exception("Current Stack Trace");
Debug.logWarning(e2, "In clearTransactionBeginStack no stack placeholder was in place, here is the current location: ", module);
return null;
} else {
transactionBeginStack.set(null);
return e;
}
transactionBeginStack.set(null);
return e;
}
public static Exception getTransactionBeginStack() {
@@ -791,7 +789,7 @@ public final class TransactionUtil implements Status {
private static void pushSetRollbackOnlyCauseSave(RollbackOnlyCause e) {
List<RollbackOnlyCause> el = setRollbackOnlyCauseSave.get();
if (el == null) {
el = new LinkedList<RollbackOnlyCause>();
el = new LinkedList<>();
setRollbackOnlyCauseSave.set(el);
}
el.add(0, e);
@@ -801,9 +799,8 @@ public final class TransactionUtil implements Status {
List<RollbackOnlyCause> el = setRollbackOnlyCauseSave.get();
if (UtilValidate.isNotEmpty(el)) {
return el.remove(0);
} else {
return null;
}
return null;
}
private static void setSetRollbackOnlyCause(String causeMessage, Throwable causeThrowable) {
@@ -824,17 +821,10 @@ public final class TransactionUtil implements Status {
private static RollbackOnlyCause clearSetRollbackOnlyCause() {
RollbackOnlyCause roc = setRollbackOnlyCause.get();
if (roc == null) {
/* this is an obnoxious message, leaving out for now; could be added manually if a problem with this is suspected
if (Debug.verboseOn()) {
// for this in particular, unlike the begin location, normally there will not be a setRollbackOnlyCause, so don't complain about it except in verbose
Debug.logVerbose(new Exception("Current Stack Trace"), "In clearSetRollbackOnlyCause no stack placeholder was in place, here is the current location: ", module);
}
*/
return null;
} else {
setRollbackOnlyCause.set(null);
return roc;
}
setRollbackOnlyCause.set(null);
return roc;
}
public static RollbackOnlyCause getSetRollbackOnlyCause() {
if (setRollbackOnlyCause.get() == null) {
@@ -971,11 +961,7 @@ public final class TransactionUtil implements Status {
}
throw t;
}
} catch (GenericEntityException e) {
throw e;
} catch (Error e) {
throw e;
} catch (RuntimeException e) {
} catch (GenericEntityException | Error | RuntimeException e) {
throw e;
} catch (Throwable t) {
throw new GenericEntityException(t);
@@ -1010,10 +996,7 @@ public final class TransactionUtil implements Status {
}
throw t;
}
} catch (Error e) {
transactionAbortCause = e;
throw e;
} catch (RuntimeException e) {
} catch (Error | RuntimeException e) {
transactionAbortCause = e;
throw e;
} catch (Throwable t) {
@@ -1039,8 +1022,9 @@ public final class TransactionUtil implements Status {
return con;
} else if (datasourceInfo.getUseProxyCursor()) {
try {
if (datasourceInfo.getResultFetchSize() > 1)
if (datasourceInfo.getResultFetchSize() > 1) {
con = CursorConnection.newCursorConnection(con, datasourceInfo.getProxyCursorName(), datasourceInfo.getResultFetchSize());
}
} catch (Exception ex) {
Debug.logWarning(ex, "Error creating the cursor connection proxy " + helperInfo.getHelperBaseName() + " datasource.", module);
}