1 // Copyright 2004-2007 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
17 using System
.Collections
;
20 /// Implements a transaction root.
22 public class StandardTransaction
: AbstractTransaction
24 private readonly TransactionDelegate onTransactionCommitted
;
25 private readonly TransactionDelegate onTransactionRolledback
;
27 private IList children
= ArrayList
.Synchronized( new ArrayList() );
28 private bool rollbackOnly
;
30 protected StandardTransaction()
34 public StandardTransaction(TransactionDelegate onTransactionCommitted
, TransactionDelegate onTransactionRolledback
,
35 TransactionMode transactionMode
, IsolationMode isolationMode
, bool distributedTransaction
) :
36 this(transactionMode
, isolationMode
, distributedTransaction
)
38 this.onTransactionCommitted
= onTransactionCommitted
;
39 this.onTransactionRolledback
= onTransactionRolledback
;
42 public StandardTransaction(TransactionMode transactionMode
, IsolationMode isolationMode
, bool distributedTransaction
) :
43 base(transactionMode
, isolationMode
, distributedTransaction
)
47 public StandardTransaction
CreateChildTransaction()
49 ChildTransaction child
= new ChildTransaction(this);
56 public override bool IsChildTransaction
61 public override void Commit()
65 throw new TransactionException("Can't commit as transaction was marked as 'rollback only'");
70 if (onTransactionCommitted
!= null) onTransactionCommitted(this);
73 public override void Rollback()
77 if (onTransactionRolledback
!= null) onTransactionRolledback(this);
80 public override void SetRollbackOnly()
85 public override bool IsRollbackOnlySet
87 get { return rollbackOnly; }
92 /// Emulates a standalone transaction but in fact it
93 /// just propages a transaction.
95 public class ChildTransaction
: StandardTransaction
97 private StandardTransaction _parent
;
99 public ChildTransaction(StandardTransaction parent
) :
100 base(parent
.TransactionMode
, parent
.IsolationMode
, parent
.DistributedTransaction
)
105 public override void Enlist(IResource resource
)
107 _parent
.Enlist(resource
);
110 public override void Begin()
115 public override void Rollback()
119 _parent
.SetRollbackOnly();
122 public override void Commit()
127 public override void SetRollbackOnly()
132 public override void RegisterSynchronization(ISynchronization synchronization
)
134 _parent
.RegisterSynchronization(synchronization
);
137 public override IDictionary Context
139 get { return _parent.Context; }
142 public override bool IsChildTransaction
147 public override bool IsRollbackOnlySet
149 get { return _parent.IsRollbackOnlySet; }