From 0f212c0048844f0e8620b14317721881e7225ef8 Mon Sep 17 00:00:00 2001 From: ravinperera00 Date: Fri, 18 Oct 2024 21:34:38 +0530 Subject: [PATCH] Add missing methods in TransactionResourceManager --- .../transactions/TransactionConstants.java | 3 ++ .../TransactionResourceManager.java | 54 +++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/transactions/TransactionConstants.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/transactions/TransactionConstants.java index 345b8f82cac1..4cbb09926954 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/transactions/TransactionConstants.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/transactions/TransactionConstants.java @@ -70,4 +70,7 @@ public class TransactionConstants { public static final String ANN_NAME_TRX_PARTICIPANT_CONFIG = "Participant"; public static final String TIMESTAMP_OBJECT_VALUE_FIELD = "timeValue"; + + public static final int DEFAULT_TRX_AUTO_COMMIT_TIMEOUT = 120; + public static final int DEFAULT_TRX_CLEANUP_TIMEOUT = 600; } diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/transactions/TransactionResourceManager.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/transactions/TransactionResourceManager.java index b12e8a342b95..1ab246921a75 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/transactions/TransactionResourceManager.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/transactions/TransactionResourceManager.java @@ -59,6 +59,8 @@ import javax.transaction.xa.Xid; import static io.ballerina.runtime.api.constants.RuntimeConstants.BALLERINA_BUILTIN_PKG_PREFIX; +import static io.ballerina.runtime.transactions.TransactionConstants.DEFAULT_TRX_AUTO_COMMIT_TIMEOUT; +import static io.ballerina.runtime.transactions.TransactionConstants.DEFAULT_TRX_CLEANUP_TIMEOUT; import static io.ballerina.runtime.transactions.TransactionConstants.TRANSACTION_PACKAGE_ID; import static io.ballerina.runtime.transactions.TransactionConstants.TRANSACTION_PACKAGE_NAME; import static io.ballerina.runtime.transactions.TransactionConstants.TRANSACTION_PACKAGE_VERSION; @@ -85,6 +87,8 @@ public class TransactionResourceManager { private static final String ATOMIKOS_LOG_BASE_PROPERTY = "com.atomikos.icatch.log_base_dir"; private static final String ATOMIKOS_LOG_NAME_PROPERTY = "com.atomikos.icatch.log_base_name"; private static final String ATOMIKOS_REGISTERED_PROPERTY = "com.atomikos.icatch.registered"; + public static final String TRANSACTION_AUTO_COMMIT_TIMEOUT_KEY = "transactionAutoCommitTimeout"; + public static final String TRANSACTION_CLEANUP_TIMEOUT_KEY = "transactionCleanupTimeout"; private static final Logger log = LoggerFactory.getLogger(TransactionResourceManager.class); private Map> resourceRegistry; @@ -580,6 +584,56 @@ void endXATransaction(String transactionId, String transactionBlockId, boolean a } } + /** + * This method gets the user specified config for the transaction auto commit timeout. Default is 120. + * + * @return int transaction auto commit timeout value + */ + public static int getTransactionAutoCommitTimeout() { + VariableKey transactionAutoCommitTimeoutKey = new VariableKey(TRANSACTION_PACKAGE_ID, + TRANSACTION_AUTO_COMMIT_TIMEOUT_KEY, PredefinedTypes.TYPE_INT, false); + if (!ConfigMap.containsKey(transactionAutoCommitTimeoutKey)) { + return DEFAULT_TRX_AUTO_COMMIT_TIMEOUT; + } else { + Object configValue = ConfigMap.get(transactionAutoCommitTimeoutKey); + if (configValue == null) { + return DEFAULT_TRX_AUTO_COMMIT_TIMEOUT; + } + return parseTimeoutValue(configValue, DEFAULT_TRX_AUTO_COMMIT_TIMEOUT); + } + } + + /** + * This method gets the user specified config for cleaning up dead transactions. Default is 600. + * + * @return int transaction cleanup after value + */ + public static int getTransactionCleanupTimeout() { + VariableKey transactionCleanupTimeoutKey = new VariableKey(TRANSACTION_PACKAGE_ID, + TRANSACTION_CLEANUP_TIMEOUT_KEY, + PredefinedTypes.TYPE_INT, false); + if (!ConfigMap.containsKey(transactionCleanupTimeoutKey)) { + return DEFAULT_TRX_CLEANUP_TIMEOUT; + } else { + Object configValue = ConfigMap.get(transactionCleanupTimeoutKey); + if (configValue == null) { + return DEFAULT_TRX_CLEANUP_TIMEOUT; + } + return parseTimeoutValue(configValue, DEFAULT_TRX_CLEANUP_TIMEOUT); + } + } + + private static int parseTimeoutValue(Object configValue, int defaultValue) { + if (!(configValue instanceof Number number)) { + return defaultValue; + } + int timeoutValue = number.intValue(); + if (timeoutValue <= 0) { + return defaultValue; + } + return timeoutValue; + } + private void removeContextsFromRegistry(String transactionCombinedId, String gTransactionId) { resourceRegistry.remove(transactionCombinedId); if (transactionManagerEnabled) {