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
|
public class TransactionManager
{
private SqlConnection _connection;
private List<object> _adapters;
private SqlTransaction _transaction;
public SqlTransaction Transaction
{
get { return this._transaction; }
set { this._transaction = value;}
}
//Konstruktor
public TransactionManager() { _adapters = new List<object>(); }
/// <summary>
/// Hier kann ein Adapter registriert werden.
/// </summary>
/// <param name="adapter"></param>
public void registerAdapter(object adapter)
{
// suche Connection
PropertyInfo conn = adapter.GetType().GetProperty("Connection", BindingFlags.Instance | BindingFlags.NonPublic);
if (this._connection == null)
_connection = conn.GetValue(adapter, null) as SqlConnection;
else
conn.SetValue(adapter, _connection, null);
_adapters.Add(adapter);
}
public void registerAdapter(object[] adapters)
{
foreach (object a in adapters)
this.registerAdapter(a);
}
public void BeginTransaction()
{
if (this._connection == null)
throw new Exception("no Adapter registered");
if (this._connection.State != ConnectionState.Open) this._connection.Open();
// Create the transaction and assign it to the Transaction property
this.Transaction = this._connection.BeginTransaction();
// set the Transaction for the others too
foreach (object curAdapter in _adapters)
{
PropertyInfo adapterProp = curAdapter.GetType().GetProperty("Adapter", BindingFlags.Instance | BindingFlags.NonPublic);
SqlDataAdapter dataAdapter = adapterProp.GetValue(curAdapter, null) as SqlDataAdapter;
if (dataAdapter.InsertCommand != null)
dataAdapter.InsertCommand.Transaction = this.Transaction;
if (dataAdapter.UpdateCommand != null)
dataAdapter.UpdateCommand.Transaction = this.Transaction;
if (dataAdapter.DeleteCommand != null)
dataAdapter.DeleteCommand.Transaction = this.Transaction;
PropertyInfo commandCollProp = curAdapter.GetType().GetProperty("CommandCollection", BindingFlags.Instance | BindingFlags.NonPublic);
SqlCommand[] commandCollection = commandCollProp.GetValue(curAdapter, null) as SqlCommand[];
foreach (SqlCommand command in commandCollection)
{
command.Transaction = this.Transaction;
}
}
}
public void Comit()
{
this.Transaction.Commit();
this._connection.Close();
}
public void Rollback()
{
this.Transaction.Rollback();
this._connection.Close();
}
}
// möglicher Aufruf
TransactionManager transactionManager = new TransactionManager();
transactionManager.registerAdapter(new object[] { ua, cA, ceA });
transactionManager.BeginTransaction();
try{
// do something
ua.dosomething();
cA.dosomething();
transactionManager.Comit();
}
catch
{
transactionManager.Rollback();
throw;
}
|