4

I'm trying to get MVCMiniProfiler to work with PetaPoco

I'm trying to set the connection in the creation of the PetaPoco DB, but run into problems (connectionClosed)

public class DbHelper
{
    static Database _CurrentDb = null;
    public static Database CurrentDb()
    {
        if (_CurrentDb == null)
        {
            string connstr = ConfigurationManager.ConnectionStrings["MainConnectionString"].ConnectionString;
            var conn = ProfiledDbConnection.Get(new SqlConnection(connstr));
            _CurrentDb = new PetaPoco.Database(conn);
        }
        return _CurrentDb;
    }

}

I've read this item https://github.com/toptensoftware/PetaPoco/issues/44 but can get it to work

What is the right way to do it?

Edit

The solution was provided by Gareth Elms:

public class DbHelper
{
    static Database _CurrentDb = null;
    public static Database CurrentDb()
    {
        if (_CurrentDb == null)
        {
            _CurrentDb = new DatabaseWithMVCMiniProfiler("MainConnectionString");
        }
        return _CurrentDb;
    }

}
public class DatabaseWithMVCMiniProfiler : PetaPoco.Database
{
    public DatabaseWithMVCMiniProfiler(IDbConnection connection) : base(connection) { }
    public DatabaseWithMVCMiniProfiler(string connectionStringName) : base(connectionStringName) { }
    public DatabaseWithMVCMiniProfiler(string connectionString, string providerName) : base(connectionString, providerName) { }
    public DatabaseWithMVCMiniProfiler(string connectionString, DbProviderFactory dbProviderFactory) : base(connectionString, dbProviderFactory) { }

    public override IDbConnection OnConnectionOpened( IDbConnection connection)
    {
        // wrap the connection with a profiling connection that tracks timings 
        return MvcMiniProfiler.Data.ProfiledDbConnection.Get( connection as DbConnection, MiniProfiler.Current);
    }
}
smallmouse
  • 55
  • 6

2 Answers2

3

I wonder if this is because it is a static class. It might be some weirdness around the connection being automatically closed after a request, and petapoco's _sharedConnectionDepth counter not knowing about it. I reproduced this easily using your code. Have a look at my sample petapoco app at github https://github.com/GarethElms/PetaPoco----A-simple-web-app all I do is instantiate Database in the base controller

Typo Johnson
  • 5,974
  • 6
  • 29
  • 40
  • I think the same, maybe I should return a new PetaPoco database in dev and the static in production (for perf reasons) – smallmouse Aug 20 '11 at 20:51
  • 1
    I would be instantiating a PetaPoco class once per request. – Schotime Aug 24 '11 at 00:37
  • I mean...the Database class in PetaPoco. eg. Create a action filter that on action executing, it instantiates the Database class, begins a transaction, then on action result executed, you can commit the transaction or roll it back if an exception occurred. – Schotime Sep 05 '11 at 00:36
1

as Schotime mentioned, per request is the best for mvc applications. This is my impl using Ninject

private static void RegisterServices(IKernel kernel)
{


#if DEBUG
            kernel.Bind<IDatabase>().To<DebugDatabase>()
               .InRequestScope()
               .WithConstructorArgument("connectionStringName", "DebugCnnString");
#else
            kernel.Bind<IDatabase>().To<ReleaseDatabase>()
               .InRequestScope()
               .WithConstructorArgument("connectionStringName", "ReleaseCnnString");
#endif
}

public class DebugDatabase : PetaPoco.Database
{
    public DebugDatabase(string connectionStringName) : base(connectionStringName) { }

    public override IDbConnection OnConnectionOpened(IDbConnection connection)
    {
        // wrap the connection with a profiling connection
        return new ProfiledDbConnection(connection as DbConnection, MiniProfiler.Current);
    }
}

public class ReleaseDatabase : PetaPoco.Database
{
    public ReleaseDatabase(string connectionStringName) : base(connectionStringName)    {   }

    // ... some stuff
}
Eldar
  • 862
  • 9
  • 22