File: SqlConnectionManager.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (169 lines) | stat: -rw-r--r-- 6,102 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq.Expressions;
using System.IO;
using System.Reflection;
using System.Text;
using System.Transactions;

namespace System.Data.Linq.SqlClient {
    using System.Data.Linq;
    using System.Data.Linq.Provider;

    internal class SqlConnectionManager : IConnectionManager {
        private IProvider provider;
        private DbConnection connection;
        private bool autoClose;
        private bool disposeConnection;  // should we dispose this connection when the context is disposed?
        private DbTransaction transaction;
        private Transaction systemTransaction;
        private SqlInfoMessageEventHandler infoMessagehandler;
        private List<IConnectionUser> users;
        private int maxUsers;

        internal SqlConnectionManager(IProvider provider, DbConnection con, int maxUsers, bool disposeConnection) {
            this.provider = provider;
            this.connection = con;
            this.maxUsers = maxUsers;
            this.infoMessagehandler = new SqlInfoMessageEventHandler(this.OnInfoMessage);
            this.users = new List<IConnectionUser>(maxUsers);
            this.disposeConnection = disposeConnection;
        }


        public DbConnection UseConnection(IConnectionUser user) {
            if (user == null) {
                throw Error.ArgumentNull("user");
            }
            if (this.connection.State == ConnectionState.Closed) {
                this.connection.Open();
                this.autoClose = true;
                this.AddInfoMessageHandler();
                if (System.Transactions.Transaction.Current != null) {
                    System.Transactions.Transaction.Current.TransactionCompleted += this.OnTransactionCompleted;
                }
            }
            if (this.transaction == null && System.Transactions.Transaction.Current != null &&
                        System.Transactions.Transaction.Current != systemTransaction) {
                this.ClearConnection();
                systemTransaction = System.Transactions.Transaction.Current;
                this.connection.EnlistTransaction(System.Transactions.Transaction.Current);
            }

            if (this.users.Count == this.maxUsers) {
                this.BootUser(this.users[0]);
            }
            this.users.Add(user);
            return this.connection;
        }

        private void BootUser(IConnectionUser user) {
            bool saveAutoClose = this.autoClose;
            this.autoClose = false;
            int index = this.users.IndexOf(user);
            if (index >= 0) {
                this.users.RemoveAt(index);
            }
            user.CompleteUse();
            this.autoClose = saveAutoClose;
        }

        internal DbConnection Connection {
            get { return this.connection; }
        }

        internal int MaxUsers {
            get { return this.maxUsers; }
        }

        internal void DisposeConnection() {
            // only close this guy if we opened it in the first place
            if (this.autoClose) {
                this.CloseConnection();
            }

            // If we created the connection, we need to dispose it even if the user explicitly
            // opened it using the Connection property on the context.
            if (this.connection != null && this.disposeConnection) {
                this.connection.Dispose();
                this.connection = null;
            }
        }

        internal void ClearConnection() {
            while (this.users.Count > 0) {
                this.BootUser(this.users[0]);
            }
        }

        internal bool AutoClose {
            get { return this.autoClose; }
            set { this.autoClose = value; }
        }

        internal DbTransaction Transaction {
            get { return this.transaction; }
            set {
                if (value != this.transaction) {
                    if (value != null) {
                        if (this.connection != value.Connection) {
                            throw Error.TransactionDoesNotMatchConnection();
                        }
                    }
                    this.transaction = value;
                }
            }
        }

        public void ReleaseConnection(IConnectionUser user) {
            if (user == null) {
                throw Error.ArgumentNull("user");
            }
            int index = this.users.IndexOf(user);
            if (index >= 0) {
                this.users.RemoveAt(index);
            }
            if (this.users.Count == 0 && this.autoClose && this.transaction == null && System.Transactions.Transaction.Current == null) {
                this.CloseConnection();
            }
        }

        private void CloseConnection() {
            if (this.connection != null && this.connection.State != ConnectionState.Closed) {
                this.connection.Close();
            }
            this.RemoveInfoMessageHandler();
            this.autoClose = false;
        }

        private void OnInfoMessage(object sender, SqlInfoMessageEventArgs args) {
            if (this.provider.Log != null) {
                this.provider.Log.WriteLine(Strings.LogGeneralInfoMessage(args.Source, args.Message));
            }
        }

        private void OnTransactionCompleted(object sender, System.Transactions.TransactionEventArgs args) {
            if (this.users.Count == 0 && this.autoClose) {
                this.CloseConnection();
            }
        }

        private void AddInfoMessageHandler() {
            SqlConnection scon = this.connection as SqlConnection;
            if (scon != null) {
                scon.InfoMessage += this.infoMessagehandler;
            }
        }

        private void RemoveInfoMessageHandler() {
            SqlConnection scon = this.connection as SqlConnection;
            if (scon != null) {
                scon.InfoMessage -= this.infoMessagehandler;
            }
        }
    }
}