Quick Objects Documentation Akal Tech Logo
Sharing Connection Between Multiple Objects

Glossary Item Box

Business Logic Framework - How To:

Sharing Connection between multiple object instances is automatic when you use the Join method. However, sometimes you need to run commands on unrelated objects and connection reuse can offer a significant performance gain.  The following code snippet shows how two unrelated objects can share the same connection.

Please note that once you dispose off one of the objects, the database connection will no longer be available. Hence it is important that you only dispose the object that needs to use the connection last.
1Using p As New Products() 
2    p.ProductID.Value = 1 
3    ' Lets create another instance of an unrelated class. 
4    ' Each business object has an overloaded contructor that can be used 
5    ' to pass another instance from which the database connection/transaction 
6    ' should be shared. 
7    Dim c As New Customers(p) 
8    ' The above line is equivalent to the following two line. 
9    ' Customers c = new Customers(); 
10    ' p.AssignConnection(c); 
11    p.Load() 
12     
13    ' Now you can perform any operation on "c" as well and all commands will go over the same connection. 
14    ' If the UseTransaction property was set to true prior to sharing the connection, that would result in 
15    ' both objects sharing the same transaction as well. 
16     
17    c.CustomerID.Value = 1 
18        ' Now you will notice that do not need to call the Dispose method on "c", since the "p" (Products) instance will automatically dispose 
19        ' of the database connection that is shared by both p and c. 
20    c.Load() 
21End Using 

1using (Products p = new Products()) 
2
3    p.ProductID.Value = 1; 
4    // Lets create another instance of an unrelated class. 
5    // Each business object has an overloaded contructor that can be used 
6    // to pass another instance from which the database connection/transaction 
7    // should be shared. 
8    Customers c = new Customers(p); 
9    // The above line is equivalent to the following two line. 
10    // Customers c = new Customers(); 
11    // p.AssignConnection(c); 
12    p.Load(); 
13 
14    // Now you can perform any operation on "c" as well and all commands will go over the same connection. 
15    // If the UseTransaction property was set to true prior to sharing the connection, that would result in 
16    // both objects sharing the same transaction as well. 
17 
18    c.CustomerID.Value = 1; 
19    c.Load(); 
20    // Now you will notice that do not need to call the Dispose method on "c", since the "p" (Products) instance will automatically dispose 
21    // of the database connection that is shared by both p and c. 
22
23