http://stackoverflow.com/questions/4335827/changing-nhibernate-connectionstring

http://stackoverflow.com/questions/491139/how-do-you-change-nhibernates-connection-string-per-http-request

http://jasondentler.com/blog/2009/11/authentication-impersonation-and-dynamic-nhibernate-connection-strings

http://www.nhforge.org/wikis/howtonh/dynamically-change-user-info-in-connection-string.aspx

Dynamically change user info in connection string

In some cases our clients has to use the same database user id in each connection, so they can use audit and security features of their database system (and their DBAs will be happy [:)]).

To do that in Nh We can use the ConnectionProvider facility. Just derive a class from the standard DriverConnectionProvider class:

public class DynamicConnectionProvider : DriverConnectionProvider
{
 private string _connectionString;
 public override void Configure(IDictionary<string, string> settings)
 {
 
  // Connection string in the configuration overrides named connection string
  if (!settings.TryGetValue(NHibernate.Cfg.Environment.ConnectionString,out _connectionString))
 _connectionString = GetNamedConnectionString(settings);
 
  if (_connectionString == null)
  {
   throw new HibernateException("Could not find connection string setting (set " 
    + NHibernate.Cfg.Environment.ConnectionString + " or " 
    + NHibernate.Cfg.Environment.ConnectionStringName + " property)");
  }
  ConfigureDriver(settings);
 }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

This is necessary because the original connection string is private, but just copy the code from base method.

The real magic is in "ConnectionString" property, it is called when nh has to connect in a Session. You have to override it so you can make the changes you need.

protected override string ConnectionString
{
 get { return FixConnectionString(_connectionString); }
}
 

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

In this case FixConnectionString read the user info from some environment variable and inject it in the connection string.

Finally configure NH to use the ConnectionProvider:

   <property name="connection.provider">
    MyAssembly.DynamicConnectionProvider, MyAssembly
   </property>

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

nhibernate change connection的更多相关文章

  1. weblogic JDBC Connection Pools--转官方文档

    http://docs.oracle.com/cd/E13222_01/wls/docs81/ConsoleHelp/jdbc_connection_pools.html#1106016 JDBC C ...

  2. BLE Hacking:使用Ubertooth one扫描嗅探低功耗蓝牙

    0×00 前言 低功耗蓝牙(Low Energy; LE),又视为Bluetooth Smart或蓝牙核心规格4.0版本.其特点具备节能.便于采用,是蓝牙技术专为物联网(Internet of Thi ...

  3. P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1

    P6 Professional Installation and Configuration Guide (Microsoft SQL Server Database) 16 R1       May ...

  4. Mysql –>EF edmx(model first)–> Sql server table

    一.mysql environment When we create an new database,first We need draw er diagram for somebody to sho ...

  5. How to import a GitHub project into Eclipse

    Assuming you have created a project in GitHub, these are the steps to import it into Eclipse. First, ...

  6. NUnit使用

    NUnit是.net平台上使用得最为广泛的测试框架之一,本文将通过示例来描述NUnit的使用方法,并提供若干编写单元测试的建议和技巧,供单元测试的初学者参考. 继续下文之前,先来看看一个非常简单的测试 ...

  7. Mysql Communications link failure 问题的解决

    问题现象 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last p ...

  8. 蓝牙核心技术概述(四):蓝牙协议规范(HCI、L2CAP、SDP、RFOCMM)(转载)

    一.主机控制接口协议  HCI 蓝牙主机-主机控模型 蓝牙软件协议栈堆的数据传输过程: 1.蓝牙控制器接口数据分组:指令分组.事件分组.数据分组(1).指令分组 如:Accpet Connection ...

  9. C#三层架构详细解剖

    深入浅出C#三层架构(转) 本文用一个示例来介绍如何建设一个三层架构的项目,并说明项目中各个文件所处的层次与作用.写本文的目的,不是为了说明自己的这个方法有多对,而是希望给那些初学三层架构却不知从何入 ...

随机推荐

  1. linux软件管理(Vim编辑器使用) ——(七)

    windows : .exe     安装 .卸载 安装:  mysql.exe  cc.exe 卸载 : 该软件唯一的标识  ,包名   alibaba android : *.apk   卸载 包 ...

  2. v4l2 Camera详细设置【转】

    转自:http://blog.csdn.net/smilefyx/article/details/39555289 转载自:http://blog.sina.com.cn/s/blog_602f877 ...

  3. APMserv常见问题

    一.启动是提示apache启动失败,mysql启动成功 1.去掉ssl的勾选(勾选需要证书),重启看解决没有 2.端口被占,吧apache端口改成80或者8080端口(其他端口也可以试试,80的话访问 ...

  4. Centos 7.2 双网卡绑定之踩坑

    线上服务器,安装centos7.2 x64最小化安装,需要做链路聚合,双网卡绑定.在centos 6.x 和 centos 7上测试都OK,于是直接开搞. 说明下,以下环境是在虚拟机中实现的: 系统: ...

  5. C# 正则表达式判断IP,URL等及其解释

    C# 正则表达式判断IP,URL等及其解释 判断IP格式方法: public static bool ValidateIPAddress(string ipAddress) { Regex valid ...

  6. python的加密算法(1):反转加密

    说白了,就是把字符串倒序. 在js里,有一个reverse.但是python中没有. 不过,有一个更简单的方法,就是: ‘abcd’ [::-1] 这里,具体解释一下: (参看:https://doc ...

  7. dict的setdefault(学习bottle源代码)

    在bottle的源代码中,看到如下一段话: sys.modules.setdefault('bottle', sys.modules['__main__']) 这段话,实际上就是dict类型的setd ...

  8. win7或win2008系统中,出现【已停止工作,联机检查解决方案并关闭该程序,关闭程序】解决方法!

    win7或win2008系统中,出现[已停止工作,联机检查解决方案并关闭该程序,关闭程序]解决方法! 经过摸索,点击[控制面板]-[操作中心]-[更改操作中心设置]-[问题报告设置]-[从不检查解决方 ...

  9. php多台服务器实现session共享

    使用Redis存储Session(前提是服务期间已实现redis共享,可参照:laravel项目使用twemproxy部署redis集群) 修改php.ini: session.save_handle ...

  10. HDU 2562 奇偶位互换(字符串,水)

      奇偶位互换 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...