简体   繁体   English

流畅的NHibernate会话关闭数据库连接

[英]Fluent NHibernate sessions closes the database connection

I'm using Fluent NHibernate to write to an Oracle 11g database. 我正在使用Fluent NHibernate写入Oracle 11g数据库。 I'm not sure if it's a problem, but the NHibernate drivers have only configuration settings for 9 and 10g databases. 我不确定这是否有问题,但NHibernate驱动程序只有9和10g数据库的配置设置。 Anyways, when I had instantiated only one NHibernate SessionFactory and only one NHibernate session (a used both a regular session and a IStatelessSession). 无论如何,当我只实例化一个NHibernate SessionFactory和一个NHibernate会话时(同时使用常规会话和IStatelessSession)。 Anytime I performed a read or write to the database, the Oracle sys.aud$ table would have a log of the transactions being performed. 每当我对数据库执行读或写操作时,Oracle sys.aud $表都会记录正在执行的事务。 Our DBA said it was because the connection was logging in and then logging off after each read or write transaction. 我们的DBA说这是因为连接正在登录,然后在每次读取或写入事务后注销。 With a large amount of queries, we would end up killing the audit table. 通过大量查询,我们最终会终止审计表。 We're going to create a second database user with tweaked auditing for that account, but is it the default nature for NHibernate to close and open the connection for each transaction? 我们将创建第二个数据库用户,对该帐户进行调整审计,但NHibernate关闭并打开每个事务的连接是否为默认属性? Is there a way to prevent the connection from logging in and logging off? 有没有办法阻止连接登录和注销?

Here's the SessionFactory configuration 这是SessionFactory配置

public static ISessionFactory getSessionFactory() {
        var cfg = FluentNHibernate.Cfg.Db.OracleClientConfiguration.Oracle10;
        cfg.ConnectionString(c => {
            c.Instance(...);
            c.Username(...);
            c.Password(...);
            c.Server(...);
        });


        return Fluently.Configure().Database(cfg).Mappings(m => {
            m.FluentMappings.Add<DummyDataMap>();
        }).BuildSessionFactory();
    }

and here's the Main method for the tests I wrote 这是我写的测试的主要方法

 static void Main(string[] args) {
        try {
            var sessionFactory = getSessionFactory();

            /*using (var statelessSession = sessionFactory.OpenStatelessSession()) {
                for (int i = 0; i < 1000; i++) {
                    var dd = new DummyData();
                    dd.FIRST_COLUMN = i;
                    using (var t = statelessSession.BeginTransaction()) {
                        statelessSession.Insert(dd);
                        t.Commit();
                    }
                }
            }*/
            /*using (var statefulSession = sessionFactory.OpenSession()) {
                for (int i = 0; i < 1000; i++) {
                    var dd = new DummyData();
                    dd.FIRST_COLUMN = i;
                    using (var t = statefulSession.BeginTransaction()) {
                        statefulSession.Save(dd);
                        t.Commit();
                    }
                }
            }*/

            using (var statefulSession = sessionFactory.OpenSession()) { 
                for (int i = 0; i < 1000; i++) {
                    statefulSession.Query<DummyData>().Where(dd => dd.FIRST_COLUMN == i).ForEach(dd =>
                        Console.Out.WriteLine(dd.FIRST_COLUMN));
                }
            }
        } catch (Exception ex) {
            Console.Out.WriteLine(ex.Message);
        }

opening and closing the connection for each use case is the recommended way in ADO.NET 在ADO.NET中建议使用每个用例打开和关闭连接

Using Connections 使用连接

High performance applications keep connections to the data source in use for a minimal amount of time, as well as take advantage of performance enhancing technology such as connection pooling. 高性能应用程序可在最短的时间内保持与正在使用的数据源的连接,并利用连接池等性能增强技术。

Double check that connectionpooling is enabled and supported by the ADO.Net driver you are using. 仔细检查您正在使用的ADO.Net驱动程序是否启用并支持连接池。

If you really need to have one global connection then implement IConnectionProvider which opens a connection on first CreateConnection and hands out the created each time, however you have to make sure that no 2 databaseoperations are performed at the same time because this is not supported by the connection. 如果您确实需要一个全局连接,那么实现IConnectionProvider,它会在第一个CreateConnection上打开一个连接并分发每次创建的连接,但是您必须确保不会同时执行2个数据库操作,因为这不支持连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM