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 Imports
|
28
|
|
using System;
|
29
|
|
using System.Data;
|
30
|
|
|
31
|
|
using IBatisNet.Common;
|
32
|
|
using IBatisNet.DataMapper.Exceptions;
|
33
|
|
|
34
|
|
using log4net;
|
35
|
|
#endregion
|
36
|
|
|
37
|
|
|
38
|
|
namespace IBatisNet.DataMapper
|
39
|
|
{
|
40
|
|
|
41
|
|
|
42
|
|
|
43
|
|
[Serializable]
|
44
|
|
public class SqlMapSession : IDalSession
|
45
|
|
{
|
46
|
|
#region Fields
|
47
|
|
private static readonly ILog _logger = LogManager.GetLogger( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType );
|
48
|
|
private SqlMapper _sqlMapper = null;
|
49
|
|
private DataSource _dataSource = null;
|
50
|
|
|
51
|
|
#endregion
|
52
|
|
|
53
|
|
#region Constructor (s) / Destructor
|
54
|
|
|
55
|
|
|
56
|
|
|
57
|
|
|
58
|
5
|
public SqlMapSession(SqlMapper sqlMapper)
|
59
|
|
{
|
60
|
5
|
_dataSource = sqlMapper.DataSource;
|
61
|
5
|
_sqlMapper = sqlMapper;
|
62
|
|
}
|
63
|
|
|
64
|
|
|
65
|
|
|
66
|
|
|
67
|
|
|
68
|
20678
|
public SqlMapSession(DataSource dataSource)
|
69
|
|
{
|
70
|
20678
|
_dataSource = dataSource;
|
71
|
|
}
|
72
|
|
#endregion
|
73
|
|
|
74
|
|
#region IDalSession Members
|
75
|
|
|
76
|
|
#region Fields
|
77
|
|
private bool _isOpenTransaction = false;
|
78
|
|
|
79
|
|
|
80
|
|
|
81
|
|
private bool _consistent = false;
|
82
|
|
|
83
|
|
|
84
|
|
|
85
|
|
|
86
|
|
private IDbConnection _connection = null;
|
87
|
|
|
88
|
|
|
89
|
|
|
90
|
|
|
91
|
|
private IDbTransaction _transaction = null;
|
92
|
|
#endregion
|
93
|
|
|
94
|
|
#region Properties
|
95
|
|
|
96
|
|
|
97
|
|
|
98
|
|
public DataSource DataSource
|
99
|
|
{
|
100
|
329702
|
get { return _dataSource; }
|
101
|
|
}
|
102
|
|
|
103
|
|
|
104
|
|
|
105
|
|
|
106
|
0
|
public IDbConnection Connection
|
107
|
|
{
|
108
|
|
get { return _connection; }
|
109
|
|
}
|
110
|
|
|
111
|
|
|
112
|
|
|
113
|
|
|
114
|
0
|
public IDbTransaction Transaction
|
115
|
|
{
|
116
|
|
get { return _transaction; }
|
117
|
|
}
|
118
|
|
|
119
|
|
|
120
|
|
|
121
|
|
|
122
|
|
private bool Consistent
|
123
|
|
{
|
124
|
1
|
set
|
125
|
|
{
|
126
|
1
|
_consistent = value;
|
127
|
|
}
|
128
|
|
}
|
129
|
|
#endregion
|
130
|
|
|
131
|
|
#region Methods
|
132
|
|
|
133
|
|
|
134
|
|
|
135
|
|
|
136
|
|
|
137
|
|
|
138
|
1
|
public void Complete()
|
139
|
|
{
|
140
|
1
|
this.Consistent = true;
|
141
|
|
}
|
142
|
|
|
143
|
|
|
144
|
|
|
145
|
|
|
146
|
283
|
public void OpenConnection()
|
147
|
|
{
|
148
|
283
|
if (_connection == null)
|
149
|
|
{
|
150
|
283
|
_connection = _dataSource.Provider.GetConnection();
|
151
|
283
|
_connection.ConnectionString = _dataSource.ConnectionString;
|
152
|
283
|
try
|
153
|
|
{
|
154
|
283
|
_connection.Open();
|
155
|
283
|
if (_logger.IsDebugEnabled)
|
156
|
|
{
|
157
|
283
|
_logger.Debug( string.Format("Open Connection \"{0}\" to \"{1}\".", _connection.GetHashCode().ToString(), _dataSource.Provider.Description) );
|
158
|
|
}
|
159
|
|
}
|
160
|
|
catch(Exception ex)
|
161
|
|
{
|
162
|
0
|
throw new DataMapperException( string.Format("Unable to open connection to \"{0}\".", _dataSource.Provider.Description), ex );
|
163
|
|
}
|
164
|
|
}
|
165
|
0
|
else if (_connection.State != ConnectionState.Open)
|
166
|
|
{
|
167
|
|
try
|
168
|
|
{
|
169
|
|
_connection.Open();
|
170
|
|
if (_logger.IsDebugEnabled)
|
171
|
|
{
|
172
|
|
_logger.Debug(string.Format("Open Connection \"{0}\" to \"{1}\".", _connection.GetHashCode().ToString(), _dataSource.Provider.Description) );
|
173
|
|
}
|
174
|
|
}
|
175
|
|
catch(Exception ex)
|
176
|
|
{
|
177
|
|
throw new DataMapperException(string.Format("Unable to open connection to \"{0}\".", _dataSource.Provider.Description), ex );
|
178
|
|
}
|
179
|
|
}
|
180
|
|
}
|
181
|
|
|
182
|
|
|
183
|
|
|
184
|
|
|
185
|
283
|
public void CloseConnection()
|
186
|
|
{
|
187
|
283
|
if ( (_connection != null) && (_connection.State != ConnectionState.Closed) )
|
188
|
|
{
|
189
|
283
|
_connection.Close();
|
190
|
283
|
if (_logger.IsDebugEnabled)
|
191
|
|
{
|
192
|
|
|
193
|
283
|
_logger.Debug(string.Format("Close Connection \"{0}\" to \"{1}\".", _connection.GetHashCode().ToString(), _dataSource.Provider.Description));
|
194
|
|
}
|
195
|
283
|
_connection.Dispose();
|
196
|
|
}
|
197
|
283
|
_connection = null;
|
198
|
|
}
|
199
|
|
|
200
|
|
|
201
|
|
|
202
|
|
|
203
|
4
|
public void BeginTransaction()
|
204
|
|
{
|
205
|
4
|
if (_connection == null || _connection.State != ConnectionState.Open)
|
206
|
|
{
|
207
|
4
|
OpenConnection();
|
208
|
|
}
|
209
|
4
|
_transaction = _connection.BeginTransaction();
|
210
|
4
|
if (_logger.IsDebugEnabled)
|
211
|
|
{
|
212
|
4
|
_logger.Debug("Begin Transaction.");
|
213
|
|
}
|
214
|
4
|
_isOpenTransaction = true;
|
215
|
|
}
|
216
|
|
|
217
|
|
|
218
|
|
|
219
|
|
|
220
|
|
|
221
|
0
|
public void BeginTransaction(bool openConnection)
|
222
|
|
{
|
223
|
|
if (openConnection)
|
224
|
|
{
|
225
|
|
this.BeginTransaction();
|
226
|
|
}
|
227
|
|
else
|
228
|
|
{
|
229
|
|
if (_connection == null || _connection.State != ConnectionState.Open)
|
230
|
|
{
|
231
|
|
throw new DataMapperException("SqlMapSession could not invoke BeginTransaction(). A Connection must be started. Call OpenConnection() first.");
|
232
|
|
}
|
233
|
|
_transaction = _connection.BeginTransaction();
|
234
|
|
if (_logger.IsDebugEnabled)
|
235
|
|
{
|
236
|
|
_logger.Debug("Begin Transaction.");
|
237
|
|
}
|
238
|
|
_isOpenTransaction = true;
|
239
|
|
}
|
240
|
|
}
|
241
|
|
|
242
|
|
|
243
|
|
|
244
|
|
|
245
|
|
|
246
|
|
|
247
|
|
|
248
|
0
|
public void BeginTransaction(IsolationLevel isolationLevel)
|
249
|
|
{
|
250
|
|
if (_connection == null || _connection.State != ConnectionState.Open)
|
251
|
|
{
|
252
|
|
OpenConnection();
|
253
|
|
}
|
254
|
|
_transaction = _connection.BeginTransaction(isolationLevel);
|
255
|
|
if (_logger.IsDebugEnabled)
|
256
|
|
{
|
257
|
|
_logger.Debug("Begin Transaction.");
|
258
|
|
}
|
259
|
|
_isOpenTransaction = true;
|
260
|
|
}
|
261
|
|
|
262
|
|
|
263
|
|
|
264
|
|
|
265
|
|
|
266
|
|
|
267
|
|
|
268
|
0
|
public void BeginTransaction(bool openConnection, IsolationLevel isolationLevel)
|
269
|
|
{
|
270
|
|
if (openConnection)
|
271
|
|
{
|
272
|
|
this.BeginTransaction(isolationLevel);
|
273
|
|
}
|
274
|
|
else
|
275
|
|
{
|
276
|
|
if (_connection == null || _connection.State != ConnectionState.Open)
|
277
|
|
{
|
278
|
|
throw new DataMapperException("SqlMapSession could not invoke StartTransaction(). A Connection must be started. Call OpenConnection() first.");
|
279
|
|
}
|
280
|
|
_transaction = _connection.BeginTransaction(isolationLevel);
|
281
|
|
if (_logger.IsDebugEnabled)
|
282
|
|
{
|
283
|
|
_logger.Debug("Begin Transaction.");
|
284
|
|
}
|
285
|
|
_isOpenTransaction = true;
|
286
|
|
}
|
287
|
|
}
|
288
|
|
|
289
|
|
|
290
|
|
|
291
|
|
|
292
|
|
|
293
|
|
|
294
|
|
|
295
|
|
|
296
|
3
|
public void CommitTransaction()
|
297
|
|
{
|
298
|
3
|
if (_logger.IsDebugEnabled)
|
299
|
|
{
|
300
|
3
|
_logger.Debug("Commit Transaction.");
|
301
|
|
}
|
302
|
3
|
_transaction.Commit();
|
303
|
3
|
_transaction.Dispose();
|
304
|
3
|
if (_connection.State != ConnectionState.Closed)
|
305
|
|
{
|
306
|
3
|
this.CloseConnection();
|
307
|
|
}
|
308
|
|
}
|
309
|
|
|
310
|
|
|
311
|
|
|
312
|
|
|
313
|
|
|
314
|
0
|
public void CommitTransaction(bool closeConnection)
|
315
|
|
{
|
316
|
|
if (closeConnection)
|
317
|
|
{
|
318
|
|
this.CommitTransaction();
|
319
|
|
}
|
320
|
|
else
|
321
|
|
{
|
322
|
|
_transaction.Commit();
|
323
|
|
if (_logger.IsDebugEnabled)
|
324
|
|
{
|
325
|
|
_logger.Debug("Commit Transaction.");
|
326
|
|
}
|
327
|
|
_transaction.Dispose();
|
328
|
|
}
|
329
|
|
}
|
330
|
|
|
331
|
|
|
332
|
|
|
333
|
|
|
334
|
|
|
335
|
|
|
336
|
|
|
337
|
1
|
public void RollBackTransaction()
|
338
|
|
{
|
339
|
1
|
_transaction.Rollback();
|
340
|
1
|
if (_logger.IsDebugEnabled)
|
341
|
|
{
|
342
|
1
|
_logger.Debug("RollBack Transaction.");
|
343
|
|
}
|
344
|
1
|
_transaction.Dispose();
|
345
|
1
|
_transaction = null;
|
346
|
1
|
if (_connection.State != ConnectionState.Closed)
|
347
|
|
{
|
348
|
1
|
this.CloseConnection();
|
349
|
|
}
|
350
|
|
}
|
351
|
|
|
352
|
|
|
353
|
|
|
354
|
|
|
355
|
|
|
356
|
0
|
public void RollBackTransaction(bool closeConnection)
|
357
|
|
{
|
358
|
|
if (closeConnection)
|
359
|
|
{
|
360
|
|
this.RollBackTransaction();
|
361
|
|
}
|
362
|
|
else
|
363
|
|
{
|
364
|
|
if (_logger.IsDebugEnabled)
|
365
|
|
{
|
366
|
|
_logger.Debug("RollBack Transaction.");
|
367
|
|
}
|
368
|
|
_transaction.Rollback();
|
369
|
|
_transaction.Dispose();
|
370
|
|
_transaction = null;
|
371
|
|
}
|
372
|
|
}
|
373
|
|
|
374
|
|
|
375
|
|
|
376
|
|
|
377
|
|
|
378
|
|
|
379
|
32485
|
public IDbCommand CreateCommand(CommandType commandType)
|
380
|
|
{
|
381
|
32485
|
IDbCommand command = null;
|
382
|
|
|
383
|
32485
|
command = _dataSource.Provider.GetCommand();
|
384
|
|
|
385
|
32485
|
command.CommandType = commandType;
|
386
|
|
|
387
|
32485
|
command.Connection = _connection;
|
388
|
|
|
389
|
32485
|
if (_transaction != null)
|
390
|
|
{
|
391
|
6
|
try
|
392
|
|
{
|
393
|
6
|
command.Transaction = _transaction;
|
394
|
|
}
|
395
|
|
catch
|
396
|
|
{}
|
397
|
|
}
|
398
|
|
|
399
|
32485
|
if (_connection!= null)
|
400
|
|
{
|
401
|
355
|
try
|
402
|
|
{
|
403
|
355
|
command.CommandTimeout = _connection.ConnectionTimeout;
|
404
|
|
}
|
405
|
|
catch(NotSupportedException e)
|
406
|
|
{
|
407
|
0
|
if (_logger.IsInfoEnabled)
|
408
|
|
{
|
409
|
|
_logger.Info(e.Message);
|
410
|
|
}
|
411
|
|
}
|
412
|
|
}
|
413
|
|
|
414
|
32485
|
return command;
|
415
|
|
}
|
416
|
|
|
417
|
|
|
418
|
|
|
419
|
|
|
420
|
|
|
421
|
0
|
public IDataParameter CreateDataParameter()
|
422
|
|
{
|
423
|
|
IDataParameter dataParameter = null;
|
424
|
|
|
425
|
|
dataParameter = _dataSource.Provider.GetDataParameter();
|
426
|
|
|
427
|
|
return dataParameter;
|
428
|
|
}
|
429
|
|
|
430
|
|
|
431
|
|
|
432
|
|
|
433
|
|
|
434
|
0
|
public IDbDataAdapter CreateDataAdapter()
|
435
|
|
{
|
436
|
|
return _dataSource.Provider.GetDataAdapter();
|
437
|
|
}
|
438
|
|
|
439
|
|
|
440
|
|
|
441
|
|
|
442
|
|
|
443
|
|
|
444
|
0
|
public IDbDataAdapter CreateDataAdapter(IDbCommand command)
|
445
|
|
{
|
446
|
|
IDbDataAdapter dataAdapter = null;
|
447
|
|
|
448
|
|
dataAdapter = _dataSource.Provider.GetDataAdapter();
|
449
|
|
dataAdapter.SelectCommand = command;
|
450
|
|
|
451
|
|
return dataAdapter;
|
452
|
|
}
|
453
|
|
#endregion
|
454
|
|
|
455
|
|
#endregion
|
456
|
|
|
457
|
|
#region IDisposable Members
|
458
|
|
|
459
|
|
|
460
|
|
|
461
|
|
|
462
|
2
|
public void Dispose()
|
463
|
|
{
|
464
|
2
|
if (_logger.IsDebugEnabled)
|
465
|
|
{
|
466
|
2
|
_logger.Debug("Dispose SqlMapSession");
|
467
|
|
}
|
468
|
2
|
if (_isOpenTransaction == false)
|
469
|
|
{
|
470
|
1
|
if (_connection.State != ConnectionState.Closed)
|
471
|
|
{
|
472
|
1
|
_sqlMapper.CloseConnection();
|
473
|
|
}
|
474
|
|
}
|
475
|
|
else
|
476
|
|
{
|
477
|
1
|
if (_consistent)
|
478
|
|
{
|
479
|
1
|
_sqlMapper.CommitTransaction();
|
480
|
|
}
|
481
|
|
else
|
482
|
|
{
|
483
|
0
|
if (_connection.State != ConnectionState.Closed)
|
484
|
|
{
|
485
|
|
_sqlMapper.RollBackTransaction();
|
486
|
|
}
|
487
|
|
}
|
488
|
|
}
|
489
|
|
}
|
490
|
|
|
491
|
|
#endregion
|
492
|
|
}
|
493
|
|
}
|
494
|
|
|