Skip to content

Commit

Permalink
IDEMPIERE-6125 Allow monitoring null transactions on idempiereMonitor (
Browse files Browse the repository at this point in the history
…#2332)

* IDEMPIERE-6125 Allow monitoring null transactions on idempiereMonitor

* - add warning
  • Loading branch information
CarlosRuiz-globalqss committed Apr 26, 2024
1 parent 3a73b7e commit 4d39141
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
6 changes: 6 additions & 0 deletions org.adempiere.base/src/org/compiere/db/StatementProxy.java
Expand Up @@ -25,6 +25,7 @@
import javax.sql.RowSet;

import org.adempiere.exceptions.DBException;
import org.compiere.model.SystemProperties;
import org.compiere.util.CCachedRowSet;
import org.compiere.util.CLogger;
import org.compiere.util.CStatementVO;
Expand Down Expand Up @@ -133,8 +134,11 @@ public Object invoke(Object obj, Method method, Object[] args)
}
}
Method m = p_stmt.getClass().getMethod(name, method.getParameterTypes());
String nullTrxName = null;
try
{
if (SystemProperties.isTraceNullTrxConnection() && p_vo.getTrxName() == null)
nullTrxName = Trx.registerNullTrx();
return m.invoke(p_stmt, args);
}
catch (InvocationTargetException e)
Expand All @@ -143,6 +147,8 @@ public Object invoke(Object obj, Method method, Object[] args)
}
finally
{
if (nullTrxName != null && p_vo.getTrxName() == null)
Trx.unregisterNullTrx(nullTrxName);
if (log.isLoggable(Level.FINE) && logSql != null && logOperation != null)
{
log.fine((DisplayType.getDateFormat(DisplayType.DateTime)).format(new Date(System.currentTimeMillis()))+","+logOperation+","+logSql+","+(p_vo.getTrxName() != null ? p_vo.getTrxName() : "")+" (end)");
Expand Down
11 changes: 11 additions & 0 deletions org.adempiere.base/src/org/compiere/model/SystemProperties.java
Expand Up @@ -54,6 +54,7 @@ public class SystemProperties {
private static final String PropertyFile = "PropertyFile";
private static final String PropertyHomeFile = "PropertyHomeFile";
private static final String TestOCI = "TestOCI";
private static final String TRACE_NULL_TRX_CONNECTION = "TRACE_NULL_TRX_CONNECTION";
private static final String ZK_THEME = MSysConfig.ZK_THEME;
private static final String ZkUnitTest = "ZkUnitTest";

Expand Down Expand Up @@ -257,4 +258,14 @@ public static boolean isZkUnitTest() {
return "true".equals(System.getProperty(ZkUnitTest));
}

/**
* TRACE_NULL_TRX_CONNECTION=true to allow tracing null transactions on idempiereMonitor
* WARNING! this setting can have a big performance impact, it is disabled by default
* use it with care in production just temporarily to trace problematic connection slowness or leaks
* @return
*/
public static boolean isTraceNullTrxConnection() {
return "true".equals(System.getProperty(TRACE_NULL_TRX_CONNECTION));
}

}
37 changes: 37 additions & 0 deletions org.adempiere.base/src/org/compiere/util/Trx.java
Expand Up @@ -34,6 +34,7 @@
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.exceptions.DBException;
import org.compiere.Adempiere;
import org.compiere.db.StatementProxy;
import org.compiere.model.MSysConfig;
import org.compiere.model.PO;

Expand Down Expand Up @@ -877,4 +878,40 @@ public void run() {
}, 2, TimeUnit.SECONDS);
}
}

/**
* Register a null trx
* @return
*/
public static String registerNullTrx() {
String nullTrxName = "NullTrx_" + UUID.randomUUID().toString();
Trx nullTrx = new Trx(nullTrxName);
nullTrx.trace = new Exception();
nullTrx.m_startTime = System.currentTimeMillis();
String displayName = null;
StackWalker walker = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
Optional<String> stackName = walker.walk(frames -> frames.map(
stackFrame -> stackFrame.getClassName() + "." +
stackFrame.getMethodName() + ":" +
stackFrame.getLineNumber())
.filter(f -> ! (f.startsWith(Trx.class.getName() + ".") || f.startsWith(StatementProxy.class.getName() + ".") || f.startsWith("jdk.proxy") || f.startsWith("org.compiere.util.DB.")))
.findFirst());
displayName = (stackName.orElse(null));
if (displayName != null)
nullTrx.setDisplayName(displayName);
s_cache.put(nullTrxName, nullTrx);
return nullTrxName;
}

/**
* Unregister a null trx
* @param nullTrxName
*/
public static void unregisterNullTrx(String nullTrxName) {
Trx nullTrx = s_cache.get(nullTrxName);
nullTrx.setDisplayName(null);
nullTrx.trace = null;
s_cache.remove(nullTrxName);
}

} // Trx

0 comments on commit 4d39141

Please sign in to comment.