mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Improving sessions management
This commit is contained in:
@@ -31,7 +31,7 @@ namespace Orchard.ContentManagement {
|
|||||||
private readonly IRepository<ContentItemVersionRecord> _contentItemVersionRepository;
|
private readonly IRepository<ContentItemVersionRecord> _contentItemVersionRepository;
|
||||||
private readonly IContentDefinitionManager _contentDefinitionManager;
|
private readonly IContentDefinitionManager _contentDefinitionManager;
|
||||||
private readonly ICacheManager _cacheManager;
|
private readonly ICacheManager _cacheManager;
|
||||||
private readonly Func<IContentManagerSession> _contentManagerSession;
|
private readonly Lazy<IContentManagerSession> _contentManagerSession;
|
||||||
private readonly Lazy<IContentDisplay> _contentDisplay;
|
private readonly Lazy<IContentDisplay> _contentDisplay;
|
||||||
private readonly Lazy<ISessionLocator> _sessionLocator;
|
private readonly Lazy<ISessionLocator> _sessionLocator;
|
||||||
private readonly Lazy<IEnumerable<IContentHandler>> _handlers;
|
private readonly Lazy<IEnumerable<IContentHandler>> _handlers;
|
||||||
@@ -50,7 +50,7 @@ namespace Orchard.ContentManagement {
|
|||||||
IRepository<ContentItemVersionRecord> contentItemVersionRepository,
|
IRepository<ContentItemVersionRecord> contentItemVersionRepository,
|
||||||
IContentDefinitionManager contentDefinitionManager,
|
IContentDefinitionManager contentDefinitionManager,
|
||||||
ICacheManager cacheManager,
|
ICacheManager cacheManager,
|
||||||
Func<IContentManagerSession> contentManagerSession,
|
Lazy<IContentManagerSession> contentManagerSession,
|
||||||
Lazy<IContentDisplay> contentDisplay,
|
Lazy<IContentDisplay> contentDisplay,
|
||||||
Lazy<ISessionLocator> sessionLocator,
|
Lazy<ISessionLocator> sessionLocator,
|
||||||
Lazy<IEnumerable<IContentHandler>> handlers,
|
Lazy<IEnumerable<IContentHandler>> handlers,
|
||||||
@@ -132,7 +132,7 @@ namespace Orchard.ContentManagement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public virtual ContentItem Get(int id, VersionOptions options, QueryHints hints) {
|
public virtual ContentItem Get(int id, VersionOptions options, QueryHints hints) {
|
||||||
var session = _contentManagerSession();
|
var session = _contentManagerSession.Value;
|
||||||
ContentItem contentItem;
|
ContentItem contentItem;
|
||||||
|
|
||||||
ContentItemVersionRecord versionRecord = null;
|
ContentItemVersionRecord versionRecord = null;
|
||||||
@@ -651,7 +651,7 @@ namespace Orchard.ContentManagement {
|
|||||||
public void Clear() {
|
public void Clear() {
|
||||||
var session = _sessionLocator.Value.For(typeof(ContentItemRecord));
|
var session = _sessionLocator.Value.For(typeof(ContentItemRecord));
|
||||||
session.Clear();
|
session.Clear();
|
||||||
_contentManagerSession().Clear();
|
_contentManagerSession.Value.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IContentQuery<ContentItem> Query() {
|
public IContentQuery<ContentItem> Query() {
|
||||||
|
@@ -52,23 +52,33 @@ namespace Orchard.Data {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void RequireNew(IsolationLevel level) {
|
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();
|
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);
|
Logger.Debug("Creating new transaction with isolation level {0}", level);
|
||||||
_transaction = _session.BeginTransaction(level);
|
_transaction = _session.BeginTransaction(level);
|
||||||
}
|
}
|
||||||
@@ -102,11 +112,15 @@ namespace Orchard.Data {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DisposeSession();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DisposeSession() {
|
||||||
if (_session != null) {
|
if (_session != null) {
|
||||||
|
Logger.Debug("Disposing NHibernate session");
|
||||||
_session.Dispose();
|
_session.Dispose();
|
||||||
_session = null;
|
_session = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void EnsureSession() {
|
private void EnsureSession() {
|
||||||
@@ -115,7 +129,7 @@ namespace Orchard.Data {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var sessionFactory = _sessionFactoryHolder.GetSessionFactory();
|
var sessionFactory = _sessionFactoryHolder.GetSessionFactory();
|
||||||
Logger.Information("Opening database session");
|
Logger.Debug("Opening NHibernate session");
|
||||||
_session = sessionFactory.OpenSession(new OrchardSessionInterceptor(_interceptors.ToArray(), Logger));
|
_session = sessionFactory.OpenSession(new OrchardSessionInterceptor(_interceptors.ToArray(), Logger));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -4,6 +4,7 @@ using JetBrains.Annotations;
|
|||||||
using Orchard.Data;
|
using Orchard.Data;
|
||||||
using Orchard.Environment.Configuration;
|
using Orchard.Environment.Configuration;
|
||||||
using Orchard.Logging;
|
using Orchard.Logging;
|
||||||
|
using Orchard.ContentManagement;
|
||||||
|
|
||||||
namespace Orchard.Tasks {
|
namespace Orchard.Tasks {
|
||||||
|
|
||||||
@@ -16,11 +17,17 @@ namespace Orchard.Tasks {
|
|||||||
private readonly IEnumerable<IBackgroundTask> _tasks;
|
private readonly IEnumerable<IBackgroundTask> _tasks;
|
||||||
private readonly ITransactionManager _transactionManager;
|
private readonly ITransactionManager _transactionManager;
|
||||||
private readonly string _shellName;
|
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;
|
_tasks = tasks;
|
||||||
_transactionManager = transactionManager;
|
_transactionManager = transactionManager;
|
||||||
_shellName = shellSettings.Name;
|
_shellName = shellSettings.Name;
|
||||||
|
_contentManager = contentManager;
|
||||||
Logger = NullLogger.Instance;
|
Logger = NullLogger.Instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,6 +36,7 @@ namespace Orchard.Tasks {
|
|||||||
public void Sweep() {
|
public void Sweep() {
|
||||||
foreach(var task in _tasks) {
|
foreach(var task in _tasks) {
|
||||||
try {
|
try {
|
||||||
|
_contentManager.Clear();
|
||||||
_transactionManager.RequireNew();
|
_transactionManager.RequireNew();
|
||||||
task.Sweep();
|
task.Sweep();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user