Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Is this as good as using msdtc to maintain data integrity in diffe

6 views
Skip to first unread message

Ben

unread,
Jun 3, 2010, 9:40:24 PM6/3/10
to

Ben

unread,
Jun 3, 2010, 9:40:24 PM6/3/10
to
The following codes use both msmq and sqlsserver transactions to maintain
data integrity. If there is error in either msmq or sqlserver, then both will
roll back.

Is it as good as msdtc to maintain data integrity in both the msmq and
sqlserver?

The msmq and sqlserver might be in different boxes.

private void button5_Click(object sender, EventArgs e)
{
string connString = @"Data Source=.\one;Initial
Catalog=testdb;Integrated Security=SSPI;Connect Timeout=60";

SqlConnection cn = new SqlConnection(connString);

SqlTransaction tran = null;
MessageQueueTransaction mtx = new MessageQueueTransaction();

try
{
cn.Open();

mtx.Begin();
tran = cn.BeginTransaction();

string msg = "msg 3";
string path = @".\private$\testq";
MessageQueue q = new MessageQueue(path);
q.Send(msg, mtx);

SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;
cmd.Transaction = tran;

string sql = "UPDATE TB SET createtimestamp = GETDATE(), f1
= 'test' WHERE recID = 1";
cmd.CommandText = sql;
int recAff = cmd.ExecuteNonQuery();

if (recAff == 0)
{
throw (new Exception("no rec is updated."));
}

mtx.Commit();
tran.Commit();

tbInfo.Text = "task completed :)";
}
catch (SqlException ex)
{
mtx.Abort();
if (tran != null) { tran.Rollback(); }
tbInfo.Text = ex.Message;
}
catch (Exception ex)
{
mtx.Abort();
if (tran != null) { tran.Rollback(); }
tbInfo.Text = ex.Message;
}
finally
{
cn.Close();
}
}

Thanks,
Ben


0 new messages