My favorites | Sign in
Project Home Wiki Issues Source
Repository:
Checkout   Browse   Changes   Clones    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.IO;
using System.Web;
using log4net;
using NHibernate;
using uNhAddIns.SessionEasier;

namespace uNhAddIns.Web
{
public class NHSessionWebModule : IHttpModule
{
private static readonly ILog log = LogManager.GetLogger(typeof (NHSessionWebModule));
private static readonly string[] NoPersistenceFileExtensions = new string[] { ".jpg", ".gif", ".png", ".css", ".js", ".swf", ".xap" };
private ISessionFactoryProvider sfp;

#region Implementation of IHttpModule

public void Init(HttpApplication context)
{
log.Debug("Obtaining SessionFactoryProvider from Application context.");

sfp = context.Application[Commons.SessionFactoryProviderKey] as ISessionFactoryProvider;
if (sfp == null)
{
throw new HibernateException("Couldn't obtain SessionFactoryProvider from WebApplicationContext.");
}

context.BeginRequest += ApplicationBeginRequest;
context.EndRequest += ApplicationEndRequest;
}

public void Dispose() {}

private void ApplicationBeginRequest(object sender, EventArgs e)
{
if (!RequestMayNeedIterationWithPersistence(sender as HttpApplication))
{
return;
}
foreach (ISessionFactory factory in sfp)
{
factory.GetCurrentSession().BeginTransaction();
}
}

private void ApplicationEndRequest(object sender, EventArgs e)
{
if (!RequestMayNeedIterationWithPersistence(sender as HttpApplication))
{
return;
}
foreach (ISessionFactory factory in sfp)
{
ISession session = factory.GetCurrentSession();
try
{
if (session.IsOpen && session.Transaction.IsActive)
{
session.Transaction.Commit();
}
}
catch (Exception)
{
session.Transaction.Rollback();
throw;
}
finally
{
session.Dispose();
}
}
}

private static bool RequestMayNeedIterationWithPersistence(HttpApplication application)
{
if (application == null)
{
return false;
}
HttpContext context = application.Context;
if (context == null)
{
return false;
}
string fileExtension = Path.GetExtension(context.Request.PhysicalPath);
return fileExtension != null && Array.IndexOf(NoPersistenceFileExtensions, fileExtension.ToLower()) < 0;
}

#endregion
}
}

Change log

1719f7bac85e by Fabio Maulo <fabiomaulo> on Dec 9, 2010   Diff
Modified filter to open session
Go to: 
Sign in to write a code review

Older revisions

d232519da739 by Fabio Maulo <fabiomaulo> on Apr 21, 2009   Diff
Minor
4ae6903241b8 by Fabio Maulo <fabiomaulo> on Apr 4, 2009   Diff
Minor (only to not lost a little
snippet)
b181b4f32107 by Fabio Maulo <fabiomaulo> on Oct 3, 2008   Diff
Refactoring (moved Commons)
All revisions of this file

File info

Size: 2338 bytes, 91 lines
Powered by Google Project Hosting