我正在使用Sybase ASE 16并尝试在.Net中创建不受信任的事务。以下是我的代码适用于SQL Server但不适用于Sybase ASE数据库:
public void DistTransaction()
{
try
{
var op = new TransactionOptions
{
IsolationLevel = IsolationLevel.ReadCommitted,
Timeout = TimeSpan.FromSeconds(60)
};
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.RequiresNew,op))
{
Transaction tx1 = Transaction.Current;
var tx = (System.EnterpriseServices.ITransaction)(TransactionInterop.GetDtcTransaction(Transaction.Current));
using (AseConnection conn1 = new AseConnection(connstring))
{
using (AseConnection conn2 = new AseConnection(connstring))
{
conn1.Open();
conn2.Open();
conn1.EnlistDistributedTransaction(tx);
conn2.EnlistDistributedTransaction(tx);
//conn1.EnlistTransaction(tx1);
//conn2.EnlistTransaction(tx1);
using (AseCommand cmd1 = conn1.CreateCommand())
{
cmd1.CommandText = "insert into dbo.Table1(Name) values('bnm')";
cmd1.ExecuteNonQuery();
}
using (AseCommand cmd2 = conn2.CreateCommand())
{
cmd2.CommandText = "insert into dbo.Table2(Id) values(45)";
cmd2.ExecuteNonQuery();
}
}
}
scope.Complete();
}
}
catch (Exception ex)
{
string msg = ex.Message;
}
}
第二个查询失败,它应该回滚第一个查询,但它在Sybase ASE中没有。我也在Sybase中启用了DTM,但没有区别。
有什么想法吗?
答案 0 :(得分:0)
快速诊断太复杂了...但我建议单步执行程序并观察systransactions等系统表,看看开放的交易是否按预期出现。