Added RedirectUsingNamedRoute
[castle.git] / Services / Transaction / Castle.Services.Transaction / StandardTransaction.cs
blobbfd0d37ebefe4d9c2ae5c1e01c7a98e5a97a9e1e
1 // Copyright 2004-2008 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;
18 using System.Collections;
20 /// <summary>
21 /// Implements a transaction root.
22 /// </summary>
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);
54 children.Add(child);
56 return child;
59 public override bool IsChildTransaction
61 get { return false; }
64 public override void Commit()
66 if (rollbackOnly)
68 throw new TransactionException("Can't commit as transaction was marked as 'rollback only'");
71 try
73 base.Commit();
75 catch (TransactionException transactionError)
77 if (onTransactionFailed != null)
79 try
81 onTransactionFailed(this, transactionError);
83 catch (Exception e)
85 // Log exception & swallow it
86 Logger.Warn("An error occured while notifying caller about transaction commit failure.", e);
89 throw;
92 if (onTransactionCommitted != null) onTransactionCommitted(this);
95 public override void Rollback()
97 try
99 base.Rollback();
101 catch (TransactionException transactionError)
103 if (onTransactionFailed != null)
107 onTransactionFailed(this, transactionError);
109 catch (Exception e)
111 // Log exception & swallow it
112 Logger.Warn("An error occured while notifying caller about transaction rollback failure.", e);
115 throw;
118 if (onTransactionRolledback != null) onTransactionRolledback(this);
121 public override void SetRollbackOnly()
123 rollbackOnly = true;
126 public override bool IsRollbackOnlySet
128 get { return rollbackOnly; }
132 /// <summary>
133 /// Emulates a standalone transaction but in fact it
134 /// just propages a transaction.
135 /// </summary>
136 public class ChildTransaction : StandardTransaction
138 private StandardTransaction _parent;
140 public ChildTransaction(StandardTransaction parent) :
141 base(parent.TransactionMode, parent.IsolationMode, parent.DistributedTransaction)
143 _parent = parent;
146 public override void Enlist(IResource resource)
148 _parent.Enlist(resource);
151 public override void Begin()
153 // Ignored
156 public override void Rollback()
158 // Vote as rollback
160 _parent.SetRollbackOnly();
163 public override void Commit()
165 // Vote as commit
168 public override void SetRollbackOnly()
170 Rollback();
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
185 get { return true; }
188 public override bool IsRollbackOnlySet
190 get { return _parent.IsRollbackOnlySet; }