Exemple #1
0
        public bool commit(TxInfo txInfo)
        {
            TransactionId           id        = (TransactionId)(object)txInfo.getTxId();
            bool                    commited  = false;
            CacheTransactionManager txManager = null;

            txManager = CacheHelper <TKey, TVal> .DCache.CacheTransactionManager;
            if (txManager.TryResume(id, 30000))
            {
                try
                {
                    txManager.Commit();
                    commited = true;
                }
                catch (CommitConflictException ex)
                {
                    //Expected exception with concurrent transactions.
                    Util.Log("Got expected exception {0}", ex);
                }
                catch (TransactionDataRebalancedException ex)
                {
                    FwkTest <TKey, TVal> .CurrentTest.FwkException("Got {0}", ex);
                }
                catch (TransactionDataNodeHasDepartedException ex)
                {
                    FwkTest <TKey, TVal> .CurrentTest.FwkException("Got {0}", ex);
                }
            }
            else
            {
                Util.Log("TxId {0} is not suspended in this member with tryResume time limit, cannot commit.  Expected, continuing test.");
            }
            Util.Log("Commited returning {0}", commited);
            return(commited);
        }
Exemple #2
0
        public void DoPopulateRegion()
        {
            CacheTransactionManager txManager = CacheHelper <TKey, TVal> .DCache.CacheTransactionManager;
            //TransactionId txId = CacheHelper<TKey, TVal>.DCache.CacheTransactionManager;
            IRegion <TKey, TVal> reg = GetRegion();

            for (int i = 0; i < 5; i++)
            {
                TKey key   = (TKey)(object)(i);
                TVal value = (TVal)(object)"Value_";
                txManager.Begin();
                reg[key] = value;
                txManager.Commit();
                Util.Log("The Key is={0} and value={1}", key.ToString(), value.ToString());
            }
        }
        static void Main(string[] args)
        {
            try
            {
                // Create a Geode Cache
                CacheFactory cacheFactory = CacheFactory.CreateCacheFactory();

                Cache cache = cacheFactory.Create();

                Console.WriteLine("Created the Geode cache.");

                RegionFactory regionFactory = cache.CreateRegionFactory(RegionShortcut.CACHING_PROXY);

                Console.WriteLine("Created the RegionFactory.");

                // Create the example Region
                IRegion <string, string> region = regionFactory.Create <string, string>("exampleRegion");

                Console.WriteLine("Created the region with generics support.");

                // Get the cache transaction manager from the cache.
                CacheTransactionManager txManager = cache.CacheTransactionManager;

                // Starting a transaction
                txManager.Begin();
                Console.WriteLine("Transaction started.");

                region["Key1"] = "Value1";
                region["Key2"] = "Value2";

                Console.WriteLine("Put two entries into the region");

                try {
                    // Prepare the transaction
                    txManager.Prepare();
                    Console.WriteLine("Transaction Prepared");

                    // Commit the transaction
                    txManager.Commit();
                    Console.WriteLine("Transaction Committed");
                }
                catch (CommitConflictException e)
                {
                    Console.WriteLine("CommitConflictException encountered. Exception: {0}", e.Message);
                }

                if (region.ContainsKey("Key1"))
                {
                    Console.WriteLine("Obtained the first entry from the Region");
                }

                if (region.ContainsKey("Key2"))
                {
                    Console.WriteLine("Obtained the second entry from the Region");
                }

                //start a new transaction
                txManager.Begin();
                Console.WriteLine("Transaction Started");

                // Put a new entry
                region["Key3"] = "Value3";
                Console.WriteLine("Put the third entry into the Region");

                // remove the first key
                region.Remove("Key1", null);
                Console.WriteLine("remove the first entry");

                txManager.Prepare();
                Console.WriteLine("Transaction Prepared");

                txManager.Rollback();
                Console.WriteLine("Transaction Rollbacked");

                if (region.ContainsKey("Key1"))
                {
                    Console.WriteLine("Obtained the first entry from the Region");
                }

                if (region.ContainsKey("Key2"))
                {
                    Console.WriteLine("Obtained the second entry from the Region");
                }

                if (region.ContainsKey("Key3"))
                {
                    Console.WriteLine("ERROR: Obtained the third entry from the Region.");
                }

                cache.Close();

                Console.WriteLine("Closed the Geode Cache");
            }
            // An exception should not occur
            catch (GeodeException gfex)
            {
                Console.WriteLine("Transactions Geode Exception: {0}", gfex.Message);
            }
        }
Exemple #4
0
        public void DoBasicTX()
        {
            FwkInfo("Inside ResumableTx:DoBasicTX()");
            IRegion <TKey, TVal> region = GetRegion();
            string opcode  = null;
            int    action  = 0;
            int    minNoEx = GetUIntValue("minExecutions");

            if (region == null)
            {
                FwkSevere("ResumableTx:DoBasicTX(): No region to perform operations on.");
            }
            CacheTransactionManager txManager = null;
            TransactionId           txId      = null;

            int numOfEx = 0;

            try
            {
                txManager = CacheHelper <TKey, TVal> .DCache.CacheTransactionManager;
                if (numOfEx == 0)
                {
                    FwkInfo("Begin.() tx numOfEx={0} and minNoEx={1}", numOfEx, minNoEx);
                    txManager.Begin();
                    action = BEGIN_TX;
                    doValidateTxOps(txId, action);
                }
                while (!(numOfEx > minNoEx))
                {
                    try
                    {
                        FwkInfo("numOfEx={0} and minNoEx={1}", numOfEx, minNoEx);
                        txId   = txManager.Suspend();
                        action = SUSPEND;
                        doValidateTxOps(txId, action);

                        txManager.Resume(txId);
                        int numKeys = 100;
                        Dictionary <TKey, TVal> keyValMap = new Dictionary <TKey, TVal>();
                        TKey key;
                        TVal value;
                        for (int i = 0; i < numKeys; i++)
                        {
                            string keyName = String.Format("key_{0}", i);
                            key               = (TKey)(object)keyName.ToString();
                            value             = (TVal)(object)"Value_";
                            region[key]       = value;
                            keyValMap[key]    = value;
                            AfterTxMap["put"] = keyValMap;
                        }
                        action = RESUME;
                        FwkInfo("keyValMap count={0} and region count is {1}", keyValMap.Count, region.Keys.Count);
                        doValidateTxOps(txId, action);
                    }
                    catch (IllegalStateException ex)
                    {
                        FwkException("Got {0}", ex.Message);
                    }
                    catch (CommitConflictException ex)
                    {
                        FwkException("Got {0}", ex.Message);
                    }
                    numOfEx++;
                }
                if (numOfEx > minNoEx)
                {
                    try
                    {
                        txManager.Commit();
                    }
                    catch (CommitConflictException)
                    {
                        FwkInfo("Got Expected exception as there was a write conflict");
                    }
                    action = COMMIT;
                    doValidateTxOps(txId, action);
                }
            }
            catch (Exception ex)
            {
                FwkException("ResumableTx.DoEntryOpsWithTX() Caught unexpected " +
                             "exception during entry '{0}' operation: {1}.", opcode, ex);
            }
        }