在回答关于计算器另一个问题RavenDB不与事务范围内发挥很好我已经创建了一个测试情况下我孤立我的存储和读取数据。 在我的测试情况下,我想提出一个乌鸦文档存储的调用和相同的事务中的MSSQL数据库。 但我得到一个Web异常。 任何想法,为什么或如何解决它。 下面是我的代码
private static string con = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TestDB;Data Source=.";
static void Main(string[] args)
{
using (var scope = new TransactionScope())
{
additemtosql();
}
Console.WriteLine((itemSqlCount() == 0));
using (var mDocumentStore = new DocumentStore())
{
mDocumentStore.ParseConnectionString("Url=http://localhost:8081");
mDocumentStore.Initialize();
using (var scope = new TransactionScope())
{
additemtoravendb(mDocumentStore);
Console.WriteLine((itemSqlCount() == 0));
scope.Complete();
}
Console.WriteLine(itemRavenCount(mDocumentStore) == 1);
}
}
private static void additemtoravendb(IDocumentStore mDocumentStore)
{
using (var session = mDocumentStore.OpenSession())
{
var dog = new Dog()
{
Age = 10,
BirthDay = DateTime.Now,
Breed = "Dalmation",
Name = "Fluffy"
};
session.Store(dog);
session.SaveChanges();
}
}
private static int itemRavenCount(IDocumentStore mDocumentStore)
{
using (var session = mDocumentStore.OpenSession())
{
var dogs = session.Query<Dog>().ToList();
return dogs.Count();
}
}
private static void additemtosql()
{
var sql = "INSERT INTO RECORDS_TEXT VALUES (1, 'XX', 'XX') ";
using (var connection = new SqlConnection(con))
using (var command = new SqlCommand(sql, connection))
{
connection.Open();
command.ExecuteNonQuery();
Console.WriteLine((itemSqlCount() == 1));
}
}
private static int itemSqlCount()
{
using (var connection = new SqlConnection(con))
using (var command = new SqlCommand("", connection))
{
connection.Open();
command.CommandText = "SELECT COUNT(*) FROM RECORDS_TEXT";
using (var reader = command.ExecuteReader())
{
reader.Read();
var count = reader.GetInt32(0);
return count;
}
}
}
我清楚地知道,我开始一个事务范围,然后我不完成它,这就是我正在测试。 未完成事务应该回滚,如果我是对的。 我想测试一个失败的交易,然后是成功的。
为什么我打电话这里的同一个事务中ravendb和MSSQL的原因是因为我模拟可能与嵌套事务,其中一个电话可能是SQL和另一个乌鸦发生什么,不知道其他的呼叫还送。