I`m writing an ASP.NET MVC application using IIS 7.5 as web server. I would like to open a DB connection at the begin of any request and close this connection at the end of the request.
Therefore, in global.asax I specify the member
Public Shared conn As OleDbConnection
In the Application_BeginRequest handler I open the connection like this:
Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs)
Dim strConn As String = "myConnectionString..."
conn = New OleDbConnection(strConn)
conn.Open()
End Sub 'Application_BeginRequest
And in the Application_EndRequest handler I close it:
Sub Application_EndRequest(ByVal sender As Object, ByVal e As EventArgs)
conn.Close()
conn.Dispose()
End Sub 'Application_EndRequest
Then, maybe in the Index() method of the HomeController, I use the Db connection provided by the global.asax:
Dim _conn As OleDbConnection = MvcApplication.conn
'[then get or set data in the DB...]
I now experience a strange thing: The DB connection is opened properly when I completely reload a page from the server. When IIS delivers the page from cache, the DB connection is not openend and I get an error (although seemingly from my reporting tool the Application_BeginRequest handler IS entered). Is there a way to overcome this problem? I already tried setting the cache control via HTML metatags and by leveraging the following Q&A provided here: How to switch off caching for MVC requests but not for static files in IIS7? The problem still remains. Can anyone help? Many thanks, niewi