Skip to content
Browse files

Use one session for the request

  • Loading branch information...
1 parent f9d608c commit b284efae61108af991221d948eb891e1310bc64b @fitzchak fitzchak committed Dec 27, 2011
Showing with 29 additions and 30 deletions.
  1. +6 −29 HibernatingRhinos.Loci.Common/Controllers/RavenController.cs
  2. +23 −1 RaccoonBlog.Web/Global.asax.cs
View
35 HibernatingRhinos.Loci.Common/Controllers/RavenController.cs
@@ -18,36 +18,13 @@ public static IDocumentStore DocumentStore
}
private static IDocumentStore _documentStore;
- public IDocumentSession RavenSession { get; protected set; }
-
- protected override void OnActionExecuting(ActionExecutingContext filterContext)
- {
- RavenSession = _documentStore.OpenSession();
- }
-
- // TODO: Consider re-applying https://github.com/ayende/RaccoonBlog/commit/ff954e563e6996d44eb59a28f0abb2d3d9305ffe
- protected override void OnActionExecuted(ActionExecutedContext filterContext)
- {
- CompleteSessionHandler(filterContext);
- }
-
- protected void CompleteSessionHandler(ActionExecutedContext filterContext)
+ public IDocumentSession RavenSession { get; protected set; }
+
+ protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
- if (filterContext.IsChildAction)
- return;
-
- using (RavenSession)
- {
- if (filterContext.Exception != null)
- return;
-
- if (RavenSession != null)
- RavenSession.SaveChanges();
- }
-
- TaskExecutor.StartExecuting();
- }
-
+ RavenSession = (IDocumentSession)HttpContext.Items["CurrentRequestRavenSession"];
+ }
+
protected HttpStatusCodeResult HttpNotModified()
{
return new HttpStatusCodeResult(304);
View
24 RaccoonBlog.Web/Global.asax.cs
@@ -18,7 +18,29 @@
namespace RaccoonBlog.Web
{
public class MvcApplication : HttpApplication
- {
+ {
+ public MvcApplication()
+ {
+ BeginRequest += (sender, args) =>
+ {
+ HttpContext.Current.Items["CurrentRequestRavenSession"] = RavenController.DocumentStore.OpenSession();
+ };
+ EndRequest += (sender, args) =>
+ {
+ using (var session = (IDocumentSession)HttpContext.Current.Items["CurrentRequestRavenSession"])
+ {
+ if (session == null)
+ return;
+
+ if (Server.GetLastError() != null)
+ return;
+
+ session.SaveChanges();
+ }
+ TaskExecutor.StartExecuting();
+ };
+ }
+
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());

0 comments on commit b284efa

Please sign in to comment.
Something went wrong with that request. Please try again.