Nhibernate的Session和StatelessSession性能比较

Posted Jesai的技术博客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nhibernate的Session和StatelessSession性能比较相关的知识,希望对你有一定的参考价值。

NhibernateSessionStatelessSession性能比较

作者:Jesai

一个月入30K的大神有一天跟我说:我当年在你现在这个阶段,还在吊儿郎当呢!所以你努力吧!

有时候,一个想法就是1000秒,另一个想法只需要10秒

 

前言:

近段时间忙着给一个政府机关推送数据到国家数据库,数据量一共加起来有六十几万吧。这么多数据使用人工推送显然是一个不小的工作量,所以必须要使用程序来处理代码。为了方便,我们选择使用Nhibernate框架来进行CURD操作。有人大呼脑残,哈哈哈···为了方便偷懒,就是什么事情都敢做。六十几万,也算是一个大数据了吧。写程序可能才使用了三天左右的时间,然后就是跑数据。结果跑数据跑了两天多。1000条数据使用了1分钟左右。当时时间也很紧急,没有想太多。只好硬着头皮日夜加班跑数据。

这段时间回来公司有空闲,后面还要继续推送数据。于是领导就交代我一个任务,想一个跑数据更快捷的方案。

 

首先想到的是使用原生的ADO。但是,我又不甘心写太多代码,我在想为什么NHIBERNATE会这么慢?究竟是什么原因。查了一番资料。终于找到了可行的方案。自己顺便做了一个实验。证实此方案可行。原来是NHIBERNATE 的Session和StateLessSession这两个的原因。

 

测试环境:

Windows7

Access

Hibernate4

数据量:20000数据

 

