代码改变世界

NHibernate Step By Step(10)-常用的配置属性

2010-08-26 23:57  Clingingboy  阅读(994)  评论(0编辑  收藏  举报

概况

(1)connection.driver_class 数据访问驱动
(2)dialect sql方言
(3)
connection.connection_string 数据连接字符串
(4)connection.provider 
数据库连接驱动
(5)
connection.release_mode 连接释放模式
(6)default_entity_mode 实体表现模式



1.connection.driver_class

<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>\
用于配置数据访问驱动,
1若.net类库中有的,则用强类型,如SqlConnection,OldeConnection,OdbcConnection,OracleConnection.

/// <summary>
 /// Creates an uninitialized <see cref="IDbConnection" /> object for 
 /// the SqlClientDriver.
 /// </summary>
 /// <value>An unitialized <see cref="System.Data.SqlClient.SqlConnection"/> object.</value>
 public override IDbConnection CreateConnection()
 {
     return new SqlConnection();
 }
 
 /// <summary>
 /// Creates an uninitialized <see cref="IDbCommand" /> object for 
 /// the SqlClientDriver.
 /// </summary>
 /// <value>An unitialized <see cref="System.Data.SqlClient.SqlCommand"/> object.</value>
 public override IDbCommand CreateCommand()
 {
     return new System.Data.SqlClient.SqlCommand();
 }
2.若内置没有提供的,第三方提供的,则采用动态反射方式实现

public MySqlDataDriver() : base(
     "MySql.Data",
     "MySql.Data.MySqlClient.MySqlConnection",
     "MySql.Data.MySqlClient.MySqlCommand")
 {
 }

2.dialect(sql方言)

既根据不同数据库,会采用不同了sql语句,如标准的分页查询就是用dialect的GetLimitString方法实现的

<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>

3.数据连接字符串
(1)connection.connection_string
<property name="connection.connection_string">Data Source=CLINGINGBOY-PC\SQLEXPRESS;Initial Catalog=nhibernate;Integrated Security=True</property>

(2)connection.connection_string_name

从config中connectionStrings节点中获取连接字符串

4.数据库连接驱动
默认是实现IConnectionProvider接口的NHibernate.Connection.DriverConnectionProvider

<property name="connection.provider">NHibernate.Test.DebugConnectionProvider, NHibernate.Test</property>
用户打开和关闭数据库
image
实现
Driver用户创建数据库驱动

public override IDbConnection GetConnection()
 {
     log.Debug("Obtaining IDbConnection from Driver");
     IDbConnection conn = Driver.CreateConnection();
     try
     {
         conn.ConnectionString = ConnectionString;
         conn.Open();
     }
     catch (Exception)
     {
         conn.Dispose();
         throw;
     }
     
     return conn;
 }

 

public virtual void CloseConnection(IDbConnection conn)
 {
     log.Debug("Closing connection");
     try
     {
         conn.Close();
     }
     catch (Exception e)
     {
         throw new ADOException("Could not close " + conn.GetType() + " connection", e);
     }
 }

5.连接释放模式connection.release_mode

默认为auto,不必修改造成其他的错误

ConnectionReleaseMode releaseMode;
 if ("auto".Equals(releaseModeName))
 {
     releaseMode = ConnectionReleaseMode.AfterTransaction; //transactionFactory.DefaultReleaseMode;
 }
 else
 {
     releaseMode = ParseConnectionReleaseMode(releaseModeName);
 }

private static ConnectionReleaseMode ParseConnectionReleaseMode(string name)
 {
     switch (name)
     {
         case "after_statement":
             throw new HibernateException("aggressive connection release (after_statement) not supported by NHibernate");
         case "after_transaction":
             return ConnectionReleaseMode.AfterTransaction;
         case "on_close":
             return ConnectionReleaseMode.OnClose;
         default:
             throw new HibernateException("could not determine appropriate connection release mode [" + name + "]");
     }
 }

6.实体表现模式(default_entity_mode)

见此http://www.clingingboy.cn/article/53.aspx