Get More Support

Here for you 24/7

learn more
VOD Free SDK

Start Building your Engine Now

Download Now
VOD Extranet

Access to patches, license management,
tech docs and more for existing VOD customers.

Learn More
What is causing this DatabaseFileLockedException when trying to open a db4o database in an ASP.NET MVC app?
Last Post 09 Aug 2010 09:51 AM by gamlerhart. 1 Replies.
AddThis - Bookmarking and Sharing Button Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages Not Resolved
markashleybell
New Member
New Member
Posts: 1


--
08 Aug 2010 09:50 PM

    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 code 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?
    gamlerhart
    Advanced Member
    Advanced Member
    Posts: 825


    --
    09 Aug 2010 09:51 AM

    Same answer as in StackOverflow:

    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 is, 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)
          {
            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
     

    You are not authorized to post a reply.


    Active Forums 4.3