forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConnectionProvider.cs
124 lines (108 loc) · 2.96 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
using System;
using System.Collections;
using System.Data;
using log4net;
using NHibernate.Driver;
using Environment = NHibernate.Cfg.Environment;
namespace NHibernate.Connection
{
/// <summary>
/// The base class for the ConnectionProvider.
/// </summary>
public abstract class ConnectionProvider : IConnectionProvider, IDisposable
{
private static readonly ILog log = LogManager.GetLogger( typeof( ConnectionProvider ) );
private string connString = null;
private IDriver driver = null;
/// <summary>
///
/// </summary>
/// <param name="conn"></param>
public virtual void CloseConnection( IDbConnection conn )
{
log.Debug( "Closing connection" );
try
{
conn.Close();
}
catch( Exception e )
{
throw new ADOException( "Could not close " + conn.GetType().ToString() + " connection", e );
}
}
/// <summary>
/// Configures the ConnectionProvider with the Driver and the ConnectionString.
/// </summary>
/// <param name="settings">A name/value Dictionary that contains the settings for this ConnectionProvider.</param>
/// <exception cref="HibernateException">Thrown when a ConnectionString could not be found or the Driver Class could not be loaded.</exception>
public virtual void Configure( IDictionary settings )
{
log.Info( "Configuring ConnectionProvider" );
connString = settings[ Environment.ConnectionString ] as string;
if( connString == null )
{
throw new HibernateException( "Could not find connection string setting" );
}
ConfigureDriver( settings );
}
/// <summary>
/// Configures the driver for the ConnectionProvider.
/// </summary>
/// <param name="settings">A name/value Dictionary that contains the settings for the Driver.</param>
protected virtual void ConfigureDriver( IDictionary settings )
{
string driverClass = settings[ Environment.ConnectionDriver ] as string;
if( driverClass == null )
{
throw new HibernateException( "The " + Environment.ConnectionDriver + " must be specified in the NHibernate configuration section." );
}
else
{
try
{
driver = ( IDriver ) Activator.CreateInstance( System.Type.GetType( driverClass ) );
}
catch( Exception e )
{
throw new HibernateException( "Could not create the driver from " + driverClass + ".", e );
}
}
}
/// <summary>
///
/// </summary>
protected virtual string ConnectionString
{
get { return connString; }
}
/// <summary>
///
/// </summary>
public IDriver Driver
{
get { return driver; }
}
/// <summary>
/// Grab a Connection from this ConnectionProvider
/// </summary>
/// <returns></returns>
public abstract IDbConnection GetConnection();
/// <summary>
///
/// </summary>
public abstract bool IsStatementCache { get; }
/// <summary>
///
/// </summary>
public abstract void Close();
#region IDisposable Members
/// <summary>
///
/// </summary>
public void Dispose()
{
Close();
}
#endregion
}
}