首先看Session实现代码

 

 

  1 using System;
  2 
  3 using System.Collections.Generic;
  4 
  5 using System.Linq;
  6 
  7 using System.Text;
  8 
  9 using System.Threading.Tasks;
 10 
 11 using NHibernate;
 12 
 13 using NHibernate.Cfg;
 14 
 15  
 16 
 17 namespace BDC.Framework
 18 
 19 {
 20 
 21     public class DataSourceFactoryStateless
 22 
 23     {
 24 
 25         private static Dictionary<string, IStatelessSession> staticSessionDictionary = new Dictionary<string, IStatelessSession>();
 26 
 27         private static object lockObject = new object();
 28 
 29         private const  string OracleAssembly = "BDC.ZcServer";
 30 
 31         private const string AccessAssembly = "BDC.Standard";
 32 
 33  
 34 
 35         public static IStatelessSession GetSession<T>() where T:class
 36 
 37         {
 38 
 39             string assembly = typeof(T).Assembly.GetName().Name;
 40 
 41             IStatelessSession _session = null;
 42 
 43             bool isFind = false;
 44 
 45             if (staticSessionDictionary.Keys.Contains(assembly)) { _session = staticSessionDictionary[assembly]; isFind = true; }
 46 
 47             try
 48 
 49             {
 50 
 51                 if(_session == null)
 52 
 53                 {
 54 
 55                     lock (lockObject)
 56 
 57                     {
 58 
 59                         ISessionFactory factory = null;
 60 
 61                         switch (assembly) {
 62 
 63                             case OracleAssembly:
 64 
 65                                 factory = LoadOracleConfig();
 66 
 67                                 break;
 68 
 69                             case AccessAssembly:
 70 
 71                                 factory = LoadAccessConfig();
 72 
 73                                 break;
 74 
 75                             default:
 76 
 77                                 throw new Exception("没有找到对应的程序集");
 78 
 79                         }
 80 
 81                         // _session = factory.OpenSession();
 82 
 83                         _session=factory.OpenStatelessSession();
 84 
 85                         if (isFind) { staticSessionDictionary[assembly] = _session; }
 86 
 87                         else { staticSessionDictionary.Add(assembly, _session); }
 88 
 89                     }
 90 
 91                 }
 92 
 93  
 94 
 95                 return _session;
 96 
 97             }catch(Exception ex)
 98 
 99             {
100 
101                 throw new Exception("数据库初始化失败");
102 
103             }
104 
105         }
106 
107  
108 
109         private static ISessionFactory LoadOracleConfig()
110 
111         {
112 
113             Configuration config = new Configuration();
114 
115             config.SetProperty("dialect", "NHibernate.Dialect.Oracle10gDialect");
116 
117             config.SetProperty("hibernate.show_sql", "true");
118 
119             config.SetProperty("connection.driver_class", "NHibernate.Driver.OracleManagedDataClientDriver");
120 
121             config.SetProperty("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
122 
123             config.SetProperty("proxyfactory.factory_class", "NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate");
124 
125             config.SetProperty("connection.isolation", "ReadCommitted");
126 
127              config.SetProperty("connection.release_mode", "auto");
128 
129             config.SetProperty("adonet.batch_size", "500");
130 
131             config.SetProperty("current_session_context_class", "call");
132 
133             config.SetProperty("cache.use_query_cache", "false");
134 
135             config.AddAssembly("BDC.ZcServer");
136 
137             config.SetProperty("connection.connection_string", OracleConnectionString.ConnectionString);
138 
139  
140 
141             return config.BuildSessionFactory();
142 
143         }
144 
145  
146 
147         private static ISessionFactory LoadAccessConfig()
148 
149         {
150 
151             Configuration config = new Configuration();
152 
153             config.SetProperty("dialect", "NHibernate.JetDriver.JetDialect, NHibernate.JetDriver");
154 
155             config.SetProperty("hibernate.show_sql", "true");
156 
157             config.SetProperty("connection.driver_class", "NHibernate.JetDriver.JetDriver, NHibernate.JetDriver");
158 
159             config.SetProperty("connection.provider","NHibernate.Connection.DriverConnectionProvider");
160 
161             config.SetProperty("proxyfactory.factory_class", "NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate");
162 
163             config.SetProperty("connection.isolation", "ReadCommitted");
164 
165             config.SetProperty("connection.release_mode", "auto");
166 
167             config.SetProperty("adonet.batch_size", "500");
168 
169             config.SetProperty("current_session_context_class", "call");
170 
171             config.SetProperty("cache.use_query_cache", "false");
172 
173             config.AddAssembly("BDC.Standard");
174 
175             config.SetProperty("connection.connection_string", AccessConnectionString.ConnectionString);
176 
177  
178 
179             return config.BuildSessionFactory();
180 
181         }
182 
183  
184 
185  
186 
187     }
188 
189 }

 

 

 

 

Session使用时间

 

 

 

StatelessSession实现代码:

 

  1 using System;
  2 
  3 using System.Collections.Generic;
  4 
  5 using System.Linq;
  6 
  7 using System.Text;
  8 
  9 using System.Threading.Tasks;
 10 
 11 using NHibernate;
 12 
 13 using NHibernate.Cfg;
 14 
 15  
 16 
 17 namespace BDC.Framework
 18 
 19 {
 20 
 21     public class DataSourceFactory
 22 
 23     {
 24 
 25         private static Dictionary<string, ISession> staticSessionDictionary = new Dictionary<string, ISession>();
 26 
 27         private static object lockObject = new object();
 28 
 29         private const  string OracleAssembly = "BDC.ZcServer";
 30 
 31         private const string AccessAssembly = "BDC.Standard";
 32 
 33  
 34 
 35         public static ISession GetSession<T>() where T:class
 36 
 37         {
 38 
 39             string assembly = typeof(T).Assembly.GetName().Name;
 40 
 41             ISession _session = null;
 42 
 43             bool isFind = false;
 44 
 45             if (staticSessionDictionary.Keys.Contains(assembly)) { _session = staticSessionDictionary[assembly]; isFind = true; }
 46 
 47             try
 48 
 49             {
 50 
 51                 if(_session == null)
 52 
 53                 {
 54 
 55                     lock (lockObject)
 56 
 57                     {
 58 
 59                         ISessionFactory factory = null;
 60 
 61                         switch (assembly) {
 62 
 63                             case OracleAssembly:
 64 
 65                                 factory = LoadOracleConfig();
 66 
 67                                 break;
 68 
 69                             case AccessAssembly:
 70 
 71                                 factory = LoadAccessConfig();
 72 
 73                                 break;
 74 
 75                             default:
 76 
 77                                 throw new Exception("没有找到对应的程序集");
 78 
 79                         }
 80 
 81                         _session = factory.OpenSession();
 82 
 83                         if (isFind) { staticSessionDictionary[assembly] = _session; }
 84 
 85                         else { staticSessionDictionary.Add(assembly, _session); }
 86 
 87                     }
 88 
 89                 }
 90 
 91  
 92 
 93                 return _session;
 94 
 95             }catch(Exception ex)
 96 
 97             {
 98 
 99                 throw new Exception("数据库初始化失败");
100 
101             }
102 
103         }
104 
105  
106 
107         private static ISessionFactory LoadOracleConfig()
108 
109         {
110 
111             Configuration config = new Configuration();
112 
113             config.SetProperty("dialect", "NHibernate.Dialect.Oracle10gDialect");
114 
115             config.SetProperty("hibernate.show_sql", "true");
116 
117             config.SetProperty("connection.driver_class", "NHibernate.Driver.OracleManagedDataClientDriver");
118 
119             config.SetProperty("connection.provider", "NHibernate.Connection.DriverConnectionProvider");
120 
121             config.SetProperty("proxyfactory.factory_class", "NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate");
122 
123             config.SetProperty("connection.isolation", "ReadCommitted");
124 
125              config.SetProperty("connection.release_mode", "auto");
126 
127             config.SetProperty("adonet.batch_size", "500");
128 
129             config.SetProperty("current_session_context_class", "call");
130 
131             config.SetProperty("cache.use_query_cache", "false");
132 
133             config.AddAssembly("BDC.ZcServer");
134 
135             config.SetProperty("connection.connection_string", OracleConnectionString.ConnectionString);
136 
137  
138 
139             return config.BuildSessionFactory();
140 
141         }
142 
143  
144 
145         private static ISessionFactory LoadAccessConfig()
146 
147         {
148 
149             Configuration config = new Configuration();
150 
151             config.SetProperty("dialect", "NHibernate.JetDriver.JetDialect, NHibernate.JetDriver");
152 
153             config.SetProperty("hibernate.show_sql", "true");
154 
155             config.SetProperty("connection.driver_class", "NHibernate.JetDriver.JetDriver, NHibernate.JetDriver");
156 
157             config.SetProperty("connection.provider","NHibernate.Connection.DriverConnectionProvider");
158 
159             config.SetProperty("proxyfactory.factory_class", "NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate");
160 
161             config.SetProperty("connection.isolation", "ReadCommitted");
162 
163             config.SetProperty("connection.release_mode", "auto");
164 
165             config.SetProperty("adonet.batch_size", "500");
166 
167             config.SetProperty("current_session_context_class", "call");
168 
169             config.SetProperty("cache.use_query_cache", "false");
170 
171             config.AddAssembly("BDC.Standard");
172 
173             config.SetProperty("connection.connection_string", AccessConnectionString.ConnectionString);
174 
175  
176 
177             return config.BuildSessionFactory();
178 
179         }
180 
181  
182 
183  
184 
185     }
186 
187 }
188 
189  

 

 

StatelessSession使用时间

 

 

 

 

 

 

ADO执行原生SQL

 

 1 using System;
 2 
 3 using System.Collections.Generic;
 4 
 5 using System.Data.OleDb;
 6 
 7 using System.Linq;
 8 
 9 using System.Text;
10 
11 using System.Threading.Tasks;
12 
13  
14 
15 namespace BDC.Standard
16 
17 {
18 
19     public class AccessConnectionTest
20 
21     {
22 
23         public bool AccessTest()
24 
25         {
26 
27             OleDbConnection mycon = null;
28 
29             OleDbCommand mycom = null;
30 
31             try {
32 
33                 string strcon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\XXXXXXSXB.mdb;";
34 
35                 mycon = new OleDbConnection(strcon);
36 
37                 mycom = mycon.CreateCommand();
38 
39                 mycon.Open();
40 
41               
42 
43                 for( int j= 0; j < 20000; j++)
44 
45                 {
46 
47  
48 
49  
50 
51  
52 
53                     string sql = string.Format("insert into sqr(QLRDLJG,Q

以上是关于Nhibernate的Session和StatelessSession性能比较的主要内容,如果未能解决你的问题,请参考以下文章

DB2 session.get() 上的 NHibernate 抛出 System.IndexOutOfRangeException

更改 NHibernate Session.Save 命令超时

NHibernate ISession Flush:何时何地使用它,为啥?

NHibernate之"Could not initialize proxy - no Session" Bug

NHibernate中的Clear和Flush方法

NHibernate的Session.delete()不能更新到数据库。请问是啥情况。