forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionProvider.cs
221 lines (198 loc) · 6.63 KB
/
ConnectionProvider.cs
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
using System;
using System.Collections;
using System.Data.Common;
using NHibernate.Driver;
using NHibernate.Util;
using Environment=NHibernate.Cfg.Environment;
using System.Collections.Generic;
namespace NHibernate.Connection
{
/// <summary>
/// The base class for the ConnectionProvider.
/// </summary>
public abstract partial class ConnectionProvider : IConnectionProvider
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(ConnectionProvider));
private string connString;
private IDriver driver;
/// <summary>
/// Closes the <see cref="DbConnection"/>.
/// </summary>
/// <param name="conn">The <see cref="DbConnection"/> to clean up.</param>
public virtual void CloseConnection(DbConnection conn)
{
if (conn == null)
throw new ArgumentNullException(nameof(conn));
log.Debug("Closing connection");
try
{
conn.Close();
}
catch (Exception e)
{
throw new ADOException("Could not close " + conn.GetType() + " connection", e);
}
}
/// <summary>
/// Configures the ConnectionProvider with the Driver and the ConnectionString.
/// </summary>
/// <param name="settings">An <see cref="IDictionary"/> that contains the settings for this ConnectionProvider.</param>
/// <exception cref="HibernateException">
/// Thrown when a <see cref="Cfg.Environment.ConnectionString"/> could not be found
/// in the <c>settings</c> parameter or the Driver Class could not be loaded.
/// </exception>
public virtual void Configure(IDictionary<string, string> settings)
{
log.Info("Configuring ConnectionProvider");
// Connection string in the configuration overrides named connection string
if (!settings.TryGetValue(Environment.ConnectionString, out connString))
connString = GetNamedConnectionString(settings);
if (connString == null)
{
throw new HibernateException("Could not find connection string setting (set " +
Environment.ConnectionString +
" or " +
Environment.ConnectionStringName +
" property)");
}
ConfigureDriver(settings);
}
/// <summary>
/// Get a named connection string, if configured.
/// </summary>
/// <exception cref="HibernateException">
/// Thrown when a <see cref="Environment.ConnectionStringName"/> was found
/// in the <c>settings</c> parameter but could not be found in the app.config.
/// </exception>
protected virtual string GetNamedConnectionString(IDictionary<string, string> settings)
{
return Environment.GetNamedConnectionString(settings);
}
/// <summary>
/// Configures the driver for the ConnectionProvider.
/// </summary>
/// <param name="settings">An <see cref="IDictionary"/> that contains the settings for the Driver.</param>
/// <exception cref="HibernateException">
/// Thrown when the <see cref="Environment.ConnectionDriver"/> could not be
/// found in the <c>settings</c> parameter or there is a problem with creating
/// the <see cref="IDriver"/>.
/// </exception>
protected virtual void ConfigureDriver(IDictionary<string, string> settings)
{
string driverClass;
if (!settings.TryGetValue(Environment.ConnectionDriver, out driverClass))
{
throw new HibernateException("The " + Environment.ConnectionDriver +
" must be specified in the NHibernate configuration section.");
}
else
{
try
{
driver =
(IDriver) Environment.ObjectsFactory.CreateInstance(ReflectHelper.ClassForName(driverClass));
driver.Configure(settings);
}
catch (Exception e)
{
throw new HibernateException("Could not create the driver from " + driverClass + ".", e);
}
}
}
/// <summary>
/// Gets the <see cref="String"/> for the <see cref="DbConnection"/>
/// to connect to the database.
/// </summary>
/// <value>
/// The <see cref="String"/> for the <see cref="DbConnection"/>
/// to connect to the database.
/// </value>
//TODO 6.0: Make public
protected internal virtual string ConnectionString
{
get { return connString; }
}
/// <summary>
/// Gets the <see cref="IDriver"/> that can create the <see cref="DbConnection"/> object.
/// </summary>
/// <value>
/// The <see cref="IDriver"/> that can create the <see cref="DbConnection"/>.
/// </value>
public IDriver Driver
{
get { return driver; }
}
/// <summary>
/// Get an open <see cref="DbConnection"/>.
/// </summary>
/// <returns>An open <see cref="DbConnection"/>.</returns>
public virtual DbConnection GetConnection()
{
return GetConnection(ConnectionString);
}
//TODO 6.0: Make abstract
/// <summary>
/// Gets an open <see cref="DbConnection"/> for given connectionString
/// </summary>
/// <returns>An open <see cref="DbConnection"/>.</returns>
public virtual DbConnection GetConnection(string connectionString)
{
throw new NotImplementedException("This method must be overriden.");
}
#region IDisposable Members
/// <summary>
/// A flag to indicate if <c>Disose()</c> has been called.
/// </summary>
private bool _isAlreadyDisposed;
/// <summary>
/// Finalizer that ensures the object is correctly disposed of.
/// </summary>
~ConnectionProvider()
{
Dispose(false);
}
/// <summary>
/// Takes care of freeing the managed and unmanaged resources that
/// this class is responsible for.
/// </summary>
public void Dispose()
{
Dispose(true);
}
/// <summary>
/// Takes care of freeing the managed and unmanaged resources that
/// this class is responsible for.
/// </summary>
/// <param name="isDisposing">Indicates if this ConnectionProvider is being Disposed of or Finalized.</param>
/// <remarks>
/// <p>
/// If this ConnectionProvider is being Finalized (<c>isDisposing==false</c>) then make
/// sure not to call any methods that could potentially bring this
/// ConnectionProvider back to life.
/// </p>
/// <p>
/// If any subclasses manage resources that also need to be disposed of this method
/// should be overridden, but don't forget to call it in the override.
/// </p>
/// </remarks>
protected virtual void Dispose(bool isDisposing)
{
if (_isAlreadyDisposed)
{
// don't dispose of multiple times.
return;
}
// free managed resources that are being managed by the ConnectionProvider if we
// know this call came through Dispose()
if (isDisposing)
{
log.Debug("Disposing of ConnectionProvider.");
// nothing for Finalizer to do - so tell the GC to ignore it
GC.SuppressFinalize(this);
}
// free unmanaged resources here
_isAlreadyDisposed = true;
}
#endregion
}
}