Connection Manager connectionmanager in csla

Source: Internet
Author: User

When csla implements a nested database connection, use a database connection to view the instance:

 

Code 1 private string _ conn = "connection string ";
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}

 

 

 

When you create a connection in the insertb method, the connection created in inserta is used.

Implementation principle:

1. Create a connection Mgr (connection variable name) in inserta, save the connection string in the local context localcontext, and set the counter mrefcount ++ to 1,

2. If this connection string is found in localcontext when you create a connection in insertb, use the memory of the existing connection Mgr. The counter mrefcount ++ is 2.

3. When the using block in insertb ends, the counter mrefcount -- is 1

4. When the using block in inserta ends, the counter mrefcount -- is 0, the database connection is destroyed, and the connection string is removed from localcontext.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.