mirror of
https://github.com/lidarr/Lidarr
synced 2024-12-26 01:27:00 +00:00
Fixed petapoco's craptastic connection management.
This commit is contained in:
parent
52e6d09325
commit
a5527df584
4 changed files with 47 additions and 27 deletions
|
@ -45,19 +45,17 @@ public static String LogConnectionString
|
|||
public static IDatabase GetPetaPocoDb(string connectionString, Boolean profiled = true)
|
||||
{
|
||||
MigrationsHelper.Run(connectionString, true);
|
||||
var sqliteConnection = new SqlCeConnection(connectionString);
|
||||
DbConnection connection = sqliteConnection;
|
||||
|
||||
if (profiled)
|
||||
var factory = new PetaDbProviderFactory
|
||||
{
|
||||
connection = ProfiledDbConnection.Get(sqliteConnection);
|
||||
}
|
||||
IsProfiled = profiled
|
||||
};
|
||||
|
||||
var db = new Database(connection, Database.DBType.SqlServerCE);
|
||||
db.ForceDateTimesToUtc = false;
|
||||
|
||||
if (connection.State != ConnectionState.Open)
|
||||
connection.Open();
|
||||
var db = new Database(connectionString, factory, Database.DBType.SqlServerCE)
|
||||
{
|
||||
KeepConnectionAlive = true,
|
||||
ForceDateTimesToUtc = false,
|
||||
};
|
||||
|
||||
return db;
|
||||
}
|
||||
|
|
28
NzbDrone.Core/Datastore/PetaDbProviderFactory.cs
Normal file
28
NzbDrone.Core/Datastore/PetaDbProviderFactory.cs
Normal file
|
@ -0,0 +1,28 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.Common;
|
||||
using System.Data.SqlServerCe;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using MvcMiniProfiler.Data;
|
||||
|
||||
namespace NzbDrone.Core.Datastore
|
||||
{
|
||||
class PetaDbProviderFactory : DbProviderFactory
|
||||
{
|
||||
public Boolean IsProfiled { get; set; }
|
||||
|
||||
public override DbConnection CreateConnection()
|
||||
{
|
||||
var sqliteConnection = new SqlCeConnection();
|
||||
DbConnection connection = sqliteConnection;
|
||||
|
||||
if (IsProfiled)
|
||||
{
|
||||
connection = ProfiledDbConnection.Get(sqliteConnection);
|
||||
}
|
||||
|
||||
return connection;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -282,7 +282,7 @@ public Database(IDbConnection connection, DBType dbType)
|
|||
{
|
||||
_sharedConnection = connection;
|
||||
_connectionString = connection.ConnectionString;
|
||||
_sharedConnectionDepth = 2; // Prevent closing external connection
|
||||
|
||||
_dbType = dbType;
|
||||
CommonConstruct();
|
||||
}
|
||||
|
@ -294,10 +294,11 @@ public Database(string connectionString, string providerName)
|
|||
CommonConstruct();
|
||||
}
|
||||
|
||||
public Database(string connectionString, DbProviderFactory provider)
|
||||
public Database(string connectionString, DbProviderFactory provider, DBType dbType)
|
||||
{
|
||||
_connectionString = connectionString;
|
||||
_factory = provider;
|
||||
_dbType = dbType;
|
||||
CommonConstruct();
|
||||
}
|
||||
|
||||
|
@ -375,6 +376,7 @@ public void Dispose()
|
|||
{
|
||||
// Automatically close one open connection reference
|
||||
// (Works with KeepConnectionAlive and manually opening a shared connection)
|
||||
KeepConnectionAlive = false;
|
||||
CloseSharedConnection();
|
||||
}
|
||||
|
||||
|
@ -384,18 +386,14 @@ public void Dispose()
|
|||
// Open a connection (can be nested)
|
||||
public void OpenSharedConnection()
|
||||
{
|
||||
if (_sharedConnectionDepth == 0)
|
||||
if (_sharedConnection == null || _sharedConnection.State == ConnectionState.Closed || _sharedConnection.State == ConnectionState.Broken)
|
||||
{
|
||||
_sharedConnection = _factory.CreateConnection();
|
||||
_sharedConnection.ConnectionString = _connectionString;
|
||||
_sharedConnection.Open();
|
||||
|
||||
_sharedConnection = OnConnectionOpened(_sharedConnection);
|
||||
|
||||
if (KeepConnectionAlive)
|
||||
_sharedConnectionDepth++; // Make sure you call Dispose
|
||||
}
|
||||
_sharedConnectionDepth++;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -404,17 +402,13 @@ public void OpenSharedConnection()
|
|||
// Close a previously opened connection
|
||||
public void CloseSharedConnection()
|
||||
{
|
||||
if (_sharedConnectionDepth > 0)
|
||||
{
|
||||
_sharedConnectionDepth--;
|
||||
if (_sharedConnectionDepth == 0)
|
||||
if (!KeepConnectionAlive && _sharedConnection != null)
|
||||
{
|
||||
OnConnectionClosing(_sharedConnection);
|
||||
_sharedConnection.Dispose();
|
||||
_sharedConnection = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Access to our shared connection
|
||||
public IDbConnection Connection
|
||||
|
@ -2530,7 +2524,6 @@ static T RecurseInheritedTypes<T>(Type t, Func<Type, T> cb)
|
|||
DbProviderFactory _factory;
|
||||
IDbConnection _sharedConnection;
|
||||
IDbTransaction _transaction;
|
||||
int _sharedConnectionDepth;
|
||||
int _transactionDepth;
|
||||
bool _transactionCancelled;
|
||||
string _lastSql;
|
||||
|
|
|
@ -178,6 +178,7 @@
|
|||
<Compile Include="Datastore\CustomeMapper.cs" />
|
||||
<Compile Include="Datastore\Migrations\Migration20110726.cs" />
|
||||
<Compile Include="Datastore\Migrations\Migration20110707.cs" />
|
||||
<Compile Include="Datastore\PetaDbProviderFactory.cs" />
|
||||
<Compile Include="Fluent.cs" />
|
||||
<Compile Include="Helpers\EpisodeSortingHelper.cs" />
|
||||
<Compile Include="Helpers\FileSizeFormatHelpercs.cs" />
|
||||
|
|
Loading…
Reference in a new issue