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#三层架构(转) 本文用一个示例来介绍如何建设一个三层架构的项目,并说明项目中各个文件所处的层次与作用.写本文的目的,不是为了说明自己的这个方法有多对,而是希望给那些初学三层架构却不知从何入 ...
随机推荐
- asp单页面301跳转
<% Response.Status="301 Moved Permanently"Response.AddHeader "Location", &quo ...
- python中的scapy模块
scapy模块是干嘛用的? 答:Scapy的是一个强大的交互式数据包处理程序(使用python编写).它能够伪造或者解码大量的网络协议数据包,能够发送.捕捉.匹配请求和回复包等等.它可以很容易地处理一 ...
- Mark : Bootstrap fileInput控制预览页面上传、删除、详情按钮
Bootstrap fileInput默认预览上传效果: 而我们可能想要的结果是: 这时候可以通过初始参数layoutTemplates来控制:
- [ Python - 8 ] 简单demo练习
练习1: 利用os模块编写一个能实现dir -l输出的程序(注意:dir /l是windows下命令) #!_*_coding:utf-8_*_ # Author: hkey import os, t ...
- 【 sysbench 性能基准测试 】
度娘解释:sysbench是一款开源的多线程性能测试工具,可以执行CPU/内存/线程/IO/数据库等方面的性能测试. 目前支持的数据库支持:MySQL,pgsql,oracle 这3种数据库. 安装s ...
- 基于Docker 搭建 wordpress
在Docker中,一般遵循一个Docker只运行一个应用,这样方便维护. 首先需要将centos 镜像pull到本地,并搭建本地yum仓库 yum仓库地址:http://192.168.2.11:80 ...
- [ 脚本 ] RHEL6.x 及Centos6.x 初始化脚本
#!/bin/bash # check network echo "-------------check Network-------------" ping -c -t mirr ...
- Selenium2+python自动化72-logging日志使用【转载】
前言 脚本运行的时候,有时候不知道用例的执行情况,这时候可以加入日志,这样出现问题后方便查阅,也容易排查哪些用例执行了,哪些没有执行. 一.封装logging模块 1.关于logging日志的介绍,我 ...
- 使用ShowDoc在线管理API接口文档
ShowDoc是什么 每当接手一个他人开发好的模块或者项目,看着那些没有写注释的代码,我们都无比抓狂.文档呢?!文档呢?!Show me the doc !! ShowDoc就是一个非常适合IT团队的 ...
- IE8的 JS 引擎如此不堪?
之前给客户做了个网站,其中有这么一个功能: 文章内容中,有指向某个图片的链接,链接内容为图片名称(文字),点击之后在页面上弹出该图片显示,图片可以为png,jpg,gif等. 于是,祭出了JQuery ...