Added DictionaryAdapter.build.
[castle.git] / Services / Transaction / Castle.Services.Transaction / StandardTransaction.cs
blob881655d9868824f696c3a0af2a7b7f18727e0e68
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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;
19 /// <summary>
20 /// Implements a transaction root.
21 /// </summary>
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);
51 children.Add(child);
53 return child;
56 public override bool IsChildTransaction
58 get { return false; }
61 public override void Commit()
63 if (rollbackOnly)
65 throw new TransactionException("Can't commit as transaction was marked as 'rollback only'");
68 base.Commit();
70 if (onTransactionCommitted != null) onTransactionCommitted(this);
73 public override void Rollback()
75 base.Rollback();
77 if (onTransactionRolledback != null) onTransactionRolledback(this);
80 public override void SetRollbackOnly()
82 rollbackOnly = true;
85 public override bool IsRollbackOnlySet
87 get { return rollbackOnly; }
91 /// <summary>
92 /// Emulates a standalone transaction but in fact it
93 /// just propages a transaction.
94 /// </summary>
95 public class ChildTransaction : StandardTransaction
97 private StandardTransaction _parent;
99 public ChildTransaction(StandardTransaction parent) :
100 base(parent.TransactionMode, parent.IsolationMode, parent.DistributedTransaction)
102 _parent = parent;
105 public override void Enlist(IResource resource)
107 _parent.Enlist(resource);
110 public override void Begin()
112 // Ignored
115 public override void Rollback()
117 // Vote as rollback
119 _parent.SetRollbackOnly();
122 public override void Commit()
124 // Vote as commit
127 public override void SetRollbackOnly()
129 Rollback();
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
144 get { return true; }
147 public override bool IsRollbackOnlySet
149 get { return _parent.IsRollbackOnlySet; }