Return-Path: X-Original-To: apmail-ignite-commits-archive@minotaur.apache.org Delivered-To: apmail-ignite-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 14DAC18D8B for ; Thu, 9 Jul 2015 14:55:46 +0000 (UTC) Received: (qmail 609 invoked by uid 500); 9 Jul 2015 14:55:46 -0000 Delivered-To: apmail-ignite-commits-archive@ignite.apache.org Received: (qmail 580 invoked by uid 500); 9 Jul 2015 14:55:46 -0000 Mailing-List: contact commits-help@ignite.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@ignite.incubator.apache.org Delivered-To: mailing list commits@ignite.incubator.apache.org Received: (qmail 566 invoked by uid 99); 9 Jul 2015 14:55:45 -0000 Received: from Unknown (HELO spamd1-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 09 Jul 2015 14:55:45 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd1-us-west.apache.org (ASF Mail Server at spamd1-us-west.apache.org) with ESMTP id 881DAD34C8 for ; Thu, 9 Jul 2015 14:55:45 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd1-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.472 X-Spam-Level: * X-Spam-Status: No, score=1.472 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RP_MATCHES_RCVD=-0.329, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-us-west.apache.org ([10.40.0.8]) by localhost (spamd1-us-west.apache.org [10.40.0.7]) (amavisd-new, port 10024) with ESMTP id 6uUe7F7UNMpB for ; Thu, 9 Jul 2015 14:55:30 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-us-west.apache.org (ASF Mail Server at mx1-us-west.apache.org) with SMTP id 643192154C for ; Thu, 9 Jul 2015 14:55:28 +0000 (UTC) Received: (qmail 97717 invoked by uid 99); 9 Jul 2015 14:55:28 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 09 Jul 2015 14:55:28 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 1DA1DE6837; Thu, 9 Jul 2015 14:55:28 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sboikov@apache.org To: commits@ignite.incubator.apache.org Date: Thu, 09 Jul 2015 14:55:41 -0000 Message-Id: <5bfdca13785440efac618272f568040e@git.apache.org> In-Reply-To: <1fbc140702914b4b9a1bc70f0193e81d@git.apache.org> References: <1fbc140702914b4b9a1bc70f0193e81d@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [15/50] incubator-ignite git commit: # IGNITE-591 Don't use static ThreadLocal. # IGNITE-591 Don't use static ThreadLocal. Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/53143355 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/53143355 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/53143355 Branch: refs/heads/ignite-1085 Commit: 53143355dc713f2af3308f5ceef9fcc6e3d3d2a1 Parents: bac6f48 Author: sevdokimov Authored: Thu Jul 2 16:38:10 2015 +0300 Committer: sevdokimov Committed: Thu Jul 2 16:38:10 2015 +0300 ---------------------------------------------------------------------- .../processors/cache/jta/CacheJtaManager.java | 122 ++++++++++++------- 1 file changed, 77 insertions(+), 45 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/53143355/modules/jta/src/main/java/org/apache/ignite/internal/processors/cache/jta/CacheJtaManager.java ---------------------------------------------------------------------- diff --git a/modules/jta/src/main/java/org/apache/ignite/internal/processors/cache/jta/CacheJtaManager.java b/modules/jta/src/main/java/org/apache/ignite/internal/processors/cache/jta/CacheJtaManager.java index 2a078dc..c5a94bc 100644 --- a/modules/jta/src/main/java/org/apache/ignite/internal/processors/cache/jta/CacheJtaManager.java +++ b/modules/jta/src/main/java/org/apache/ignite/internal/processors/cache/jta/CacheJtaManager.java @@ -17,23 +17,34 @@ package org.apache.ignite.internal.processors.cache.jta; -import org.apache.ignite.*; -import org.apache.ignite.cache.jta.*; -import org.apache.ignite.configuration.*; -import org.apache.ignite.internal.processors.cache.transactions.*; -import org.jetbrains.annotations.*; - -import javax.transaction.*; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.cache.jta.CacheTmLookup; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.TransactionConfiguration; +import org.apache.ignite.internal.processors.cache.transactions.IgniteInternalTx; +import org.jetbrains.annotations.Nullable; + +import javax.transaction.RollbackException; +import javax.transaction.SystemException; +import javax.transaction.Transaction; +import javax.transaction.TransactionManager; +import java.lang.ref.WeakReference; +import java.util.Map; +import java.util.WeakHashMap; /** * Implementation of {@link CacheJtaManagerAdapter}. */ public class CacheJtaManager extends CacheJtaManagerAdapter { /** */ - private final static ThreadLocal xaRsrc = new ThreadLocal<>(); + private static final Map>> threadLocals = + new WeakHashMap<>(); + + /** */ + private volatile TransactionManager jtaTm; /** */ - private TransactionManager jtaTm; + private ThreadLocal> xaRsrc; /** */ private CacheTmLookup tmLookup; @@ -54,55 +65,76 @@ public class CacheJtaManager extends CacheJtaManagerAdapter { /** {@inheritDoc} */ @Override public void checkJta() throws IgniteCheckedException { + TransactionManager jtaTm = this.jtaTm; + if (jtaTm == null) { try { jtaTm = tmLookup.getTm(); + + if (jtaTm == null) + return; + + synchronized (threadLocals) { + if (this.jtaTm != null) + jtaTm = this.jtaTm; + else { + xaRsrc = threadLocals.get(jtaTm); + + if (xaRsrc == null) { + xaRsrc = new ThreadLocal<>(); + + threadLocals.put(jtaTm, xaRsrc); + } + + this.jtaTm = jtaTm; + } + } } catch (Exception e) { throw new IgniteCheckedException("Failed to get transaction manager: " + e, e); } } - if (jtaTm != null) { - GridCacheXAResource rsrc = xaRsrc.get(); - - if (rsrc == null || rsrc.isFinished()) { - try { - Transaction jtaTx = jtaTm.getTransaction(); - - if (jtaTx != null) { - IgniteInternalTx tx = cctx.tm().userTx(); - - if (tx == null) { - TransactionConfiguration tCfg = cctx.kernalContext().config() - .getTransactionConfiguration(); - - tx = cctx.tm().newTx( - /*implicit*/false, - /*implicit single*/false, - null, - tCfg.getDefaultTxConcurrency(), - tCfg.getDefaultTxIsolation(), - tCfg.getDefaultTxTimeout(), - /*store enabled*/true, - /*tx size*/0 - ); - } - - rsrc = new GridCacheXAResource(tx, cctx.kernalContext()); + WeakReference rsrcRef = xaRsrc.get(); - if (!jtaTx.enlistResource(rsrc)) - throw new IgniteCheckedException("Failed to enlist XA resource to JTA user transaction."); + GridCacheXAResource rsrc = rsrcRef == null ? null : rsrcRef.get(); - xaRsrc.set(rsrc); + if (rsrc == null || rsrc.isFinished()) { + try { + Transaction jtaTx = jtaTm.getTransaction(); + + if (jtaTx != null) { + IgniteInternalTx tx = cctx.tm().userTx(); + + if (tx == null) { + TransactionConfiguration tCfg = cctx.kernalContext().config() + .getTransactionConfiguration(); + + tx = cctx.tm().newTx( + /*implicit*/false, + /*implicit single*/false, + null, + tCfg.getDefaultTxConcurrency(), + tCfg.getDefaultTxIsolation(), + tCfg.getDefaultTxTimeout(), + /*store enabled*/true, + /*tx size*/0 + ); } + + rsrc = new GridCacheXAResource(tx, cctx.kernalContext()); + + if (!jtaTx.enlistResource(rsrc)) + throw new IgniteCheckedException("Failed to enlist XA resource to JTA user transaction."); + + xaRsrc.set(new WeakReference<>(rsrc)); } - catch (SystemException e) { - throw new IgniteCheckedException("Failed to obtain JTA transaction.", e); - } - catch (RollbackException e) { - throw new IgniteCheckedException("Failed to enlist XAResource to JTA transaction.", e); - } + } + catch (SystemException e) { + throw new IgniteCheckedException("Failed to obtain JTA transaction.", e); + } + catch (RollbackException e) { + throw new IgniteCheckedException("Failed to enlist XAResource to JTA transaction.", e); } } }