同一事务范围内的RavenDB和MsSql导致WebException

时间:2014-07-18 15:00:15

标签: sql-server ravendb transactionscope system.net.webexception

在回复stackoverflow RavenDB does not play nicely with Transaction Scope上的另一个问题时,我创建了一个测试用例,我将我的事务隔离起来进行存储和读取。在我的测试用例中,我在同一个事务中调用了一个乌鸦文档存储和一个mssql数据库。但我得到一个网络例外。任何想法为什么或如何解决它。 以下是我的代码

    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而另一个调用可能是raven,而不知道其他调用甚至是

0 个答案:

没有答案