Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

ISPN-2865 Error calling clear() via JMX on a transactional cache with…

… autoCommit=false
  • Loading branch information...
commit 56a2c431a22dc11185e01a07bae2c1a109f2f4f2 1 parent cb43246
Mircea Markus authored tristantarrant committed
23 core/src/main/java/org/infinispan/CacheImpl.java
@@ -314,11 +314,28 @@ private V removeInternal(Object key, EnumSet<Flag> explicitFlags, InvocationCont
314 314 return (V) executeCommandAndCommitIfNeeded(ctx, command);
315 315 }
316 316
317   - @Override
  317 +
318 318 @ManagedOperation(
319   - description = "Clears the cache.",
320   - displayName = "Clears the cache."
  319 + description = "Clears the cache",
  320 + displayName = "Clears the cache", name = "clear"
321 321 )
  322 + public final void clearOperation() {
  323 + //if we have a TM then this cache is transactional.
  324 + // We shouldn't rely on the auto-commit option as it might be disabled, so always start a tm.
  325 + if (transactionManager != null) {
  326 + try {
  327 + transactionManager.begin();
  328 + clear(null, null);
  329 + transactionManager.commit();
  330 + } catch (Throwable e) {
  331 + throw new CacheException(e);
  332 + }
  333 + } else {
  334 + clear(null, null);
  335 + }
  336 + }
  337 +
  338 + @Override
322 339 public final void clear() {
323 340 clear(null, null);
324 341 }
67 core/src/test/java/org/infinispan/jmx/CacheClearTest.java
... ... @@ -0,0 +1,67 @@
  1 +/*
  2 + * JBoss, Home of Professional Open Source
  3 + * Copyright 2013 Red Hat Inc. and/or its affiliates and other contributors
  4 + * as indicated by the @author tags. All rights reserved.
  5 + * See the copyright.txt in the distribution for a
  6 + * full listing of individual contributors.
  7 + *
  8 + * This copyrighted material is made available to anyone wishing to use,
  9 + * modify, copy, or redistribute it subject to the terms and conditions
  10 + * of the GNU Lesser General Public License, v. 2.1.
  11 + * This program is distributed in the hope that it will be useful, but WITHOUT A
  12 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  13 + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  14 + * You should have received a copy of the GNU Lesser General Public License,
  15 + * v.2.1 along with this distribution; if not, write to the Free Software
  16 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  17 + * MA 02110-1301, USA.
  18 + */
  19 +
  20 +package org.infinispan.jmx;
  21 +
  22 +import org.infinispan.configuration.cache.ConfigurationBuilder;
  23 +import org.infinispan.configuration.global.GlobalConfigurationBuilder;
  24 +import org.infinispan.manager.DefaultCacheManager;
  25 +import org.infinispan.manager.EmbeddedCacheManager;
  26 +import org.infinispan.test.SingleCacheManagerTest;
  27 +import org.infinispan.test.fwk.TestCacheManagerFactory;
  28 +import org.testng.annotations.Test;
  29 +
  30 +import javax.management.MBeanServer;
  31 +import javax.management.ObjectName;
  32 +
  33 +import static org.infinispan.test.TestingUtil.getCacheObjectName;
  34 +import static org.testng.AssertJUnit.assertFalse;
  35 +import static org.testng.AssertJUnit.assertTrue;
  36 +
  37 +/**
  38 + * @author Mircea Markus
  39 + * @since 5.2
  40 + */
  41 +@Test (groups = "functional", testName = "jmx.CacheClearTest")
  42 +public class CacheClearTest extends SingleCacheManagerTest {
  43 +
  44 + public static final String JMX_DOMAIN = CacheMBeanTest.class.getSimpleName();
  45 + private MBeanServer server;
  46 +
  47 + @Override
  48 + protected EmbeddedCacheManager createCacheManager() throws Exception {
  49 + GlobalConfigurationBuilder gcb = new GlobalConfigurationBuilder();
  50 + gcb.globalJmxStatistics().jmxDomain(JMX_DOMAIN).mBeanServerLookup(new PerThreadMBeanServerLookup()).enable();
  51 + ConfigurationBuilder dcc = TestCacheManagerFactory.getDefaultCacheConfiguration(true);
  52 + dcc.transaction().autoCommit(false);
  53 + dcc.jmxStatistics().enable();
  54 + server = PerThreadMBeanServerLookup.getThreadMBeanServer();
  55 + return new DefaultCacheManager(gcb.build(), dcc.build());
  56 + }
  57 +
  58 + public void testClear() throws Exception {
  59 + ObjectName defaultOn = getCacheObjectName(JMX_DOMAIN);
  60 + tm().begin();
  61 + cache().put("k","v");
  62 + tm().commit();
  63 + assertFalse(cache().isEmpty());
  64 + server.invoke(defaultOn, "clear", new Object[]{}, new String[]{});
  65 + assertTrue(cache().isEmpty());
  66 + }
  67 +}

0 comments on commit 56a2c43

Please sign in to comment.
Something went wrong with that request. Please try again.