Files
Umbraco-CMS/src/SQLCE4Umbraco/SqlCeContextGuardian.cs
Shannon Deminick b73efd16dc Fixes ServiceContext to ensure the repo's are lazy instantiated because their ctor's rely on the
RepositoryResolver.Current being initialized which normally doesn't occur until after the ServiceContext
is constructed. Adds instance level caching for the GetRecursiveValue method in case this is called more than
one time for a property in one view. Reverts PetaPocoUnitOfWork to allow more than one call to Commit().. this
isn't 'best practices' per se but it is more for performance reasons because otherwise we'd have to create a new
repo object + uow for any bulk saving operations... The end result is the same, bulk operations in the Services
cannot be processed in one transaction. Fixing up the ContentServiceTests by ensuring that the shared and always open sqlce
connection with the legacy SqlCeContextGuardian is closed on TearDown.
2012-12-15 10:33:29 +05:00

65 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlServerCe;
using System.Linq;
using System.Text;
namespace SQLCE4Umbraco
{
public static class SqlCeContextGuardian
{
private static SqlCeConnection _constantOpenConnection;
private static readonly object Lock = new object();
// Awesome SQL CE 4 speed improvement by Erik Ejskov Jensen - SQL CE 4 MVP
// It's not an issue with SQL CE 4 that we never close the connection
public static SqlCeConnection Open(string connectionString)
{
var connectionStringBuilder = new DbConnectionStringBuilder();
try
{
connectionStringBuilder.ConnectionString = connectionString;
}
catch (Exception ex)
{
throw new ArgumentException("Bad connection string.", "connectionString", ex);
}
connectionStringBuilder.Remove("datalayer");
// SQL CE 4 performs better when there's always a connection open in the background
EnsureOpenBackgroundConnection(connectionStringBuilder.ConnectionString);
SqlCeConnection conn = new SqlCeConnection(connectionStringBuilder.ConnectionString);
conn.Open();
return conn;
}
/// <summary>
/// Sometimes we need to ensure this is closed especially in unit tests
/// </summary>
internal static void CloseBackgroundConnection()
{
if (_constantOpenConnection != null)
_constantOpenConnection.Close();
}
private static void EnsureOpenBackgroundConnection(string connectionString)
{
lock (Lock)
{
if (_constantOpenConnection == null)
{
_constantOpenConnection = new SqlCeConnection(connectionString);
_constantOpenConnection.Open();
}
else if (_constantOpenConnection.State != ConnectionState.Open)
_constantOpenConnection.Open();
}
}
}
}