我正在处理一个与 WCF 服务中的 NHibernate 和分布式事务相关的奇怪问题。见 Deadlocks causing 'Server failed to resume the transaction' with NHibernate and distributed transactions更多细节。
似乎可以解决我的问题的一件事是使用 NHibernate 的 AdoNetTransactionFactory,而不是 AdoNetWithDistributedTransactionsFactory。
我相信 AdoNetWithDistributedTransactionsFactory 与使 NHibernate 的二级缓存机制正常工作有关,但我们没有使用它。将 AdoNetTransactionFactory 与分布式事务一起使用还存在哪些(如果有)其他问题?
谢谢你的时间!
最佳答案
我注意到您从其他问题/答案中提到:
SqlConnection class is not thread-safe, and that includes closing the connection
on a separate thread. Based on this response we have filed a
bug report for NHibernate.
但是,来自 NHibernate's documentation :
11.2. Threads and connections
在创建 NHibernate Session 时,您应该遵守以下做法:
如果您尝试与 NHibernate 建立多线程连接,那么它可能无法正常工作。您是否考虑过不同的 ORM,例如 Entity Framework ?
不管你选择什么 ORM,数据库连接都不是线程安全的。这是普遍的。
“许多数据库驱动程序不是线程安全的。使用单例意味着如果你有很多线程,它们将共享相同的连接。单例模式不会给你线程安全。它只是允许多个线程轻松共享一个“全局”实例。” - https://stackoverflow.com/a/6507820/1026459
关于nhibernate - 我可以在分布式事务中使用 NHibernate 的 AdoNetTransactionFactory 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8582206/