NHibernate拦截器修改事务级别
尝试使用 SQLite 进行单元测试,效果很好,但是测试一些存储库是有问题的,因为它们使用事务。
对于使用 ReadCommited 的事务,一切都很好,但是其中一些使用 ReadUncommited,看看 System.Data.SQLite 不支持:
if ((this._defaultIsolation != IsolationLevel.Serializable) && (this._defaultIsolation != IsolationLevel.ReadCommitted))
{
throw new NotSupportedException("Invalid Default IsolationLevel specified");
}
我想知道,是否可以拦截 Session.BeginTransaction(...) 所以我可以更改事务级别,仅适用于单元测试。
更新: 我的解决方案是下载 System.Data.SQLite 的源代码并修改异常以设置默认隔离级别。我在这里写了博客:
http://www.philliphaydon。 com/2011/01/system-data-sqlite-isolationlevel-exception/
Trying to use SQLite for unit testing which is working fine, testing some of the repositories however is probomatic because they use transactions.
For the transactions that use ReadCommitted everything is fine, however some of them use ReadUncommitted which having a look at System.Data.SQLite does not support:
if ((this._defaultIsolation != IsolationLevel.Serializable) && (this._defaultIsolation != IsolationLevel.ReadCommitted))
{
throw new NotSupportedException("Invalid Default IsolationLevel specified");
}
I'm wondering, is it possible to intercept the Session.BeginTransaction(...) so i can change the transaction level, only for unit tests.
Update:
My solution was to download the sourcecode for System.Data.SQLite and modify the exceptions to set the default isolation level. I Bloggled about it here:
http://www.philliphaydon.com/2011/01/system-data-sqlite-isolationlevel-exception/
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了实现
ITransactionFactory
和ITransaction
之外,没有简单的扩展点。将调用包装到
BeginTransaction
以避免在测试时使用 ReadUncommissed 可能更容易。There aren't easy extensibility points for that, short of implementing
ITransactionFactory
andITransaction
.It's probably easier to wrap the call to
BeginTransaction
to avoid using ReadUncommitted when testing.