1
|
|
|
2
|
|
#region Apache Notice
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
#endregion
|
26
|
|
|
27
|
|
#region Using
|
28
|
|
using System;
|
29
|
|
using System.Data;
|
30
|
|
using System.Collections;
|
31
|
|
using System.Reflection;
|
32
|
|
using System.Xml.Serialization;
|
33
|
|
|
34
|
|
using Castle.DynamicProxy;
|
35
|
|
|
36
|
|
using IBatisNet.Common;
|
37
|
|
using IBatisNet.Common.Utilities.Objects;
|
38
|
|
|
39
|
|
using IBatisNet.Common.Exceptions;
|
40
|
|
|
41
|
|
using log4net;
|
42
|
|
#endregion
|
43
|
|
|
44
|
|
namespace IBatisNet.Common.Logging
|
45
|
|
{
|
46
|
|
|
47
|
|
|
48
|
|
|
49
|
|
|
50
|
|
public class IDbConnectionProxy : IInterceptor
|
51
|
|
{
|
52
|
|
|
53
|
|
#region Fields
|
54
|
|
private string _connectionString = string.Empty;
|
55
|
|
private IDbConnection _connection = null;
|
56
|
|
private Provider _provider = null;
|
57
|
|
private static ArrayList _passthroughMethods = new ArrayList();
|
58
|
|
private static readonly ILog _logger = LogManager.GetLogger( "System.Data.IDbConnection" );
|
59
|
|
|
60
|
|
#endregion
|
61
|
|
|
62
|
|
#region Constructors
|
63
|
|
|
64
|
0
|
static IDbConnectionProxy()
|
65
|
|
{
|
66
|
|
_passthroughMethods.Add("GetType");
|
67
|
|
_passthroughMethods.Add("ToString");
|
68
|
|
}
|
69
|
|
|
70
|
|
|
71
|
|
|
72
|
|
|
73
|
|
|
74
|
|
|
75
|
0
|
internal IDbConnectionProxy(IDbConnection connection, Provider provider)
|
76
|
|
{
|
77
|
|
_connection = connection;
|
78
|
|
_provider = provider;
|
79
|
|
}
|
80
|
|
#endregion
|
81
|
|
|
82
|
|
#region Methods
|
83
|
|
|
84
|
|
|
85
|
|
|
86
|
|
|
87
|
|
|
88
|
|
|
89
|
|
|
90
|
0
|
internal static IDbConnection NewInstance(IDbConnection connection, Provider provider)
|
91
|
|
{
|
92
|
|
object proxyConnection = null;
|
93
|
|
IInterceptor handler = new IDbConnectionProxy(connection, provider);
|
94
|
|
|
95
|
|
ProxyGenerator proxyGenerator = new ProxyGenerator();
|
96
|
|
|
97
|
|
proxyConnection = proxyGenerator.CreateProxy(typeof(IDbConnection), handler, connection);
|
98
|
|
|
99
|
|
return (IDbConnection) proxyConnection;
|
100
|
|
}
|
101
|
|
#endregion
|
102
|
|
|
103
|
|
#region IInterceptor Members
|
104
|
|
|
105
|
|
|
106
|
|
|
107
|
|
|
108
|
|
|
109
|
|
|
110
|
|
|
111
|
0
|
public object Intercept(IInvocation invocation, params object[] arguments)
|
112
|
|
{
|
113
|
|
object returnValue = null;
|
114
|
|
|
115
|
|
if (invocation.Method.Name=="Open")
|
116
|
|
{
|
117
|
|
_connection.Open();
|
118
|
|
if (_logger.IsDebugEnabled)
|
119
|
|
{
|
120
|
|
_logger.Debug( string.Format("Open Connection \"{0}\" to \"{1}\".", _connection.GetHashCode().ToString(), _provider.Description) );
|
121
|
|
}
|
122
|
|
}
|
123
|
|
else if (invocation.Method.Name=="Close")
|
124
|
|
{
|
125
|
|
_connection.Close();
|
126
|
|
if (_logger.IsDebugEnabled)
|
127
|
|
{
|
128
|
|
_logger.Debug( string.Format("Close Connection \"{0}\" to \"{1}\".", _connection.GetHashCode().ToString(), _provider.Description) );
|
129
|
|
}
|
130
|
|
}
|
131
|
|
else
|
132
|
|
{
|
133
|
|
returnValue = invocation.Method.Invoke( _connection, arguments);
|
134
|
|
}
|
135
|
|
|
136
|
|
return returnValue;
|
137
|
|
}
|
138
|
|
|
139
|
|
#endregion
|
140
|
|
}
|
141
|
|
}
|
142
|
|
|