【问题标题】:release_mode, Pooling, Max Pool size for InMemory SQLite with FluentNHibernaterelease_mode, Pooling, Max Pool size for In Memory SQLite with Fluent NHibernate
【发布时间】:2012-11-08 20:10:16
【问题描述】:

我在使用 Sqlite 时遇到了一些问题。

我有一个具有 CPF 字段的课程 - 类似于美国的 SSN。作为业务规则,CPF 在系统中必须是唯一的。

所以我决定检查具有该字段的类。现在可能这里有代码异味:我用 ORM 检查这是否是一个冲突的 CPF。

    private CPF cpf;
    public virtual CPF CPF
    {
        get { return cpf; }
        set
        {
            if (this.ormCreated) //Do not check if it is loaded from the DB. Otherwise, it loops, generating a StackOverflow exception
            {
                cpf = value;
            }
            else
            {
                this.setNewCpf(value);
            }
        }
    }

    private void setNewCpf(CPF newCpf)
    {
        if (this.cpf == newCpf)
        {
            return;
        }

        if (Helper.Orm.IsConflictingCpf(newCpf))
        {
            throw new ConflictingCpfException();
        }
        else
        {
            cpf = newCpf;
        }
    }

这是 ORM Helper 类上的实现。

    bool OrmHelper.IsConflictingCpf(CPF cpf)
    {
        int? cpfNumber = cpf.NumeroSemDV;
        if (cpfNumber.HasValue)
        {
            var teste = findByCpfNumber<Client>(cpf);
            return 
                (
                findByCpfNumber<Client>(cpf) != null ||
                findByCpfNumber<Adversary>(cpf) != null
                );
        }
        else
        {
            //CPFSemDV = Nullable 
            return false;
        }
    }

    private PersonType findByCpfNumber<PersonType> (CPF cpf) where PersonType : PessoaFisica
    {
        int? cpfNumber = cpf.NumeroSemDV;
        using (var session = this.NewSession())
        using (var transaction = session.BeginTransaction())
        {
            try
            {
                var person = session.Query<PersonType>()
                    .Where(c => c.CPF.NumeroSemDV == cpfNumber)
                    .FirstOrDefault<PersonType>();
                return person; 
            }
            catch (Exception) { transaction.Rollback(); }
            finally 
               { 
                  session.Close();
               }
        }
        return null;
    }

问题发生在我的测试中。我正在使用 FluentNHibernate 和内存 SQLite。

    protected override FluentConfiguration PersistenceProvider 
    { 
        get 
        {
            return Fluently
                .Configure()
                .Database(
                    SQLiteConfiguration
                    .Standard
                    .InMemory()
                    .ShowSql()
                    );
        } 
    }

这是失败的测试。

    protected override void Given()
    {
        base.Given();
        var clients = new List<Client>();

        Client client1 = new Client("Luiz Angelo Heinzen")
        {
            Capaz = true,
            CPF = new CPF(18743509),
            eMail = "lah@furb.br"
        };

        session.Save(client1);
        session.Evict(client1);
    }

    [Then]
    public void Motherfaker()
    {
        Client fromDb;
        var clientsFromDb = session.Query<Client>()
            .Where(c => c.eMail == "lah@furb.br");
        fromDb = clientsFromDb.FirstOrDefault<Client>();

        Assert.AreEqual(fromDb.FullName, "Luiz Angelo Heinzen");

    }

失败的原因是什么?一开始它失败了,因为该表不存在。在内存中,sqlite 会破坏每个新会话的模式。所以我更改了代码以在 NewSession() 上返回相同的会话。但现在它因 NHibernate 异常而失败:会话已关闭。我已经测试过,如果从这里更改 findByCpfNumber

    private PersonType findByCpfNumber<PersonType> (CPF cpf) where PersonType : PessoaFisica
    {
        int? cpfNumber = cpf.NumeroSemDV;
        using (var session = this.NewSession())
        using (var transaction = session.BeginTransaction())
        {
            try
            {
                var person = session.Query<PersonType>()
                    .Where(c => c.CPF.NumeroSemDV == cpfNumber)
                    .FirstOrDefault<PersonType>();
                return person; 
            }
            catch (Exception) { transaction.Rollback(); }
            finally 
               { 
                  session.Close();
               }
        }
        return null;
    }

到这里

    private PersonType findByCpfNumber<PersonType> (CPF cpf) where PersonType : PessoaFisica
    {
        int? cpfNumber = cpf.NumeroSemDV;
        //using (var session = this.NewSession())
        var session = this.NewSession();
        using (var transaction = session.BeginTransaction())
        {
            try
            {
                var person = session.Query<PersonType>()
                    .Where(c => c.CPF.NumeroSemDV == cpfNumber)
                    .FirstOrDefault<PersonType>();
                return person; 
            }
            catch (Exception) { transaction.Rollback(); }
            finally 
               { 
                  //session.Close();
                  this.CloseSession(session);
               }
        }
        this.CloseSession(session);
        return null;
    }

错误不再发生。显然,我必须实现 CloseSession 方法。它将关闭生产数据库上的会话,如果正在使用 Sqlite,它将什么也不做。

但我宁愿以某种方式配置 SQLite,它不会处理会话。我已经阅读了here 关于 release_mode、Pooling 和 Max Pool 属性的内容。但我似乎无法在 FluentNHibernate 中找到它,所以甚至无法测试它是否有效。我克隆了 FluentNHibernate,它似乎将 release_mode 设置为 on_close,但这无济于事。

我什至尝试过:

    public override ISession NewSession()
    {
        if (this.session == null)
        {
            if (sessionFactory == null)
            {
                CreateSessionFactory();
            }
            this.session = sessionFactory.OpenSession();
        }
        if (!session.IsOpen)
        {
            sessionFactory.OpenSession(session.Connection);
            session.Connection.Open();
        }
        return session;
    }

但它一直告诉我会话已关闭。那么,有人对如何解决这个问题有任何建议吗?

还是这臭到无法拯救?

我希望这已经足够清楚了。请原谅我的错误:我来自巴西,不是以英语为母语的人。

谢谢,

路易斯安吉洛。

【问题讨论】:

    标签: sqlite fluent-nhibernate in-memory-database


    【解决方案1】:

    我遇到了同样的问题。发生的事情是,当连接关闭时,内存中的 SQLite 将删除整个模式。如果您为所有测试创建一个会话,它将保留所有其他会话的结构。

    有关代码和更完整的解释,请查看此答案:Random error when testing with NHibernate on an in-Memory SQLite db

    【讨论】:

      【解决方案2】:

      我会在系统中创建 CPF 时检查唯一性,并在数据库中有一个额外的唯一性约束来强制执行。然后,如果您将每个对 CPF 的引用设置为无级联(默认为无),则无法将新创建的重复 CPF 分配给实体并毫无例外地保存,因此不会意外发生。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-31
        • 1970-01-01
        • 1970-01-01
        • 2010-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多