本文整理汇总了C#中System.Transactions.CommittableTransaction.Commit方法的典型用法代码示例。如果您正苦于以下问题:C# CommittableTransaction.Commit方法的具体用法?C# CommittableTransaction.Commit怎么用?C# CommittableTransaction.Commit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Transactions.CommittableTransaction
的用法示例。
在下文中一共展示了CommittableTransaction.Commit方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ProcessData
public void ProcessData(int employeeId)
{
// There is not much processing logic here...
// We are assuming that the organization has 50 employees with sequential Employee Ids
// If the employeeId > 50, we are sending an Http Status code of 500 (Internal Server Error) back to the client
// else, we assume that the processing is successful and send an Http Status code of 200 (Successfull)
ReceiveContext receiveContext;
if (!ReceiveContext.TryGet(OperationContext.Current.IncomingMessageProperties, out receiveContext))
{
Console.WriteLine("ReceiveContext property was not found in the message...");
return;
}
if (employeeId > 50)
{
// Abandon
receiveContext.Abandon(TimeSpan.MaxValue);
}
else
{
// Complete in Transaction block.
CommittableTransaction committableTransaction = new CommittableTransaction();
Transaction.Current = committableTransaction;
try
{
receiveContext.Complete(TimeSpan.MaxValue);
committableTransaction.Commit();
}
catch
{
committableTransaction.Rollback();
// If the transaction was not completed we call Abandon explicitly which sends an Http 500 to the client
receiveContext.Abandon(TimeSpan.MaxValue);
}
}
}
示例2: ExplicitTransaction6
public void ExplicitTransaction6 ()
{
ExceptionAssert.Throws<InvalidOperationException>(
delegate
{
CommittableTransaction ct = new CommittableTransaction();
IntResourceManager irm = new IntResourceManager(1);
irm.Value = 2;
ct.Commit();
ct.Commit();
});
}
示例3: EnlistmentCounts_CommittableTransaction
public void EnlistmentCounts_CommittableTransaction()
{
var newGuid = Guid.NewGuid();
for (int i = 0; i < 5; i++)
{
using (var tx = new CommittableTransaction(new TransactionOptions() { IsolationLevel = IsolationLevel.ReadCommitted }))
using (new TxScope(tx, NullLogger.Instance)) // set ambient also!
{
System.Transactions.Transaction.Current.EnlistDurable(newGuid, new EnlistmentTracking(), EnlistmentOptions.None);
System.Transactions.Transaction.Current.EnlistDurable(newGuid, new EnlistmentTracking(), EnlistmentOptions.None);
tx.Commit();
}
Assert.That(EnlistmentTracking.EnlistmentCounts, Is.EqualTo(0).Or.EqualTo(1).Or.EqualTo(2));
}
}
示例4: Vol2_Dur1_Fail5
public void Vol2_Dur1_Fail5 ()
{
CommittableTransaction ct = new CommittableTransaction ();
IntResourceManager [] irm = new IntResourceManager [2];
irm [0] = new IntResourceManager ( 1 );
irm [1] = new IntResourceManager ( 3 );
Transaction.Current = ct;
irm [0].Type = ResourceManagerType.Durable;
irm [0].FailSPC = true;
irm [0].FailWithException = true;
for ( int i = 0; i < 2; i++ )
irm [i].UseSingle = true;
/* Durable RM irm[2] does on SPC, so
* all volatile RMs get Rollback */
using ( TransactionScope scope = new TransactionScope () ) {
irm [0].Value = 2;
irm [1].Value = 6;
scope.Complete ();
}
try {
ct.Commit ();
}
catch ( TransactionAbortedException e ) {
Assert.IsNotNull ( e.InnerException, "Expected e.InnerException == NotSupportedException, but got None" );
Assert.AreEqual ( typeof ( NotSupportedException ), e.InnerException.GetType (), "Expected e.InnerException == NotSupportedException, but got " + e.GetType () );
irm [0].Check ( 1, 0, 0, 0, 0, 0, 0, "irm [0]" );
irm [1].Check ( 0, 1, 0, 1, 0, 0, 0, "irm [1]" );
try {
ct.Commit ();
}
catch (InvalidOperationException x ) {
Assert.IsNull ( x.InnerException);
Transaction.Current = null;
return;
}
Assert.Fail ( "Should not be reached" );
}
Assert.Fail ( "Expected TransactionAbortedException" );
}
示例5: RetryOnFailure
public void RetryOnFailure()
{
using (var t = new CommittableTransaction())
using (new TxScope(t, NullLogger.Instance))
{
t.EnlistVolatile(new ThrowingResource(true), EnlistmentOptions.EnlistDuringPrepareRequired);
using (var c = GetConnection())
using (var cmd = c.CreateCommand())
{
cmd.CommandText = "SELECT TOP 1 Val FROM Thing";
var scalar = (double)cmd.ExecuteScalar();
Console.WriteLine("got val {0}", scalar);
}
try
{
t.Commit();
Assert.Fail("first commit should fail");
}
catch (ApplicationException)
{
Assert.That(t.TransactionInformation.Status, Is.EqualTo(TransactionStatus.Committed));
}
}
}
示例6: button84_Click
private void button84_Click(object sender, EventArgs e)
{
CommittableTransaction tx = null;
try
{
using (tx = new CommittableTransaction())
{
using (SqlConnection conn1 = new SqlConnection(Properties.Settings.Default.NorthwindConnectionString))
{
using (SqlCommand myCommand = new SqlCommand())
{
myCommand.Connection = conn1;
conn1.Open();
conn1.EnlistTransaction(tx);
//Restore database to near it's original condition so sample will work correctly.
myCommand.CommandText = "DELETE FROM Region WHERE (RegionID = 100) OR (RegionID = 101)";
myCommand.ExecuteNonQuery();
//Insert the first record.
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'MidWestern')";
myCommand.ExecuteNonQuery();
//測試交易失敗用
//Insert the first record.
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'MidWestern')";
myCommand.ExecuteNonQuery();
//Insert the second record.
myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'MidEastern')";
myCommand.ExecuteNonQuery();
tx.Commit();
MessageBox.Show("trans. Successfully");
}
}
}
}
catch (TransactionException ex)
{
MessageBox.Show(ex.Message);
tx.Rollback();
}
catch (Exception ex)
{
MessageBox.Show("trans. roll back " + ex.Message);
}
}
示例7: Delete
public override int Delete(DataTable dt, string fname, DataColumnCollection colinfo)
{
int rlt = 0;
string sqlmain = "delete from {0} ";
string sqlwhere = " where {0} in ({1}) ";
string sql, where, ids = "";
LastException = null;
foreach (DataRow row in dt.Rows)
{
if (dt.Columns.Contains(fname))
{
ids += "," + SqlHelper.MakeSafeFieldSql(row[fname].ToString(), colinfo[fname].ToString(), true);
}
}
ids = ids.Substring(1);
where = string.Format(sqlwhere, SqlHelper.MakeSafeFieldNameSql(fname), ids);
sql = string.Format(sqlmain, SqlHelper.MakeSafeFieldNameSql(dt.TableName)) + where;
Logger.Log(sql);
CommittableTransaction ts = new CommittableTransaction();
DbConnection conn = db.GetConnection();
conn.Open();
conn.EnlistTransaction(ts);
ExecuteResult er = db.Execute(sql, conn, ts);
rlt = er.IntRlt;
if (er.Exception != null)
{
LastException = er.Exception;
rlt = 0;
}
else
{
ts.Commit();
}
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
ts.Dispose();
return rlt;
}
示例8: ExplicitTransaction2
public void ExplicitTransaction2 ()
{
Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
CommittableTransaction ct = new CommittableTransaction ();
Transaction oldTransaction = Transaction.Current;
Transaction.Current = ct;
IntResourceManager irm = new IntResourceManager (1);
irm.Value = 2;
using (TransactionScope scope = new TransactionScope ()) {
Assert.AreEqual (ct, Transaction.Current, "#44");
/* Not calling scope.Complete
scope.Complete ();*/
}
Assert.AreEqual (TransactionStatus.Aborted, ct.TransactionInformation.Status, "#45");
Assert.AreEqual (ct, Transaction.Current, "#46");
Assert.AreEqual (1, irm.Actual, "#47");
Assert.AreEqual (1, irm.NumRollback, "#48");
irm.Check ( 0, 0, 1, 0, "irm" );
Transaction.Current = oldTransaction;
try {
ct.Commit ();
} catch (TransactionAbortedException) {
return;
}
Assert.Fail ("Commit on an aborted transaction should fail");
}
示例9: ExplicitTransaction3
public void ExplicitTransaction3 ()
{
Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
CommittableTransaction ct = new CommittableTransaction ();
Transaction oldTransaction = Transaction.Current;
Transaction.Current = ct;
IntResourceManager irm = new IntResourceManager (1);
using (TransactionScope scope = new TransactionScope (TransactionScopeOption.RequiresNew)) {
Assert.IsTrue (ct != Transaction.Current, "Scope with RequiresNew should have a new ambient transaction");
irm.Value = 3;
scope.Complete ();
}
irm.Value = 2;
Assert.AreEqual (3, irm.Actual, "#50");
Assert.AreEqual (ct, Transaction.Current, "#51");
ct.Commit ();
Assert.AreEqual (2, irm.Actual, "#52");
Transaction.Current = oldTransaction;
}
示例10: ExplicitTransactionCommit
public void ExplicitTransactionCommit ()
{
Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
CommittableTransaction ct = new CommittableTransaction ();
Transaction oldTransaction = Transaction.Current;
Transaction.Current = ct;
IntResourceManager irm = new IntResourceManager (1);
irm.Value = 2;
ct.Commit ();
Assert.AreEqual (2, irm.Value, "#33");
Assert.AreEqual (TransactionStatus.Committed, ct.TransactionInformation.Status, "#34");
Transaction.Current = oldTransaction;
}
示例11: ExplicitTransaction1
public void ExplicitTransaction1 ()
{
Assert.IsNull (Transaction.Current, "Ambient transaction exists (before)");
CommittableTransaction ct = new CommittableTransaction ();
Transaction oldTransaction = Transaction.Current;
Transaction.Current = ct;
IntResourceManager irm = new IntResourceManager (1);
irm.Value = 2;
using (TransactionScope scope = new TransactionScope ()) {
Assert.AreEqual (ct, Transaction.Current, "#38");
irm.Value = 4;
scope.Complete ();
}
Assert.AreEqual (ct, Transaction.Current, "#39");
Assert.AreEqual (TransactionStatus.Active, Transaction.Current.TransactionInformation.Status, "#40");
Assert.AreEqual (1, irm.Actual, "#41"); /* Actual value */
ct.Commit ();
Assert.AreEqual (4, irm.Actual, "#42"); /* New committed actual value */
Assert.AreEqual (TransactionStatus.Committed, Transaction.Current.TransactionInformation.Status, "#43");
Transaction.Current = oldTransaction;
}
示例12: ExplicitTransaction16
public void ExplicitTransaction16 ()
{
CommittableTransaction ct = new CommittableTransaction ();
IntResourceManager irm0 = new IntResourceManager ( 3 );
IntResourceManager irm = new IntResourceManager ( 1 );
Assert.IsNull ( Transaction.Current );
Transaction.Current = ct;
irm.FailPrepare = true;
irm.FailWithException = true;
irm.Value = 2;
irm0.Value = 6;
try {
ct.Commit ();
} catch (TransactionAbortedException e) {
Assert.IsNotNull ( e.InnerException, "Expected an InnerException of type NotSupportedException" );
Assert.AreEqual ( typeof (NotSupportedException), e.InnerException.GetType (), "Inner exception should be NotSupportedException" );
irm.Check ( 1, 0, 0, 0, "irm" );
irm0.Check ( 0, 0, 1, 0, "irm0" );
Transaction.Current = null;
return;
}
Assert.Fail ( "Should not be reached" );
}
示例13: Learn
public void Learn(Stream words)
{
var wordStream = new WordStream(words);
LinkedList<string> wordHistory = new LinkedList<string>();
for (int i = 0; i < 4; i++)
{
wordHistory.AddLast(String.Empty);
}
foreach (var word in wordStream)
{
using (var transaction = new CommittableTransaction())
{
wordHistory.RemoveFirst();
wordHistory.AddLast(word);
HandleWord(word);
HandleTwoGram(wordHistory.AsEnumerable().Skip(2).Take(2).ToList());
var gram = HandleThreeGram(wordHistory.AsEnumerable().Skip(1).Take(3).ToList());
HandleFourGram(wordHistory.AsEnumerable().ToList(), gram);
context.SaveChanges();
transaction.Commit();
}
}
}
示例14: ExplicitTransaction6a
public void ExplicitTransaction6a ()
{
ExceptionAssert.Throws<InvalidOperationException>(
delegate
{
CommittableTransaction ct = new CommittableTransaction();
IntResourceManager irm = new IntResourceManager(1);
irm.Value = 2;
ct.Commit();
/* Using a already committed transaction in a new
* TransactionScope
*/
TransactionScope scope = new TransactionScope(ct);
});
}
示例15: ExplicitTransaction13
public void ExplicitTransaction13 ()
{
CommittableTransaction ct = new CommittableTransaction ();
IntResourceManager irm = new IntResourceManager ( 1 );
Assert.IsNull ( Transaction.Current );
Transaction.Current = ct;
irm.Value = 2;
irm.FailPrepare = true;
try {
ct.Commit ();
} catch ( TransactionAbortedException ) {
Assert.AreEqual ( TransactionStatus.Aborted, ct.TransactionInformation.Status );
try {
ct.BeginCommit ( null, null );
} catch (Exception) {
Transaction.Current = null;
return;
}
Assert.Fail ( "Should not be reached(2)" );
}
Assert.Fail ("Should not be reached");
}