nhibernate change connection
http://stackoverflow.com/questions/4335827/changing-nhibernate-connectionstring
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的更多相关文章
- weblogic JDBC Connection Pools--转官方文档
http://docs.oracle.com/cd/E13222_01/wls/docs81/ConsoleHelp/jdbc_connection_pools.html#1106016 JDBC C ...
- BLE Hacking:使用Ubertooth one扫描嗅探低功耗蓝牙
0×00 前言 低功耗蓝牙(Low Energy; LE),又视为Bluetooth Smart或蓝牙核心规格4.0版本.其特点具备节能.便于采用,是蓝牙技术专为物联网(Internet of Thi ...
- 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 ...
- 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 ...
- 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, ...
- NUnit使用
NUnit是.net平台上使用得最为广泛的测试框架之一,本文将通过示例来描述NUnit的使用方法,并提供若干编写单元测试的建议和技巧,供单元测试的初学者参考. 继续下文之前,先来看看一个非常简单的测试 ...
- Mysql Communications link failure 问题的解决
问题现象 com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last p ...
- 蓝牙核心技术概述(四):蓝牙协议规范(HCI、L2CAP、SDP、RFOCMM)(转载)
一.主机控制接口协议 HCI 蓝牙主机-主机控模型 蓝牙软件协议栈堆的数据传输过程: 1.蓝牙控制器接口数据分组:指令分组.事件分组.数据分组(1).指令分组 如:Accpet Connection ...
- C#三层架构详细解剖
深入浅出C#三层架构(转) 本文用一个示例来介绍如何建设一个三层架构的项目,并说明项目中各个文件所处的层次与作用.写本文的目的,不是为了说明自己的这个方法有多对,而是希望给那些初学三层架构却不知从何入 ...
随机推荐
- autoKeras Windows 的入门测试
在测试中分析一下ide的效果,在pycharm中测试的时候老师提示内存溢出,而且跑autoKeras的cnn时确实消耗很大空间.但是同样的电脑,换了vscode进行测试的时候没有问题.我也不知道什么回 ...
- mongodb安全
1.流程: (1)创建超级管理员 (2)修改配置文件,验证身份登录 (3)重启服务 (4)使用超级管理员登录 (5)创建普通用户 (6)使用普通用户登录对应的数据库 mongodb数据库角色: 1创建 ...
- linux下rm命令删除文件名中包含特殊字符的文件【转】
转自:http://blog.itpub.net/143526/viewspace-1060083/ 1. 删除带“-”的文件名的方法 2. 删除包含其它特殊字符的文件 3. 删除系统打不出的乱码文件 ...
- 我的一次安装oracle的过程
1.在装oracle之前,先安装.net3.5 2.然后正常安装oracle,一直next 3.装完oracle后,安装plsql dev工具,打开工具,发现没有connect as,是需要进行一些配 ...
- 《Java编程思想》笔记 第五章 初始化与清理
1.构造器 因为创建一个类的对象构造器就会自动执行,故初始化某些东西特好 2.方法重载 方法名相同,参数列表不同. 2.1 区分重载方法 方法重载后区别不同方法的就是方法签名 -->参数类型和个 ...
- k8s的故障切换(failover)
当前3个节点的状态都为ready 当前node1有两个pod node2有1个pod 现在将node1关机会有怎样的现象 ping 分布在node1节点的pod地址已经ping不通. 在node1节 ...
- java的maven项目(三)私服的搭建(windows版)
1 私服 nexus 安装nexus 启动服务 启动失败的解决方法: 登录nexus 用户名/密码 admin/admin123 仓库类型 Virtual 虚拟仓库 Proxy 代 ...
- CSS基本属性—文本属性和背景属性
一.CSS常用文本属性 [css中的颜色表示方式] 1.直接使用颜色的单词表示:red.green.blue 2.使用颜色的十六进制表示:#ff0000,#00ff00: 六位数,两两 ...
- 图解Javascript——执行上下文
什么是执行上下文? 执行上下文(Execution Context)是ECMAScript规范中用来描述 JavaScript 代码执行的抽象概念,规定了当前代码执行的环境(当前执行代码片段中的变量. ...
- (八)for语句
(1)语法 (2)批量ping主机 这里有个重点就是把每次ping主机的动作放到后台运行 #!/bin/bash >ip.txt for i in {1..254} do ip=192.168. ...