为什么要调用NHibernate AdoTransaction的终结器?

时间:2009-06-18 08:46:03

标签: nhibernate dispose finalizer

我正在分析单位&集成测试,我发现很多时间花在了NHibernate.Transaction.AdoTransaction的终结器上 - 这意味着它没有得到正确的处理。

我没有直接在代码中使用AdoTransaction,所以它可能被NHibernate中的其他一些对象使用。知道我忘记了什么吗?

这是我的文字夹具:

public abstract class AbstractInMemoryFixture
{
    protected ISessionFactory sessionFactory;
    protected ILogger Logger { get; private set; }
    static readonly Configuration config;
    private static readonly ISessionFactory internalSessionFactory;

    static AbstractInMemoryFixture()
    {
        config = new NHibernateConfigurator().Configure(NHibernateConfigurators.SQLiteInMemory());
        internalSessionFactory = config.BuildSessionFactory();
    }

    [SetUp]
    public void SetUp()
    {
        const string sqliteInMemoryConnectionString = "Data Source=:memory:;Version=3;Pooling=False;Max Pool Size=1;";
        var con = new SQLiteConnection(sqliteInMemoryConnectionString);
        con.Open();
        new SchemaExport(config).Execute(false, true, false, true, con, System.Console.Out);
        var proxyGenerator = new ProxyGenerator();

        sessionFactory = proxyGenerator.CreateInterfaceProxyWithTarget(internalSessionFactory, new UseExistingConnectionInterceptor(con));
        Logger = new NullLogger();

        ExtraSetup();
    }

    [TearDown]
    public void TearDown()
    {
        var con = sessionFactory.OpenSession().Connection;
        if (con != null)
        {
            if (con.State == ConnectionState.Open)
                con.Close();
            con.Dispose();
        }
    }

    private class UseExistingConnectionInterceptor :IInterceptor
    {
        private readonly SQLiteConnection connection;

        public UseExistingConnectionInterceptor(SQLiteConnection connection)
        {
            this.connection = connection;
        }

        public void Intercept(IInvocation invocation)
        {
            if (invocation.Method.Name != "OpenSession" || invocation.Method.GetParameters().Length > 0)
            {
                invocation.Proceed();
                return;
            }
            var factory = (ISessionFactory) invocation.InvocationTarget;
            invocation.ReturnValue = factory.OpenSession(connection);
        }
    }
    protected virtual void ExtraSetup() { }
}

1 个答案:

答案 0 :(得分:0)

访问Sybase数据库时遇到同样的问题。我不知道为什么,也不知道问题的真正原因,但似乎\NHibernate\Transaction\AdoTransaction.cs(第307到311行)中与关闭/处理对象有关的一些代码已被禁用了一段时间。不幸的是,SVN的责备功能并没有给出太多信息:(