1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Services
.Transaction
18 using System
.Collections
;
21 /// Implements a transaction root.
23 public class StandardTransaction
: AbstractTransaction
25 private readonly TransactionDelegate onTransactionCommitted
;
26 private readonly TransactionDelegate onTransactionRolledback
;
27 private readonly TransactionErrorDelegate onTransactionFailed
;
29 private IList children
= ArrayList
.Synchronized( new ArrayList() );
30 private bool rollbackOnly
;
32 protected StandardTransaction()
36 public StandardTransaction(TransactionDelegate onTransactionCommitted
, TransactionDelegate onTransactionRolledback
,
37 TransactionErrorDelegate onTransactionFailed
, TransactionMode transactionMode
, IsolationMode isolationMode
, bool distributedTransaction
) :
38 this(transactionMode
, isolationMode
, distributedTransaction
)
40 this.onTransactionCommitted
= onTransactionCommitted
;
41 this.onTransactionRolledback
= onTransactionRolledback
;
42 this.onTransactionFailed
= onTransactionFailed
;
45 public StandardTransaction(TransactionMode transactionMode
, IsolationMode isolationMode
, bool distributedTransaction
) :
46 base(transactionMode
, isolationMode
, distributedTransaction
)
50 public StandardTransaction
CreateChildTransaction()
52 ChildTransaction child
= new ChildTransaction(this);
59 public override bool IsChildTransaction
64 public override void Commit()
68 throw new TransactionException("Can't commit as transaction was marked as 'rollback only'");
75 catch (TransactionException transactionError
)
77 if (onTransactionFailed
!= null)
81 onTransactionFailed(this, transactionError
);
85 // Log exception & swallow it
86 Logger
.Warn("An error occured while notifying caller about transaction commit failure.", e
);
92 if (onTransactionCommitted
!= null) onTransactionCommitted(this);
95 public override void Rollback()
101 catch (TransactionException transactionError
)
103 if (onTransactionFailed
!= null)
107 onTransactionFailed(this, transactionError
);
111 // Log exception & swallow it
112 Logger
.Warn("An error occured while notifying caller about transaction rollback failure.", e
);
118 if (onTransactionRolledback
!= null) onTransactionRolledback(this);
121 public override void SetRollbackOnly()
126 public override bool IsRollbackOnlySet
128 get { return rollbackOnly; }
133 /// Emulates a standalone transaction but in fact it
134 /// just propages a transaction.
136 public class ChildTransaction
: StandardTransaction
138 private StandardTransaction _parent
;
140 public ChildTransaction(StandardTransaction parent
) :
141 base(parent
.TransactionMode
, parent
.IsolationMode
, parent
.DistributedTransaction
)
146 public override void Enlist(IResource resource
)
148 _parent
.Enlist(resource
);
151 public override void Begin()
156 public override void Rollback()
160 _parent
.SetRollbackOnly();
163 public override void Commit()
168 public override void SetRollbackOnly()
173 public override void RegisterSynchronization(ISynchronization synchronization
)
175 _parent
.RegisterSynchronization(synchronization
);
178 public override IDictionary Context
180 get { return _parent.Context; }
183 public override bool IsChildTransaction
188 public override bool IsRollbackOnlySet
190 get { return _parent.IsRollbackOnlySet; }