using System; using System.Collections.Generic; using System.Linq; using System.Text; using AopAlliance.Intercept; using System.Threading; using HeuristicLab.Hive.Server.LINQDataAccess; using System.Transactions; using HeuristicLab.Hive.Contracts; using HeuristicLab.Tracing; using System.Reflection; using HeuristicLab.Hive.Server.Core; namespace HeuristicLab.Hive.Server { internal class ServiceCallInterception : IMethodInterceptor { private const bool UseTransactions = true; public object Invoke(IMethodInvocation invocation) { bool userTransaction = false; DateTime start = DateTime.Now; Logger.Info("Entering Method: " + invocation.Method.Name); if (!ContextFactory.IsContextNull()) { Logger.Info("Not null context found - why wasn't this disposed?"); try { Logger.Info("Trying to dispose"); ContextFactory.Context.Dispose(); } catch (Exception e) { Logger.Error(e); } ContextFactory.Context = null; } Logger.Info("Context info: Timeout: " + ContextFactory.Context.Connection.ConnectionTimeout + " | " + ContextFactory.Context.Connection.State + " | Conn: " + ContextFactory.Context + " | HashCode is: " + ContextFactory.Context.Connection.GetHashCode()); Object obj = null; Object[] attributes = invocation.Method.GetCustomAttributes(typeof(SpringTransaction), true); foreach (Object o in attributes) { SpringTransaction st = (SpringTransaction)o; userTransaction = st.UserTransaction; } if (UseTransactions && !userTransaction) { using ( TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = ApplicationConstants.ISOLATION_LEVEL_SCOPE })) { try { Logger.Debug("Current Transaction Isolation level is: " + Transaction.Current.IsolationLevel); obj = invocation.Proceed(); scope.Complete(); } catch (Exception e) { Logger.Error("Exception occured during method invocation", e); Logger.Error("Inner Exception: ", e.InnerException); } finally { ContextFactory.Context.Dispose(); Logger.Debug("Disposed Context"); ContextFactory.Context = null; Logger.Debug("Set Context Null"); } } } else { try { obj = invocation.Proceed(); } catch (Exception e) { Logger.Error("Exception occured during method invocation", e); } finally { ContextFactory.Context.Dispose(); Logger.Debug("Disposed Context"); ContextFactory.Context = null; Logger.Debug("Set Context Null"); } } TimeSpan ts = DateTime.Now - start; if (ts.Seconds > 2) { Logger.Warn("Invocation took: " + ts); } else if (ts.Seconds > 10) { Logger.Error("Invocation took: " + ts); } else { Logger.Info("Invocation took: " + ts); ; } Logger.Info("Leaving Method: " + invocation.Method.Name); return obj; } } }