Improving sessions management

This commit is contained in:
Sebastien Ros
2014-08-26 18:08:23 -07:00
parent ad9a10d9be
commit 3b5d4f4e85
3 changed files with 44 additions and 22 deletions

View File

@@ -31,7 +31,7 @@ namespace Orchard.ContentManagement {
private readonly IRepository<ContentItemVersionRecord> _contentItemVersionRepository;
private readonly IContentDefinitionManager _contentDefinitionManager;
private readonly ICacheManager _cacheManager;
private readonly Func<IContentManagerSession> _contentManagerSession;
private readonly Lazy<IContentManagerSession> _contentManagerSession;
private readonly Lazy<IContentDisplay> _contentDisplay;
private readonly Lazy<ISessionLocator> _sessionLocator;
private readonly Lazy<IEnumerable<IContentHandler>> _handlers;
@@ -50,7 +50,7 @@ namespace Orchard.ContentManagement {
IRepository<ContentItemVersionRecord> contentItemVersionRepository,
IContentDefinitionManager contentDefinitionManager,
ICacheManager cacheManager,
Func<IContentManagerSession> contentManagerSession,
Lazy<IContentManagerSession> contentManagerSession,
Lazy<IContentDisplay> contentDisplay,
Lazy<ISessionLocator> sessionLocator,
Lazy<IEnumerable<IContentHandler>> handlers,
@@ -132,7 +132,7 @@ namespace Orchard.ContentManagement {
}
public virtual ContentItem Get(int id, VersionOptions options, QueryHints hints) {
var session = _contentManagerSession();
var session = _contentManagerSession.Value;
ContentItem contentItem;
ContentItemVersionRecord versionRecord = null;
@@ -651,7 +651,7 @@ namespace Orchard.ContentManagement {
public void Clear() {
var session = _sessionLocator.Value.For(typeof(ContentItemRecord));
session.Clear();
_contentManagerSession().Clear();
_contentManagerSession.Value.Clear();
}
public IContentQuery<ContentItem> Query() {

View File

@@ -52,23 +52,33 @@ namespace Orchard.Data {
}
public void RequireNew(IsolationLevel level) {
if (_transaction != null) {
Logger.Debug("New transaction required");
if (_cancelled) {
if (_transaction != null) {
Logger.Debug("Reverting operations from transaction");
_transaction.Rollback();
_transaction.Dispose();
Logger.Debug("Transaction disposed");
_transaction = null;
}
_cancelled = false;
}
else {
if (_transaction != null) {
Logger.Debug("Marking transaction as complete");
_transaction.Commit();
}
}
DisposeSession();
}
EnsureSession();
if (_cancelled) {
if (_transaction != null) {
_transaction.Rollback();
_transaction.Dispose();
_transaction = null;
}
_cancelled = false;
}
else {
if (_transaction != null) {
_transaction.Commit();
}
}
Logger.Debug("Creating new transaction with isolation level {0}", level);
_transaction = _session.BeginTransaction(level);
}
@@ -102,11 +112,15 @@ namespace Orchard.Data {
}
}
DisposeSession();
}
private void DisposeSession() {
if (_session != null) {
Logger.Debug("Disposing NHibernate session");
_session.Dispose();
_session = null;
}
}
private void EnsureSession() {
@@ -115,7 +129,7 @@ namespace Orchard.Data {
}
var sessionFactory = _sessionFactoryHolder.GetSessionFactory();
Logger.Information("Opening database session");
Logger.Debug("Opening NHibernate session");
_session = sessionFactory.OpenSession(new OrchardSessionInterceptor(_interceptors.ToArray(), Logger));
}

View File

@@ -4,6 +4,7 @@ using JetBrains.Annotations;
using Orchard.Data;
using Orchard.Environment.Configuration;
using Orchard.Logging;
using Orchard.ContentManagement;
namespace Orchard.Tasks {
@@ -16,11 +17,17 @@ namespace Orchard.Tasks {
private readonly IEnumerable<IBackgroundTask> _tasks;
private readonly ITransactionManager _transactionManager;
private readonly string _shellName;
private readonly IContentManager _contentManager;
public BackgroundService(IEnumerable<IBackgroundTask> tasks, ITransactionManager transactionManager, ShellSettings shellSettings) {
public BackgroundService(
IEnumerable<IBackgroundTask> tasks,
ITransactionManager transactionManager,
ShellSettings shellSettings,
IContentManager contentManager) {
_tasks = tasks;
_transactionManager = transactionManager;
_shellName = shellSettings.Name;
_contentManager = contentManager;
Logger = NullLogger.Instance;
}
@@ -29,6 +36,7 @@ namespace Orchard.Tasks {
public void Sweep() {
foreach(var task in _tasks) {
try {
_contentManager.Clear();
_transactionManager.RequireNew();
task.Sweep();
}