Search in sources :

Example 86 with CacheTransactionManager

use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.

the class CacheXml66DUnitTest method testMultipleTXListener.

/**
   * Tests multiple transaction listeners
   * 
   * @since GemFire 5.0
   */
@Test
public void testMultipleTXListener() throws Exception {
    CacheCreation cache = new CacheCreation();
    CacheTransactionManagerCreation txMgrCreation = new CacheTransactionManagerCreation();
    TransactionListener l1 = new MyTestTransactionListener();
    TransactionListener l2 = new MySecondTestTransactionListener();
    txMgrCreation.addListener(l1);
    txMgrCreation.addListener(l2);
    cache.addCacheTransactionManagerCreation(txMgrCreation);
    testXml(cache);
    {
        CacheTransactionManager tm = getCache().getCacheTransactionManager();
        assertEquals(Arrays.asList(new TransactionListener[] { l1, l2 }), Arrays.asList(tm.getListeners()));
        tm.removeListener(l2);
        assertEquals(Arrays.asList(new TransactionListener[] { l1 }), Arrays.asList(tm.getListeners()));
        tm.removeListener(l1);
        assertEquals(Arrays.asList(new TransactionListener[] {}), Arrays.asList(tm.getListeners()));
        tm.addListener(l1);
        assertEquals(Arrays.asList(new TransactionListener[] { l1 }), Arrays.asList(tm.getListeners()));
        tm.addListener(l1);
        assertEquals(Arrays.asList(new TransactionListener[] { l1 }), Arrays.asList(tm.getListeners()));
        tm.addListener(l2);
        assertEquals(Arrays.asList(new TransactionListener[] { l1, l2 }), Arrays.asList(tm.getListeners()));
        tm.removeListener(l1);
        assertEquals(Arrays.asList(new TransactionListener[] { l2 }), Arrays.asList(tm.getListeners()));
        tm.removeListener(l1);
        assertEquals(Arrays.asList(new TransactionListener[] { l2 }), Arrays.asList(tm.getListeners()));
        tm.initListeners(new TransactionListener[] { l1, l2 });
        assertEquals(Arrays.asList(new TransactionListener[] { l1, l2 }), Arrays.asList(tm.getListeners()));
    }
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener) CacheTransactionManagerCreation(org.apache.geode.internal.cache.xmlcache.CacheTransactionManagerCreation) CacheCreation(org.apache.geode.internal.cache.xmlcache.CacheCreation) ClientCacheCreation(org.apache.geode.internal.cache.xmlcache.ClientCacheCreation) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) Test(org.junit.Test)

Example 87 with CacheTransactionManager

use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.

the class CallbackArgDUnitTest method doTest.

private void doTest() throws CacheException {
    initOtherId();
    AttributesFactory af = new AttributesFactory();
    af.setDataPolicy(DataPolicy.REPLICATE);
    af.setScope(Scope.DISTRIBUTED_ACK);
    CacheListener cl1 = new CacheListenerAdapter() {

        public void afterCreate(EntryEvent e) {
            assertEquals(getCurrentExpectedKey(), e.getKey());
            assertEquals(callbackArg, e.getCallbackArgument());
            assertEquals(true, e.isCallbackArgumentAvailable());
        }
    };
    af.addCacheListener(cl1);
    Region r1 = createRootRegion("r1", af.create());
    Region r2 = r1.createSubregion("r2", af.create());
    r2.createSubregion("r3", af.create());
    TransactionListener tl1 = new TransactionListenerAdapter() {

        public void afterCommit(TransactionEvent e) {
            assertEquals(6, e.getEvents().size());
            ArrayList keys = new ArrayList();
            Iterator it = e.getEvents().iterator();
            while (it.hasNext()) {
                EntryEvent ee = (EntryEvent) it.next();
                keys.add(ee.getKey());
                assertEquals(callbackArg, ee.getCallbackArgument());
                assertEquals(true, ee.isCallbackArgumentAvailable());
            }
            assertEquals(CallbackArgDUnitTest.this.expectedKeys, keys);
            CallbackArgDUnitTest.this.invokeCount = 1;
        }
    };
    CacheTransactionManager ctm = getCache().getCacheTransactionManager();
    ctm.addListener(tl1);
    this.invokeCount = 0;
    this.clCount = 0;
    this.expectedKeys = Arrays.asList(new String[] { "b", "c", "a", "a2", "c2", "b2" });
    doCommitOtherVm();
    assertEquals(1, this.invokeCount);
    assertEquals(6, this.clCount);
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener) TransactionListenerAdapter(org.apache.geode.cache.util.TransactionListenerAdapter) TransactionEvent(org.apache.geode.cache.TransactionEvent) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) CacheListener(org.apache.geode.cache.CacheListener) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager)

