Permalink
Browse files

ISPN-2136 lock() call misuse should ignore fail silent flag

  • Loading branch information...
1 parent 504abab commit c636b9103ee18ed7c83db486a2d9efe968d49744 @galderz galderz committed with Mircea Markus Jul 10, 2012
@@ -26,7 +26,7 @@
* Thrown when operations on {@link Cache} fail unexpectedly.
* <p/>
* Specific subclasses such as {@link org.infinispan.util.concurrent.TimeoutException} and {@link
- * org.infinispan.CacheConfigurationException.ConfigurationException} have more specific uses.
+ * org.infinispan.config.ConfigurationException} have more specific uses.
* <p/>
* Transactions: if a CacheException (including any subclasses) is thrown for an operation on a JTA transaction, then
* the transaction is marked for rollback.
@@ -0,0 +1,43 @@
+/*
+ * Copyright 2012 Red Hat, Inc. and/or its affiliates.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+
+package org.infinispan;
+
+/**
+ * Thrown when client makes cache usage errors. Situations like this include
+ * when clients invoke operations on the cache that are not allowed.
+ *
+ * @author Galder Zamarreño
+ * @since 5.2
+ */
+public class InvalidCacheUsageException extends CacheException {
+
+ public InvalidCacheUsageException(Throwable cause) {
+ super(cause);
+ }
+
+ public InvalidCacheUsageException(String msg) {
+ super(msg);
+ }
+
+ public InvalidCacheUsageException(String msg, Throwable cause) {
+ super(msg, cause);
+ }
+
+}
@@ -24,6 +24,7 @@
import org.infinispan.CacheException;
+import org.infinispan.InvalidCacheUsageException;
import org.infinispan.commands.VisitableCommand;
import org.infinispan.commands.control.LockControlCommand;
import org.infinispan.context.Flag;
@@ -130,6 +131,8 @@ private Object handleAll(InvocationContext ctx, VisitableCommand command) throws
try {
return invokeNextInterceptor(ctx, command);
+ } catch (InvalidCacheUsageException ex) {
+ throw ex; // Propagate back client usage errors regardless of flag
} catch (Throwable th) {
// If we are shutting down there is every possibility that the invocation fails.
suppressExceptions = suppressExceptions || shuttingDown;
@@ -24,6 +24,7 @@
package org.infinispan.interceptors.locking;
import org.infinispan.CacheException;
+import org.infinispan.InvalidCacheUsageException;
import org.infinispan.commands.read.GetKeyValueCommand;
import org.infinispan.commands.write.EvictCommand;
import org.infinispan.commands.write.PutKeyValueCommand;
@@ -127,7 +128,8 @@ public final Object visitEvictCommand(InvocationContext ctx, EvictCommand comman
private void assertNonTransactional(InvocationContext ctx) {
//this only happens if the cache is used in a transaction's scope
if (ctx.isInTxScope()) {
- throw new CacheException("This is a non-transactional cache and cannot be accessed with a transactional InvocationContext.");
+ throw new InvalidCacheUsageException(
+ "This is a non-transactional cache and cannot be accessed with a transactional InvocationContext.");
}
}
}
@@ -24,6 +24,7 @@
package org.infinispan.interceptors.locking;
import org.infinispan.CacheException;
+import org.infinispan.InvalidCacheUsageException;
import org.infinispan.commands.AbstractVisitor;
import org.infinispan.commands.control.LockControlCommand;
import org.infinispan.commands.read.GetKeyValueCommand;
@@ -197,7 +198,8 @@ public Object visitClearCommand(InvocationContext ctx, ClearCommand command) thr
@Override
public Object visitLockControlCommand(TxInvocationContext ctx, LockControlCommand command) throws Throwable {
- throw new CacheException("Explicit locking is not allowed with optimistic caches!");
+ throw new InvalidCacheUsageException(
+ "Explicit locking is not allowed with optimistic caches!");
}
private class LockAcquisitionVisitor extends AbstractVisitor {
@@ -25,18 +25,24 @@
import org.infinispan.CacheException;
import org.infinispan.config.Configuration;
+import org.infinispan.context.Flag;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.test.SingleCacheManagerTest;
import org.infinispan.test.fwk.TestCacheManagerFactory;
import org.infinispan.transaction.LockingMode;
import org.testng.annotations.Test;
+import java.util.concurrent.Callable;
+
+import static org.infinispan.test.TestingUtil.withTx;
+
/**
* @author Mircea Markus
* @since 5.1
*/
@Test (groups = "functional", testName = "lock.ExplicitLockingAndOptimisticCachesTest")
public class ExplicitLockingAndOptimisticCachesTest extends SingleCacheManagerTest {
+
@Override
protected EmbeddedCacheManager createCacheManager() throws Exception {
final Configuration c = getDefaultStandaloneConfig(true);
@@ -45,12 +51,35 @@ protected EmbeddedCacheManager createCacheManager() throws Exception {
}
public void testExplicitLockingNotWorkingWithOptimisticCaches() throws Exception {
- tm().begin();
- try {
- cache.getAdvancedCache().lock("a");
- assert false;
- } catch (CacheException e) {
- //expected
- }
+ // Also provide guarantees that the transaction will come to an end
+ withTx(tm(), new Callable<Object>() {
+ @Override
+ public Object call() throws Exception {
+ try {
+ cache.getAdvancedCache().lock("a");
+ assert false;
+ } catch (CacheException e) {
+ // expected
+ }
+ return null;
+ }
+ });
}
+
+ public void testExplicitLockingOptimisticCachesFailSilent() throws Exception {
+ // Also provide guarantees that the transaction will come to an end
+ withTx(tm(), new Callable<Object>() {
+ @Override
+ public Object call() throws Exception {
+ try {
+ cache.getAdvancedCache().withFlags(Flag.FAIL_SILENTLY).lock("a");
+ assert false : "Should be throwing an exception in spite of fail silent";
+ } catch (CacheException e) {
+ // expected
+ }
+ return null;
+ }
+ });
+ }
+
}

0 comments on commit c636b91

Please sign in to comment.