Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 265 lines (241 sloc) 8.297 kb
3df4ba3 @dblock Hibernate hooked up web service and site.
authored
1 using System;
2 using System.Collections;
3 using System.Collections.Specialized;
4 using System.Data;
5 using System.Reflection;
6 using NHibernate;
7 using NHibernate.Cfg;
8 using System.Web;
9 using System.Diagnostics;
10
11 namespace DBlog.Data.Hibernate
12 {
13 /// <summary>
14 /// From Andrew Mayorov, http://wiki.nhibernate.org/display/NH/Using+NHibernate+with+ASP.Net
15 /// </summary>
16 public class Session
17 {
18 private static object _locker = new object();
19 private static NHibernate.Cfg.Configuration _config = null;
20 private static ISessionFactory _factory = null;
21 private static ISessionStorage _sessionsource = null;
22 private static bool http = false;
23
24 static Session()
25 {
26 try
27 {
28 System.Web.ProcessModelInfo.GetCurrentProcessInfo();
29 http = true;
30 }
d140c48 @dblock Lower security levels on hosted environments.
authored
31 catch (System.Web.HttpException)
32 {
33
34 }
35 catch (System.Security.SecurityException)
36 {
37 http = true;
38 }
39
3df4ba3 @dblock Hibernate hooked up web service and site.
authored
40 if (http)
41 _sessionsource = new HttpSessionSource();
42 else
43 _sessionsource = new ThreadSessionSource();
44 }
45
46 /// <summary>
47 /// Returns an instance of <see cref="NHibernate.Cfg.Configuration"/> object
48 /// containing the configuration for current application.
49 /// </summary>
50 /// <remarks>The object is being created upon first request for it.</remarks>
51 public static NHibernate.Cfg.Configuration Configuration
52 {
53 get
54 {
55 lock (_locker)
56 {
57 if (_config == null)
58 {
59 CreateConfiguration();
60 }
61 return _config;
62 }
63 }
64 }
65
66 private static void CreateConfiguration()
67 {
68 _config = new NHibernate.Cfg.Configuration();
8dc6d3b @dblock Upgraded to Visual Studio 2010.
authored
69 _config.Configure();
3df4ba3 @dblock Hibernate hooked up web service and site.
authored
70 // Add interceptor, if you need to.
71 // _config.Interceptor = new Interceptor();
72 }
73
74 /// <summary>
75 /// Returns an object implementing <see cref="NHibernate.ISessionFactory"/>
76 /// interface, used to create new sessions.
77 /// </summary>
78 /// <remarks>The factory is created upon first request for it.</remarks>
79 public static ISessionFactory Factory
80 {
81 get
82 {
83 if (_factory == null)
84 {
85 NHibernate.Cfg.Configuration config = Session.Configuration;
86 lock (_locker)
87 {
88 if (_factory == null)
89 _factory = config.BuildSessionFactory();
90 }
91 }
92 return _factory;
93 }
94 }
95
96 /// <summary>
97 /// Initializes internal state for handling new request. Call this method at
98 /// the beginning of the ASP.NET request.
99 /// </summary>
100 public static void BeginRequest()
101 {
102 //if (HasCurrent())
103 //{
104 // throw new ApplicationException("There mustn't be current session at the begining of a request.");
105 //}
106 }
107
108 /// <summary>
109 /// Finalizes the request. If current session exists the method flushes it and
110 /// closes. Call this method at the end of ASP.NET request.
111 /// </summary>
112 public static void EndRequest()
113 {
114 if (HasCurrent())
115 Close();
116 _sessionsource.Set(null);
117 }
118
119 /// <summary>
120 /// Returns a current session object.
121 /// </summary>
122 /// <remarks>The session object will be created upon first request for it.</remarks>
123 public static ISession Current
124 {
125 get
126 {
127 ISession s = _sessionsource.Get();
128 if (s == null)
129 {
130 s = Factory.OpenSession((IDbConnection)null);
131 s.FlushMode = FlushMode.Never;
132 s.Disconnect();
133 _sessionsource.Set(s);
134 }
135 return s;
136 }
137 }
138
139 /// <summary>
140 /// Tells if we currently have a session object or not.
141 /// </summary>
142 /// <returns><b>true</b> if there is a current session and <b>false</b> otherwise.</returns>
143 private static bool HasCurrent()
144 {
145 return _sessionsource.Get() != null;
146 }
147
148 public static IDisposable OpenConnection(IDbConnection connection)
149 {
150 return new SessionOpener(Current, connection);
151 }
152
153 /// <summary>
154 /// Flushes and closes current session.
155 /// </summary>
156 public static void CloseAndFlush()
157 {
158 ISession session = _sessionsource.Get();
159 if (session != null)
160 {
161 session.Flush();
162 session.Close();
163 _sessionsource.Set(null);
164 }
165 }
166
167 /// <summary>
168 /// Closes current session.
169 /// </summary>
170 /// <remarks><b>N.B.</b> Close do not flush the session.</remarks>
171 public static void Close()
172 {
173 ISession session = _sessionsource.Get();
174 if (session != null)
175 {
176 session.Close();
177 session.Dispose();
178 GC.Collect();
179 GC.WaitForPendingFinalizers();
180 _sessionsource.Set(null);
181 }
182 }
183
184 /// <summary>
185 /// Flushes current session.
186 /// </summary>
187 public static void Flush()
188 {
189 ISession session = _sessionsource.Get();
190 if (session != null)
191 session.Flush();
192 }
193
194 /// <summary>
195 /// Begins new COM+ transaction.
196 /// </summary>
197 /// <returns>Transaction object allowing to set transaction vote.</returns>
198 /// <remarks>
199 /// If current session exists, it will be closed first.<br/>
200 /// Returned transaction object implements IDisposable and <b>must</b> be disposed
201 /// after use in order to finish the transaction.
202 /// </remarks>
203 /// <example>
204 /// <code>
205 /// using( Transaction tx = Session.BeginTransaction() )
206 /// {
207 /// ...
208 /// }
209 /// </code>
210 /// </example>
211 public static Transaction BeginTransaction()
212 {
213 // Close();
214 return new Transaction();
215 }
216
217 private abstract class ISessionStorage
218 {
219 public abstract ISession Get();
220 public abstract void Set(ISession value);
221 }
222
223 /// <summary>
224 /// Stores a session in the <see cref="HttpContext.Items" /> collection.
225 /// </summary>
226 private class HttpSessionSource : ISessionStorage
227 {
228 public override ISession Get()
229 {
230 if (HttpContext.Current == null)
231 return null;
232
233 return (ISession)HttpContext.Current.Items["sdf.Persist.session"];
234 }
235
236 public override void Set(ISession value)
237 {
238 if (HttpContext.Current != null)
239 {
240 HttpContext.Current.Items["sdf.Persist.session"] = value;
241 }
242 }
243 }
244
245 /// <summary>
246 /// Stores a session in the thread-static class member.
247 /// </summary>
248 private class ThreadSessionSource : ISessionStorage
249 {
250 [ThreadStatic]
251 private static ISession _session = null;
252
253 public override ISession Get()
254 {
255 return _session;
256 }
257
258 public override void Set(ISession value)
259 {
260 _session = value;
261 }
262 }
263 }
264 }
Something went wrong with that request. Please try again.