Example 88 with CacheTransactionManager

use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.

the class ExecutionHandlerContext method executeWithTransaction.

private void executeWithTransaction(ChannelHandlerContext ctx, final Executor exec, Command command) throws Exception {
    CacheTransactionManager txm = cache.getCacheTransactionManager();
    TransactionId transactionId = getTransactionID();
    txm.resume(transactionId);
    try {
        exec.executeCommand(command, this);
    } catch (UnsupportedOperationInTransactionException e) {
        command.setResponse(Coder.getErrorResponse(this.byteBufAllocator, RedisConstants.ERROR_UNSUPPORTED_OPERATION_IN_TRANSACTION));
    } catch (TransactionException e) {
        command.setResponse(Coder.getErrorResponse(this.byteBufAllocator, RedisConstants.ERROR_TRANSACTION_EXCEPTION));
    } catch (Exception e) {
        ByteBuf response = getExceptionResponse(ctx, e);
        command.setResponse(response);
    }
    getTransactionQueue().add(command);
    transactionId = txm.suspend();
    setTransactionID(transactionId);
}
Also used : TransactionException(org.apache.geode.cache.TransactionException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) ByteBuf(io.netty.buffer.ByteBuf) DecoderException(io.netty.handler.codec.DecoderException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) IOException(java.io.IOException) TransactionException(org.apache.geode.cache.TransactionException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) UnsupportedOperationInTransactionException(org.apache.geode.cache.UnsupportedOperationInTransactionException) CacheClosedException(org.apache.geode.cache.CacheClosedException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) TransactionId(org.apache.geode.cache.TransactionId)

Example 89 with CacheTransactionManager

use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.

the class RegionProvider method getOrCreateRegion0.

private Region<?, ?> getOrCreateRegion0(ByteArrayWrapper key, RedisDataType type, ExecutionHandlerContext context, boolean addToMeta) {
    checkDataType(key, type);
    Region<?, ?> r = this.regions.get(key);
    if (r != null && r.isDestroyed()) {
        removeKey(key, type);
        r = null;
    }
    if (r == null) {
        String stringKey = key.toString();
        Lock lock = this.locks.get(stringKey);
        if (lock == null) {
            this.locks.putIfAbsent(stringKey, new ReentrantLock());
            lock = this.locks.get(stringKey);
        }
        try {
            lock.lock();
            r = regions.get(key);
            if (r == null) {
                // Can create
                boolean hasTransaction = context != null && context.hasTransaction();
                // without context
                CacheTransactionManager txm = null;
                TransactionId transactionId = null;
                try {
                    if (hasTransaction) {
                        txm = cache.getCacheTransactionManager();
                        transactionId = txm.suspend();
                    }
                    Exception concurrentCreateDestroyException = null;
                    do {
                        concurrentCreateDestroyException = null;
                        r = createRegionGlobally(stringKey);
                        try {
                            if (type == RedisDataType.REDIS_LIST) {
                                doInitializeList(key, r);
                            } else if (type == RedisDataType.REDIS_SORTEDSET) {
                                try {
                                    doInitializeSortedSet(key, r);
                                } catch (RegionNotFoundException | IndexInvalidException e) {
                                    concurrentCreateDestroyException = e;
                                }
                            }
                        } catch (QueryInvalidException e) {
                            if (e.getCause() instanceof RegionNotFoundException) {
                                concurrentCreateDestroyException = e;
                            }
                        }
                    } while (concurrentCreateDestroyException != null);
                    this.regions.put(key, r);
                    if (addToMeta) {
                        RedisDataType existingType = metaPutIfAbsent(key, type);
                        if (existingType != null && existingType != type)
                            throw new RedisDataTypeMismatchException("The key name \"" + key + "\" is already used by a " + existingType.toString());
                    }
                } finally {
                    if (hasTransaction)
                        txm.resume(transactionId);
                }
            }
        } finally {
            lock.unlock();
        }
    }
    return r;
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) IndexInvalidException(org.apache.geode.cache.query.IndexInvalidException) IndexNameConflictException(org.apache.geode.cache.query.IndexNameConflictException) IndexExistsException(org.apache.geode.cache.query.IndexExistsException) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) TransactionId(org.apache.geode.cache.TransactionId)

