zoukankan      html  css  js  c++  java
  • CSLA中的连接管理器ConnectionManager

    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中移除连接字符串。

  • 相关阅读:
    去深圳办理港澳通行证签注延期
    預約申領往來港澳通行證及簽注x
    表格选中效果展示
    Purchase购物车实例分析
    IOS开发基础知识--碎片17
    IOS开发基础知识--碎片16
    IOS开发基础知识--碎片15
    IOS开发基础知识--碎片14
    IOS关于LKDBHelper实体对象映射插件运用
    IOS开发基础知识--碎片13
  • 原文地址:https://www.cnblogs.com/brawei/p/1907643.html
Copyright © 2011-2022 走看看