diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/transactions/TransactionLocalContext.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/transactions/TransactionLocalContext.java index 1b1890ecb58c..7f8cdcdea9d9 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/transactions/TransactionLocalContext.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/transactions/TransactionLocalContext.java @@ -64,8 +64,14 @@ private TransactionLocalContext(String globalTransactionId, String url, String p this.rollbackOnlyError = null; this.isTransactional = true; this.transactionId = ValueCreator.createArrayValue(globalTransactionId.getBytes()); - transactionResourceManager.transactionInfoMap.put(ByteBuffer.wrap(transactionId.getBytes().clone()), - infoRecord); + validateAndPutTransactionInfo(ByteBuffer.wrap(transactionId.getBytes().clone()), infoRecord); + } + + private void validateAndPutTransactionInfo(ByteBuffer transactionIdBytes, Object infoRecord) { + if (infoRecord == null) { + return; + } + transactionResourceManager.transactionInfoMap.put(transactionIdBytes, infoRecord); } public static TransactionLocalContext createTransactionParticipantLocalCtx(String globalTransactionId, @@ -228,7 +234,7 @@ public void setResourceParticipant(boolean resourceParticipant) { } public Object getInfoRecord() { - return transactionResourceManager.transactionInfoMap.get(ByteBuffer.wrap(transactionId.getBytes())); + return transactionResourceManager.getTransactionRecord(transactionId); } public boolean isTransactional() { 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 421f246d82d3..c7d06bc7f2cf 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 @@ -106,7 +106,7 @@ private TransactionResourceManager() { resourceRegistry = new HashMap<>(); committedFuncRegistry = new HashMap<>(); abortedFuncRegistry = new HashMap<>(); - transactionInfoMap = new HashMap<>(); + transactionInfoMap = new ConcurrentHashMap<>(); transactionManagerEnabled = getTransactionManagerEnabled(); if (transactionManagerEnabled) { trxRegistry = new HashMap<>(); @@ -606,6 +606,11 @@ public void notifyLocalParticipantFailure(String gTransactionId, String blockId) } public Object getTransactionRecord(BArray xid) { - return transactionInfoMap.get(ByteBuffer.wrap(xid.getBytes())); + synchronized (transactionInfoMap) { + if (transactionInfoMap.containsKey(ByteBuffer.wrap(xid.getBytes()))) { + return transactionInfoMap.get(ByteBuffer.wrap(xid.getBytes())); + } + return null; + } } }