Search code examples
c#asp.net-mvcdb4o

What is causing this DatabaseFileLockedException when trying to open a db4o database in an ASP.NET MVC app?


I'm building a small web application with ASP.NET MVC 2, using db4o as a datastore.

I have added an HttpModule—as per the example here—to give the application access to the db4o database, and everything is working perfectly on my development machine under the VS2008 ASP.NET Development Server.

However, when I deploy the app to my web host and try to access it, I get a DatabaseFileLockedException at the line where the HttpModule tries to open the database file. But there should be nothing else accessing the file; indeed on first run of the app it will only just have been created when this exception gets thrown.

The web host's servers are running IIS 7 on Windows Server 2008, and the application is running under Full Trust. It is a sub-application, in case that makes any difference.

I can't work out why this error is occurring on the live server, but not locally on my development server. Can anyone help me out or suggest what I should do next?


Solution

  • That's a mistake in the example-code. It assumes that the HttpModule.Init is only called once, which isn't necessarily true. Depending how your application is configured, it can be called multiple times. To fix this, check in the HttpModule-Handler if the instance is already there:

    using System;
    using System.Configuration;
    using System.Web;
    using Db4objects.Db4o;
    
    namespace Db4oDoc.WebApp.Infrastructure
    {
        public class Db4oProvider : IHttpModule
        {
            private const string DataBaseInstance = "db4o-database-instance";
            private const string SessionKey = "db4o-session";
    
            // #example: open database when the application starts
            public void Init(HttpApplication context)
            {
                if (null==HttpContext.Current.Application[DataBaseInstance])
                {
                    HttpContext.Current.Application[DataBaseInstance] = OpenDatabase();
                }
                RegisterSessionCreation(context);
            }
    
            private IEmbeddedObjectContainer OpenDatabase()
            {
                string relativePath = ConfigurationSettings.AppSettings["DatabaseFileName"];
                string filePath = HttpContext.Current.Server.MapPath(relativePath);
                return Db4oEmbedded.OpenFile(filePath);
            }
            // #end example
    
            // #example: close the database when the application shuts down
            public void Dispose()
            {
                IDisposable toDispose = HttpContext.Current.Application[DataBaseInstance] as IDisposable;
                if (null != toDispose)
                {
                    toDispose.Dispose();
                }
            }
            // #end example
    
            // #example: provide access to the database
            public static IObjectContainer Database
            {
                get { return (IObjectContainer)HttpContext.Current.Items[SessionKey]; }
            }
            // #end example
    
            // #example: A object container per request
            private void RegisterSessionCreation(HttpApplication httpApplication)
            {
                httpApplication.BeginRequest += OpenSession;
                httpApplication.EndRequest += CloseSession;
            }
    
            private void OpenSession(object sender, EventArgs e)
            {
                IEmbeddedObjectContainer container =
                    (IEmbeddedObjectContainer)HttpContext.Current.Application[DataBaseInstance];
                IObjectContainer session = container.OpenSession();
                HttpContext.Current.Items[SessionKey] = session;
            }
    
            private void CloseSession(object sender, EventArgs e)
            {
                if (HttpContext.Current.Items[SessionKey] != null)
                {
                    IObjectContainer session = (IObjectContainer)HttpContext.Current.Items[SessionKey];
                    session.Dispose();
                }
            }
            // #end example
        }
    }
    

    As alternative you could use the Application_Start from the Global.apsx, which is called only once for sure.