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

推荐订阅源

爱范儿
爱范儿
博客园_首页
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

博客园 - 白亚伟

如何在快速迭代开发中写清楚需求,提高与软件工程师的沟通效率 Tomcat8.5.56配置多个服务 PowerDesign设置列名与表名的大小写规则 Nunit在VS2010加载不了程序集的解决办法 ArcGIS JavaScript API本地部署离线开发环境 读《敏捷个人-认识自我,管理自我》之学习 读《敏捷个人-认识自我,管理自我》之Scrum的核心价值观 读《敏捷个人-认识自我,管理自我》之责任 SuperMap实现2.5维地图的热区功能 温习计算机基础的好去处--网易公开课 WIN7下.Net开发遇到的又一问题:HTTP 错误 500.19 - Internal Server Error,无法访问请求的页面,因为该页的相关配置数据无效。 IIS7下安装.net1.1 年终了,总结一下 VS2003出现ASP.NET版本不匹配,解决方法 CSLA中业务层事务的实现 CSLA.NET权限规则的困惑 转载:提升SQL Server程序员工作效率的几个技巧 svn提交时设置文件类型过滤 .net showModalDialog传递大对象时,ie出错自动关闭,求助
CSLA中的连接管理器ConnectionManager
白亚伟 · 2010-12-16 · via 博客园 - 白亚伟

CSLA中实现了嵌套数据库连接时,使用一个数据库连接,看实例:

代码

 1 private string _conn = "连接字符串";
 2 private void InsertA()
 3 {
 4     using (var ctx = ConnectionManager<SqlConnection>.GetManager(_conn, false))
 5     {
 6         using (var cm = ctx.Connection.CreateCommand())
 7         {
 8             cm.CommandType = CommandType.Text;
 9             cm.CommandText = "insert into a values('01')";
10             cm.ExecuteNonQuery();
11         }
12         InsertB();
13     }
14 }
15 
16 private void InsertB()
17 {
18     using (var ctx = ConnectionManager<SqlConnection>.GetManager(_conn, false))
19     {
20         using (var cm = ctx.Connection.CreateCommand())
21         {
22             cm.CommandType = CommandType.Text;
23             cm.CommandText = "insert into b values('02')";
24             cm.ExecuteNonQuery();
25         }
26     }
27 }

 其中InsertB方法中创建连接时,会使用InsertA中创建的连接。

 实现原理:

1、在InsertA中创建连接mgr(连接变量名),在本地上下文LocalContext中保存此连接字符串,并且有个计数器mRefCount++为1,

2、在InsertB中创建连接时,在LocalContext中发现有此连接字符串,则使用忆存在的连接mgr,计数器mRefCount++为2。

3、在InsertB中Using块结束时,计数器mRefCount--为1

4、在InsertA中Using块结束时,计数器mRefCount--为0,销毁数据库连接,LocalContext中移除连接字符串。