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
.ActiveRecord
18 using System
.Collections
.Generic
;
19 using Castle
.ActiveRecord
.Framework
.Scopes
;
23 /// Implementation of <see cref="ISessionScope"/> to
24 /// augment performance by caching the session, thus
25 /// avoiding too much opens/flushes/closes.
27 public class SessionScope
: AbstractScope
30 /// Is set to true if the session went stalled due to an error (usually db operations)
32 private bool hasSessionError
;
35 /// Initializes a new instance of the <see cref="SessionScope"/> class.
37 /// <param name="flushAction">The flush action.</param>
38 /// <param name="type">The type.</param>
39 protected SessionScope(FlushAction flushAction
, SessionScopeType type
) : base(flushAction
, type
)
44 /// Initializes a new instance of the <see cref="SessionScope"/> class.
46 public SessionScope() : this(FlushAction
.Auto
)
51 /// Initializes a new instance of the <see cref="SessionScope"/> class.
53 /// <param name="flushAction">The flush action.</param>
54 public SessionScope(FlushAction flushAction
) : base(flushAction
, SessionScopeType
.Simple
)
59 /// Deprecated! Disposes the specified discard changes. Please use new SessionScope(FlushAction.Never)
61 /// <param name="discardChanges">if set to <c>true</c> [discard changes].</param>
62 [Obsolete("This useage is deprecated - please use new SessionScope(FlushAction.Never)")]
63 public void Dispose(bool discardChanges
)
65 ThreadScopeAccessor
.Instance
.UnRegisterScope(this);
67 PerformDisposal(key2Session
.Values
, !discardChanges
, true);
74 /// Performs the disposal.
76 /// <param name="sessions">The sessions.</param>
77 protected override void PerformDisposal(ICollection
<ISession
> sessions
)
79 if (hasSessionError
|| FlushAction
== FlushAction
.Never
)
81 PerformDisposal(sessions
, false, true);
83 else if (FlushAction
== FlushAction
.Auto
)
85 PerformDisposal(sessions
, true, true);
90 /// This is called when an action on a session fails
92 /// <param name="session">The session</param>
93 public override void FailSession(ISession session
)
95 hasSessionError
= true;
99 /// Gets or sets a flag indicating whether this instance has session error.
102 /// <c>true</c> if this instance has session error; otherwise, <c>false</c>.
104 public bool HasSessionError
106 get { return hasSessionError; }
107 set { hasSessionError = true; }
111 /// Gets the current scope
113 /// <value>The current.</value>
114 public static ISessionScope Current
118 if (ThreadScopeAccessor
.Instance
.HasInitializedScope
)
120 return ThreadScopeAccessor
.Instance
.GetRegisteredScope();