本文整理汇总了C#中TransactionManager.BeginTransaction方法的典型用法代码示例。如果您正苦于以下问题:C# TransactionManager.BeginTransaction方法的具体用法?C# TransactionManager.BeginTransaction怎么用?C# TransactionManager.BeginTransaction使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TransactionManager
的用法示例。
在下文中一共展示了TransactionManager.BeginTransaction方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Instead02
private void Instead02(TransactionManager tm, Customer cus)
{
tm.BeginTransaction();
DataRepository.CustomerProvider.Insert(tm, cus);
tm.Commit();
// Or, show the message box after the transaction completed,
// e.g. to notify them of an event after saving
MessageBox.Show("Something...");
}
示例2: Instead01
private void Instead01(TransactionManager tm, Customer cus)
{
MessageBox.Show("Something...");
// Begin the transaction after the message box,
// e.g. if you want to prompt them to do something before saving.
tm.BeginTransaction();
DataRepository.CustomerProvider.Insert(tm, cus);
tm.Commit();
}
示例3: BadlyMisplacedCode
// SAMPLE 01
//
// As mentioned in "C01_TransactionManager," the idea behind transactions
// is that it should surround a block of code that either saves everything
// or nothing.
//
// Here is the thing to remember: While the transaction is open it will prevent
// anything from accessing the database tables that you are busy with. This is
// to prevent "dirty" or incomplete records.
//
// The caveat: you want your transaction to execute as fast as possible. Be
// like a ninja: get in, do your thing, and get out.
//
private void BadlyMisplacedCode(TransactionManager tm, Customer cus)
{
tm.BeginTransaction();
DataRepository.CustomerProvider.Insert(tm, cus);
MessageBox.Show("Something...");
tm.Commit();
// Can you see it?
// This code will block the Customer table on the entire branch until the user
// clicks "OK" on the message box. The impact of this is enourmous.
}
示例4: TransactionManagerRollsbackOnDispose
public void TransactionManagerRollsbackOnDispose()
{
var seed = new Seed32();
using (var manager = new TransactionManager<int, MockClassA>() )
{
manager.TransactionCommitted += new TransactionCommit<int, MockClassA>
(delegate(ITransaction<int, MockClassA> transaction)
{
transaction.MarkComplete();
Assert.Fail();
});
var testEntities = TestResourceFactory.GetMockClassAObjects(3).ToList();
testEntities.ForEach(e => e.Id = seed.Increment());
using (var tLock1 = manager.BeginTransaction())
{
foreach (var entity in testEntities)
tLock1.Transaction.Enlist(Action.Create, entity.Id, entity);
tLock1.Transaction.Dispose();
Assert.IsTrue(tLock1.Transaction.IsComplete);
}
}
}
示例5: TransactionManagerRollsbackAll
public void TransactionManagerRollsbackAll()
{
var seed = new Seed32();
using (var manager = new TransactionManager<int, MockClassA>() )
{
manager.TransactionCommitted += new TransactionCommit<int, MockClassA>
(delegate(ITransaction<int, MockClassA> transaction)
{
transaction.MarkComplete();
Assert.Fail();
});
var testEntities = TestResourceFactory.GetMockClassAObjects(3).ToList();
testEntities.ForEach(e => e.Id = seed.Increment());
using (var tLock1 = manager.BeginTransaction())
{
foreach (var entity in testEntities)
tLock1.Transaction.Enlist(Action.Create, entity.Id, entity);
using (var tLock2 = manager.BeginTransaction())
{
testEntities = TestResourceFactory.GetMockClassAObjects(3).ToList();
testEntities.ForEach(e => e.Id = seed.Increment());
foreach (var entity in testEntities)
tLock2.Transaction.Enlist(Action.Create, entity.Id, entity);
using (var tLock3 = manager.BeginTransaction())
{
testEntities = TestResourceFactory.GetMockClassAObjects(3).ToList();
testEntities.ForEach(e => e.Id = seed.Increment());
foreach (var entity in testEntities)
tLock3.Transaction.Enlist(Action.Create, entity.Id, entity);
using (var tLock4 = manager.BeginTransaction())
{
testEntities = TestResourceFactory.GetMockClassAObjects(3).ToList();
testEntities.ForEach(e => e.Id = seed.Increment());
foreach (var entity in testEntities)
tLock4.Transaction.Enlist(Action.Create, entity.Id, entity);
using (var tLock5 = manager.BeginTransaction())
{
testEntities = TestResourceFactory.GetMockClassAObjects(3).ToList();
testEntities.ForEach(e => e.Id = seed.Increment());
foreach (var entity in testEntities)
tLock5.Transaction.Enlist(Action.Create, entity.Id, entity);
manager.RollBackAll(true);
while (manager.HasActiveTransactions)
Thread.Sleep(100);
Assert.IsTrue(tLock5.Transaction.IsComplete);
}
Assert.IsTrue(tLock4.Transaction.IsComplete);
}
Assert.IsTrue(tLock3.Transaction.IsComplete);
}
Assert.IsTrue(tLock2.Transaction.IsComplete);
}
Assert.IsTrue(tLock1.Transaction.IsComplete);
}
}
}
示例6: TransactionManagerCommitsChildTransactions
public void TransactionManagerCommitsChildTransactions()
{
var seed = new Seed32();
var hits = 0;
using (var manager = new TransactionManager<int, MockClassA>() )
{
manager.TransactionCommitted += new TransactionCommit<int, MockClassA>
(delegate(ITransaction<int, MockClassA> transaction)
{
Assert.AreEqual(3, transaction.GetEnlistedActions().Count());
hits++;
transaction.MarkComplete();
});
var testEntities = TestResourceFactory.GetMockClassAObjects(3).ToList();
testEntities.ForEach(e => e.Id = seed.Increment());
using (var tLock1 = manager.BeginTransaction())
{
foreach (var entity in testEntities)
tLock1.Transaction.Enlist(Action.Create, entity.Id, entity);
using (var tLock2 = manager.BeginTransaction())
{
testEntities = TestResourceFactory.GetMockClassAObjects(3).ToList();
testEntities.ForEach(e => e.Id = seed.Increment());
foreach (var entity in testEntities)
tLock2.Transaction.Enlist(Action.Create, entity.Id, entity);
using (var tLock3 = manager.BeginTransaction())
{
testEntities = TestResourceFactory.GetMockClassAObjects(3).ToList();
testEntities.ForEach(e => e.Id = seed.Increment());
foreach (var entity in testEntities)
tLock3.Transaction.Enlist(Action.Create, entity.Id, entity);
using (var tLock4 = manager.BeginTransaction())
{
testEntities = TestResourceFactory.GetMockClassAObjects(3).ToList();
testEntities.ForEach(e => e.Id = seed.Increment());
foreach (var entity in testEntities)
tLock4.Transaction.Enlist(Action.Create, entity.Id, entity);
using (var tLock5 = manager.BeginTransaction())
{
testEntities = TestResourceFactory.GetMockClassAObjects(3).ToList();
testEntities.ForEach(e => e.Id = seed.Increment());
foreach (var entity in testEntities)
tLock5.Transaction.Enlist(Action.Create, entity.Id, entity);
tLock1.Transaction.Commit();
}
}
}
}
}
}
Assert.AreEqual(5, hits);
}
示例7: TransactionManagerCommitAllCommitsAllTransactions
public void TransactionManagerCommitAllCommitsAllTransactions()
{
var sync = new object();
var seed = new Seed32();
var hits = 0;
using (var manager = new TransactionManager<int, MockClassA>())
{
manager.TransactionCommitted += new TransactionCommit<int, MockClassA>
(delegate(ITransaction<int, MockClassA> transaction)
{
Assert.AreEqual(3, transaction.GetEnlistedActions().Count());
hits++;
transaction.MarkComplete();
});
var transList = new List<TransactionLock<int, MockClassA>>();
Parallel.For(0, 5, delegate(int i)
{
var testEntities = TestResourceFactory.GetMockClassAObjects(3).ToList();
testEntities.ForEach(e => e.Id = seed.Increment());
var trans = manager.BeginTransaction();
foreach (var entity in testEntities)
trans.Transaction.Enlist(Action.Create, entity.Id, entity);
if (i == 1)
{
lock (sync)
transList.Add(trans);
Assert.AreEqual(3, manager.GetCached().Count());
}
else
trans.Transaction.Commit();
});
manager.CommitAll(true);
Assert.AreEqual(0, manager.GetCached().Count());
}
Assert.AreEqual(5, hits);
}
示例8: TransactionLockAutoCommitsTransactions
public void TransactionLockAutoCommitsTransactions()
{
var seed = new Seed32();
int committed = 0;
using (var manager = new TransactionManager<int, MockClassA>() )
{
manager.TransactionCommitted += new TransactionCommit<int, MockClassA>
(delegate(ITransaction<int, MockClassA> transaction)
{
Assert.AreEqual(3, transaction.GetEnlistedActions().Count());
committed += transaction.GetEnlistedActions().Count();
transaction.MarkComplete();
});
var testEntities = TestResourceFactory.GetMockClassAObjects(3).ToList();
testEntities.ForEach(e => e.Id = seed.Increment());
using (var tLock1 = manager.BeginTransaction())
{
foreach (var entity in testEntities)
tLock1.Transaction.Enlist(Action.Create, entity.Id, entity);
using (var tLock2 = manager.BeginTransaction())
{
testEntities = TestResourceFactory.GetMockClassAObjects(3).ToList();
testEntities.ForEach(e => e.Id = seed.Increment());
foreach (var entity in testEntities)
tLock2.Transaction.Enlist(Action.Create, entity.Id, entity);
using (var tLock3 = manager.BeginTransaction())
{
testEntities = TestResourceFactory.GetMockClassAObjects(3).ToList();
testEntities.ForEach(e => e.Id = seed.Increment());
foreach (var entity in testEntities)
tLock3.Transaction.Enlist(Action.Create, entity.Id, entity);
using (var tLock4 = manager.BeginTransaction())
{
testEntities = TestResourceFactory.GetMockClassAObjects(3).ToList();
testEntities.ForEach(e => e.Id = seed.Increment());
foreach (var entity in testEntities)
tLock4.Transaction.Enlist(Action.Create, entity.Id, entity);
using (var tLock5 = manager.BeginTransaction())
{
testEntities = TestResourceFactory.GetMockClassAObjects(3).ToList();
testEntities.ForEach(e => e.Id = seed.Increment());
foreach (var entity in testEntities)
tLock5.Transaction.Enlist(Action.Create, entity.Id, entity);
}
}
}
}
tLock1.Transaction.Commit();
var sw = new Stopwatch();
while (!tLock1.Transaction.IsComplete && sw.ElapsedMilliseconds < 1000)
Thread.Sleep(100);
sw.Stop();
Assert.AreEqual(15, committed);
Assert.IsTrue(tLock1.Transaction.IsComplete);
}
}
}
示例9: AfmCommitsComplexTransaction
public void AfmCommitsComplexTransaction()
{
_testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
Cleanup();
var formatter = new BSONFormatter();
var core = new FileCore<int, long>() { IdSeed = new Seed32(999), SegmentSeed = new Seed64(), Stride = 32 };
var addEntities = TestResourceFactory.GetMockClassAObjects(25).ToList();
foreach (var entity in addEntities)
entity.Id = core.IdSeed.Increment();
var updateEntities = new List<MockClassC>();
IDictionary<int, long> returnSegments = null;
using (var afm = new AtomicFileManager<MockClassA>(_testName + ".database", core, formatter))
{
afm.Load<int>();
using (var manager = new TransactionManager<int, MockClassA>
(new MockTransactionFactory<int, MockClassA>()
, new TransactionSynchronizer<int, MockClassA>()))
{
afm.Rebuilt += new Rebuild<MockClassA>(delegate(Guid transactionId, int newStride, long newLength, int newSeedStride)
{
var c = (IFileCore<int, long>)afm.Core;
c.Stride = newStride;
c.MinimumCoreStride = newSeedStride;
afm.SaveCore<int>();
});
manager.TransactionCommitted += new TransactionCommit<int, MockClassA>(
delegate(ITransaction<int, MockClassA> tranny)
{
returnSegments = afm.CommitTransaction(tranny, new Dictionary<int, long>());
tranny.MarkComplete();
});
using (var tLock = manager.BeginTransaction())
{
addEntities.ForEach(delegate(MockClassA entity)
{
tLock.Transaction.Enlist(Action.Create, entity.Id, entity);
});
tLock.Transaction.Commit();
Assert.AreEqual(25, afm.Length);
}
}
}
using (var afm = new AtomicFileManager<MockClassA>(_testName + ".database", formatter))
{
afm.Load<int>();
Assert.AreEqual(25, afm.Length);
using (var manager = new TransactionManager<int, MockClassA>
(new MockTransactionFactory<int, MockClassA>()
, new TransactionSynchronizer<int, MockClassA>()))
{
afm.Rebuilt += new Rebuild<MockClassA>(delegate(Guid transactionId, int newStride, long newLength, int newSeedStride)
{
var c = (IFileCore<int, long>)afm.Core;
c.Stride = newStride;
c.MinimumCoreStride = newSeedStride;
afm.SaveCore<int>();
});
manager.TransactionCommitted += new TransactionCommit<int, MockClassA>(
delegate(ITransaction<int, MockClassA> tranny)
{
returnSegments = afm.CommitTransaction(tranny, returnSegments);
tranny.MarkComplete();
});
using (var tLock = manager.BeginTransaction())
{
foreach (var kvp in returnSegments)
updateEntities.Add(afm.LoadSegmentFrom(kvp.Value) as MockClassC);
updateEntities.ForEach(u => u.Name = (u.Location.X + u.Location.Y).ToString() + u.Name);
updateEntities.ForEach(u => tLock.Transaction.Enlist(Action.Update, u.Id, u));
var insert = TestResourceFactory.CreateRandom().WithName("numb nugget") as MockClassC;
insert.Id = core.IdSeed.Increment();
updateEntities.Add(insert);
tLock.Transaction.Enlist(Action.Create, insert.Id, insert);
tLock.Transaction.Commit();
Assert.AreEqual(26, afm.Length);
//.........这里部分代码省略.........
示例10: AfmReturnsRightNumberOfJObjects
public void AfmReturnsRightNumberOfJObjects()
{
_testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
Cleanup();
var formatter = new BSONFormatter();
var core = new FileCore<int, long>() { IdSeed = new Seed32(999), SegmentSeed = new Seed64(), MinimumCoreStride = 512 };
var addEntities = TestResourceFactory.GetMockClassAObjects(10000).ToList();
foreach (var entity in addEntities)
entity.Id = _seed.Increment();
IDictionary<int, long> returnSegments = null;
using (var afm = new AtomicFileManager<MockClassA>(_testName + ".database", core))
{
afm.Load<int>();
using (var manager = new TransactionManager<int, MockClassA>
(new MockTransactionFactory<int, MockClassA>()
, new TransactionSynchronizer<int, MockClassA>()))
{
manager.TransactionCommitted += new TransactionCommit<int, MockClassA>(
delegate(ITransaction<int, MockClassA> tranny)
{
returnSegments = afm.CommitTransaction(tranny, new Dictionary<int, long>());
tranny.MarkComplete();
});
using (var tLock1 = manager.BeginTransaction())
{
addEntities.ForEach(delegate(MockClassA entity)
{
tLock1.Transaction.Enlist(Action.Create, entity.Id, entity);
});
tLock1.Transaction.Commit();
Assert.AreEqual(10000, afm.Length);
}
foreach (var group in afm.AsEnumerable())
{
var match = group.Where(i => i.SelectToken("Id").Value<int>() > 9999).ToList();
if (match == null || !match.Any())
continue;
var result = match.First(r => r.Value<int>("Id") == 10000).ToObject<MockClassC>();
Assert.IsNotNull(result);
Assert.AreEqual(10000, result.Id);
break;
}
}
}
}
示例11: AfmRebuildsOnFailedTransaction
public void AfmRebuildsOnFailedTransaction()
{
_testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
var formatter = new BSONFormatter();
var core = new FileCore<int, long>() { IdSeed = new Seed32(999), SegmentSeed = new Seed64(), MinimumCoreStride = 512 };
var entity = TestResourceFactory.CreateRandom() as MockClassC;
var largeEntity = TestResourceFactory.CreateRandom().WithName(new String('a', 2000)) as MockClassC;
IDictionary<int, long> returnSegments = null;
using (var fLock = new ManagedFileLock(_testName))
{
Cleanup();
using (var afm = new AtomicFileManager<MockClassA>(_testName + ".database", core, formatter))
{
afm.Load<int>();
using (var manager = new TransactionManager<int, MockClassA>
(new MockTransactionFactory<int, MockClassA>()
, new TransactionSynchronizer<int, MockClassA>()))
{
manager.TransactionCommitted += new TransactionCommit<int, MockClassA>(
delegate(ITransaction<int, MockClassA> tranny)
{
returnSegments = afm.CommitTransaction(tranny, new Dictionary<int, long>());
while (afm.FileFlushQueueActive)
Thread.Sleep(100);
tranny.MarkComplete();
afm.SaveCore<int>();
});
using (var tLock = manager.BeginTransaction())
{
tLock.Transaction.Enlist(Action.Create, entity.Id, entity);
tLock.Transaction.Commit();
}
}
}
using (var afm = new AtomicFileManager<MockClassA>(_testName + ".database", formatter))
{
afm.Load<int>();
using (var manager = new TransactionManager<int, MockClassA>
(new MockTransactionFactory<int, MockClassA>()
, new TransactionSynchronizer<int, MockClassA>()))
{
manager.TransactionCommitted += new TransactionCommit<int, MockClassA>(
delegate(ITransaction<int, MockClassA> tranny)
{
returnSegments = afm.CommitTransaction(tranny, returnSegments);
while (afm.FileFlushQueueActive)
Thread.Sleep(100);
tranny.MarkComplete();
afm.SaveCore<int>();
});
using (var tLock = manager.BeginTransaction())
{
tLock.Transaction.Enlist(Action.Update, largeEntity.Id, largeEntity);
tLock.Transaction.Commit();
}
}
}
using (var afm = new AtomicFileManager<MockClassA>(_testName + ".database", formatter))
{
afm.Load<int>();
Assert.Greater(afm.Stride, 2200);
Assert.AreEqual(1024, afm.CorePosition);
var obj = afm.LoadSegmentFrom(returnSegments.First().Value) as MockClassC;
Assert.IsNotNull(obj);
Assert.AreEqual(largeEntity.Id, obj.Id);
Assert.AreEqual(largeEntity.Name, obj.Name);
Assert.AreEqual(largeEntity.GetSomeCheckSum, obj.GetSomeCheckSum);
Assert.AreEqual(largeEntity.Location.X, obj.Location.X);
Assert.AreEqual(largeEntity.Location.Y, obj.Location.Y);
Assert.AreEqual(largeEntity.Location.Z, obj.Location.Z);
Assert.AreEqual(largeEntity.Location.W, obj.Location.W);
Assert.AreEqual(largeEntity.ReferenceCode, obj.ReferenceCode);
Assert.AreEqual(largeEntity.ReplicationID, obj.ReplicationID);
}
}
}
示例12: AfmRebuildsWithNewSeedSize
public void AfmRebuildsWithNewSeedSize()
{
_testName = MethodInfo.GetCurrentMethod().Name.GetHashCode().ToString();
Cleanup();
var formatter = new BSONFormatter();
var core = new FileCore<int, long>() { IdSeed = new Seed32(999), SegmentSeed = new Seed64(), MinimumCoreStride = 512 };
var addEntities = TestResourceFactory.GetMockClassAObjects(20480).ToList();
foreach (var entity in addEntities)
entity.Id = core.IdSeed.Increment();
var updateEntities = new List<MockClassC>();
IDictionary<int, long> returnSegments = null;
IDictionary<int, long> deletedSegments = new Dictionary<int, long>();
using (var afm = new AtomicFileManager<MockClassA>(_testName + ".database", core))
{
afm.Load<int>();
using (var manager = new TransactionManager<int, MockClassA>
(new MockTransactionFactory<int, MockClassA>()
, new TransactionSynchronizer<int, MockClassA>()))
{
manager.TransactionCommitted += new TransactionCommit<int, MockClassA>(
delegate(ITransaction<int, MockClassA> tranny)
{
returnSegments = afm.CommitTransaction(tranny, new Dictionary<int, long>());
while (afm.FileFlushQueueActive)
Thread.Sleep(100);
tranny.MarkComplete();
afm.SaveCore<int>();
});
using (var tLock = manager.BeginTransaction())
{
addEntities.ForEach(delegate(MockClassA entity)
{
tLock.Transaction.Enlist(Action.Create, entity.Id, entity);
});
Assert.AreEqual(20480, tLock.Transaction.EnlistCount);
tLock.Transaction.Commit();
Assert.AreEqual(20480, afm.Length);
}
}
}
using (var afm = new AtomicFileManager<MockClassA>(_testName + ".database", core))
{
afm.Load<int>();
Assert.AreEqual(20480, afm.Length);
using (var manager = new TransactionManager<int, MockClassA>
(new MockTransactionFactory<int, MockClassA>()
, new TransactionSynchronizer<int, MockClassA>()))
{
manager.TransactionCommitted += new TransactionCommit<int, MockClassA>(
delegate(ITransaction<int, MockClassA> tranny)
{
returnSegments = afm.CommitTransaction(tranny, returnSegments);
while (afm.FileFlushQueueActive)
Thread.Sleep(100);
tranny.MarkComplete();
afm.SaveCore<int>();
core.MinimumCoreStride = (int)formatter.FormatObjStream(core).Length;
});
using (var tLock = manager.BeginTransaction())
{
deletedSegments = returnSegments.Take(9000).ToDictionary(k => k.Key, k => k.Value);
foreach (var kvp in deletedSegments)
updateEntities.Add(afm.LoadSegmentFrom(kvp.Value) as MockClassC);
updateEntities.ForEach(u => tLock.Transaction.Enlist(Action.Delete, u.Id, u));
tLock.Transaction.Commit();
afm.SaveCore<int>();
}
Assert.AreEqual(512, afm.Stride);
//Deleting items from the database adds those idsToDelete to the segmentSeed'aqn open Ids list, increasing it'aqn size. It'aqn an easy way to check for segmentSeed rebuilding.
Assert.Greater(afm.Core.MinimumCoreStride, 23000);
}
}
}