IGNITE-4619 .NET: TransactionScope example This closes #1492
Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/e6cc8cda Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/e6cc8cda Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/e6cc8cda Branch: refs/heads/ignite-4436-2 Commit: e6cc8cdab85c96d7a2088d3a5267f3f42039601b Parents: 26ee9c2 Author: Pavel Tupitsyn <[email protected]> Authored: Wed Feb 8 13:15:08 2017 +0300 Committer: Pavel Tupitsyn <[email protected]> Committed: Wed Feb 8 13:15:08 2017 +0300 ---------------------------------------------------------------------- .../Apache.Ignite.Examples.csproj | 1 + .../Datagrid/TransactionExample.cs | 75 +++++++++++++++----- 2 files changed, 59 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/e6cc8cda/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj index ebf9e92..c3ea378 100644 --- a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj +++ b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Apache.Ignite.Examples.csproj @@ -49,6 +49,7 @@ </Reference> <Reference Include="System" /> <Reference Include="System.Core" /> + <Reference Include="System.Transactions" /> </ItemGroup> <ItemGroup> <Compile Include="Compute\ClosureExample.cs" /> http://git-wip-us.apache.org/repos/asf/ignite/blob/e6cc8cda/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Datagrid/TransactionExample.cs ---------------------------------------------------------------------- diff --git a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Datagrid/TransactionExample.cs b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Datagrid/TransactionExample.cs index f90cf96..83d08ff 100644 --- a/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Datagrid/TransactionExample.cs +++ b/modules/platforms/dotnet/examples/Apache.Ignite.Examples/Datagrid/TransactionExample.cs @@ -18,7 +18,9 @@ namespace Apache.Ignite.Examples.Datagrid { using System; + using System.Transactions; using Apache.Ignite.Core; + using Apache.Ignite.Core.Cache; using Apache.Ignite.Core.Cache.Configuration; using Apache.Ignite.Core.Transactions; using Apache.Ignite.ExamplesDll.Binary; @@ -59,18 +61,9 @@ namespace Apache.Ignite.Examples.Datagrid AtomicityMode = CacheAtomicityMode.Transactional }); - // Clean up caches on all nodes before run. - cache.Clear(); + InitAccounts(cache); - // Initialize. - cache.Put(1, new Account(1, 100)); - cache.Put(2, new Account(2, 200)); - - Console.WriteLine(); - Console.WriteLine(">>> Accounts before transfer: "); - Console.WriteLine(">>> " + cache.Get(1)); - Console.WriteLine(">>> " + cache.Get(2)); - Console.WriteLine(); + Console.WriteLine("\n>>> Transferring with Ignite transaction API..."); // Transfer money between accounts in a single transaction. using (var tx = cache.Ignite.GetTransactions().TxStart(TransactionConcurrency.Pessimistic, @@ -88,18 +81,66 @@ namespace Apache.Ignite.Examples.Datagrid tx.Commit(); } - Console.WriteLine(">>> Transfer finished."); + DisplayAccounts(cache); - Console.WriteLine(); - Console.WriteLine(">>> Accounts after transfer: "); - Console.WriteLine(">>> " + cache.Get(1)); - Console.WriteLine(">>> " + cache.Get(2)); - Console.WriteLine(); + InitAccounts(cache); + + Console.WriteLine("\n>>> Transferring with TransactionScope API..."); + + // Do the same transaction with TransactionScope API. + using (var ts = new TransactionScope()) + { + Account acc1 = cache.Get(1); + Account acc2 = cache.Get(2); + + acc1.Balance += 100; + acc2.Balance -= 100; + + cache.Put(1, acc1); + cache.Put(2, acc2); + + ts.Complete(); + } + + DisplayAccounts(cache); } Console.WriteLine(); Console.WriteLine(">>> Example finished, press any key to exit ..."); Console.ReadKey(); } + + /// <summary> + /// Displays accounts. + /// </summary> + private static void DisplayAccounts(ICache<int, Account> cache) + { + Console.WriteLine(">>> Transfer finished."); + + Console.WriteLine(); + Console.WriteLine(">>> Accounts after transfer: "); + Console.WriteLine(">>> " + cache.Get(1)); + Console.WriteLine(">>> " + cache.Get(2)); + Console.WriteLine(); + } + + /// <summary> + /// Initializes account balance. + /// </summary> + private static void InitAccounts(ICache<int, Account> cache) + { + // Clean up caches on all nodes before run. + cache.Clear(); + + // Initialize. + cache.Put(1, new Account(1, 100)); + cache.Put(2, new Account(2, 200)); + + Console.WriteLine(); + Console.WriteLine(">>> Accounts before transfer: "); + Console.WriteLine(">>> " + cache.Get(1)); + Console.WriteLine(">>> " + cache.Get(2)); + Console.WriteLine(); + } } }
