惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
博客园_首页
W
WeLiveSecurity
S
Secure Thoughts
S
Security @ Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
H
Hacker News: Front Page
Project Zero
Project Zero
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
U
Unit 42
N
News and Events Feed by Topic
N
News and Events Feed by Topic
Hacker News - Newest:
Hacker News - Newest: "LLM"
Forbes - Security
Forbes - Security
T
Tor Project blog
I
Intezer
B
Blog
F
Full Disclosure
Security Archives - TechRepublic
Security Archives - TechRepublic
F
Fortinet All Blogs
Schneier on Security
Schneier on Security
T
Threat Research - Cisco Blogs
AI
AI
Google DeepMind News
Google DeepMind News
L
LINUX DO - 最新话题
Cloudbric
Cloudbric
L
Lohrmann on Cybersecurity
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
P
Privacy International News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
PCI Perspectives
PCI Perspectives
Y
Y Combinator Blog
Spread Privacy
Spread Privacy
Simon Willison's Weblog
Simon Willison's Weblog
罗磊的独立博客
Vercel News
Vercel News
A
Arctic Wolf
The Register - Security
The Register - Security
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Microsoft Azure Blog
Microsoft Azure Blog
H
Heimdal Security Blog
Know Your Adversary
Know Your Adversary
P
Proofpoint News Feed
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed

博客园 - 没肉包子

Vim as Python IDE on windows 如何在windows下安装"The Eric Python IDE" - 没肉包子 一个新的轮回,一个新的开始 一名英格兰球迷眼里的本届英格兰队 用我的MyGeneration模板生成NHibernate映射文件和关系(one-to-one,one-to-many,many-to-many) 强大的代码生成工具MyGeneration 请帮'Atlas'起个名字 - 没肉包子 - 博客园 NHibernate的关联映射(one-to-one,one-to-many,many-to-many)以及cascade分析 特里当选英格兰队新任队长 发疯的live.com NHibernate的Q&A(持续更新) NHibernate的调试技巧和Log4Net配置 诚邀博客链接 推荐两款在线日程(任务)管理的站点 NHibernate的Session管理 有关NHibernate的问题请在此提出 [调查]如果你是个讲求编码规范的程序员,而碰到给变量、方法随意命名,注释又混乱的同事时你会怎么处理??? NHibernate的灵活配置 [转载]HTTP MIME类型即HttpResponse.ContentType属性值列表
NHibernate的数据库连接机制分析和如何使用外部连接
没肉包子 · 2006-08-07 · via 博客园 - 没肉包子

1.普通数据库连接的分析

在一般情况下我们会让NHibernate应用我们让NHibernate自己管理数据库连接。

我们来看看配置文件中和连接有关的配置信息。

<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="connection.connection_string">server=.;database=user;uid=sa;pwd=App1234;</property>
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>

我们来分析一下这些属性的作用:

connection.provider需配置实现NHibernate.Connection.IConnectionProvider接口的类。
IConnectionProvider定义了创建IDbConnection(用到connection_string)和关闭IDbConnection的功能。
DriverConnectionProvide实现了IConnectionProvider接口,使用IDriver提供的IDbConnection。  
UserSuppliedConnectionProvider实现了IConnectionProvider,使用外部提供的IDbConnection。
ConnectionProviderFactory根据配置文件决定创建何种类型的IConnectionProvider。(让NHibernate自己管理设置DriverConnectionProvide。外部提供连接则无需设置。)

connection.driver_class对应NHibernate.Driver.IDriver接口的类。
描述NHibernate怎么与不同的.NET Data Providers相结合的策略(此处使用了策略模式,关于设计模式可查看Terrylee新版设计模式手册[C#] )。
主要定义了改使用怎样的连接类型和Command类型,以及创建连接和Command的方法(CreateCommand 和 CreateConnection )。
不同的数据库有不同的策略实现。例如:SqlClientDriver提供了访问SQLServer数据的策略(图中省去MySqlDataDriver等多种IDriver)。

connection.connection_string提供连接字符串。  

dialect需要配置NHibernate.Dialect.Dialect的子类。 Dialect描述了不同的RDBMS的细节。数据库数据类型和NHibernate数据类型的对应,数据库的函数信息等等。(图中省去多种Dialect)

2.使用外部连接
在某些特殊的时候我们要用到自己提供的连接,例如实现一个连接池管理数据库连接。
注意:使用这种方法的时候切忌不要在一个连接上打开两个并行的ISession

在配置文件中去掉连接的自动管理连接部分的配置.注意dialect不能去掉.

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.0">
    
<session-factory name="DDLLY.MyDoc.NHibernateTest.CustomConnect">
        
<!-- 属性 -->
        
<!--<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
        <property name="connection.connection_string">server=.;database=user;uid=sa;pwd=App1234;</property>
-->
        
<property name="show_sql">true</property>
        
<property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
        
<property name="use_outer_join">true</property>
        
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
        
<!-- 映射文件 -->
        
<mapping assembly="DDLLY.MyDoc.NHibernateTest.CustomConnect" />
    
</session-factory>
</hibernate-configuration>

代码中需把OpenSession() 改为使用它的重载函数OpenSession(IDbConnection conn)

对我写的快速入门的例子进行了一点改动,实现自行提供连接。定义一个CreateConnection的方法创建新的连接。把ISession session = factory.OpenSession()改为ISession session = factory.OpenSession(CreateConnection());

改后代码如下

using System.Collections;
using System.Data.SqlClient;
using NHibernate;
using NHibernate.Cfg;
using NUnit.Framework;
using System.Data;

namespace DDLLY.MyDoc.NHibernateTest.CustomConnect
{
    
/// <summary>
    
/// User测试类
    
/// </summary>

    [TestFixture]
    
public class UserFixture
    
{
        
常量

        
SetUp和TearDown

        
私有方法

        
测试方法
    }

}