Example 90 with CacheTransactionManager

use of org.apache.geode.cache.CacheTransactionManager in project geode by apache.

the class TXWriterJUnitTest method testAfterCommitFailedOnTransactionWriterThrowWithJTA.

/**
   * make sure standard Cache(Listener,Writer) are not called during rollback due to transaction
   * writer throw
   */
@Test
public void testAfterCommitFailedOnTransactionWriterThrowWithJTA() throws Exception {
    installCacheListenerAndWriter();
    ((CacheTransactionManager) this.txMgr).setWriter(new TransactionWriter() {

        public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
            throw new TransactionWriterException("Rollback now!");
        }

        public void close() {
        }
    });
    installTransactionListener();
    UserTransaction userTx = (UserTransaction) this.cache.getJNDIContext().lookup("java:/UserTransaction");
    userTx.begin();
    this.region.create("key1", "value1");
    this.cbCount = 0;
    try {
        userTx.commit();
        fail("Commit should have thrown RollbackException");
    } catch (RollbackException expected) {
        assertNotNull(expected.getCause());
        assertTrue(expected.getCause() + " is not a SynchronizationCommitConflictException", expected.getCause() instanceof SynchronizationCommitConflictException);
    }
    assertEquals(0, this.cbCount);
    assertEquals(1, this.failedCommits);
    assertEquals(0, this.afterCommits);
    assertEquals(1, this.afterRollbacks);
}
Also used : UserTransaction(javax.transaction.UserTransaction) TransactionEvent(org.apache.geode.cache.TransactionEvent) TransactionWriter(org.apache.geode.cache.TransactionWriter) TransactionWriterException(org.apache.geode.cache.TransactionWriterException) SynchronizationCommitConflictException(org.apache.geode.cache.SynchronizationCommitConflictException) RollbackException(javax.transaction.RollbackException) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)120 Region (org.apache.geode.cache.Region)81 Test (org.junit.Test)77 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)53 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)52 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)51 VM (org.apache.geode.test.dunit.VM)51 LocalRegion (org.apache.geode.internal.cache.LocalRegion)46 CommitConflictException (org.apache.geode.cache.CommitConflictException)45 Host (org.apache.geode.test.dunit.Host)45 CustId (org.apache.geode.internal.cache.execute.data.CustId)37 Customer (org.apache.geode.internal.cache.execute.data.Customer)34 BucketRegion (org.apache.geode.internal.cache.BucketRegion)27 AttributesFactory (org.apache.geode.cache.AttributesFactory)26 EntryNotFoundException (org.apache.geode.cache.EntryNotFoundException)24 CacheLoaderException (org.apache.geode.cache.CacheLoaderException)23 IgnoredException (org.apache.geode.test.dunit.IgnoredException)23 UnsupportedOperationInTransactionException (org.apache.geode.cache.UnsupportedOperationInTransactionException)22 CacheWriterException (org.apache.geode.cache.CacheWriterException)21 TransactionWriterException (org.apache.geode.cache.TransactionWriterException)21