[2904] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using AopAlliance.Intercept;
|
---|
| 6 | using System.Threading;
|
---|
[3011] | 7 | using HeuristicLab.Hive.Server.LINQDataAccess;
|
---|
| 8 | using System.Transactions;
|
---|
[3578] | 9 | using HeuristicLab.Hive.Contracts;
|
---|
| 10 | using HeuristicLab.Tracing;
|
---|
[4042] | 11 | using System.Reflection;
|
---|
| 12 | using HeuristicLab.Hive.Server.Core;
|
---|
[2904] | 13 |
|
---|
| 14 | namespace HeuristicLab.Hive.Server {
|
---|
[3220] | 15 | internal class ServiceCallInterception : IMethodInterceptor {
|
---|
[3578] | 16 | private const bool UseTransactions = true;
|
---|
[3220] | 17 |
|
---|
[2904] | 18 | public object Invoke(IMethodInvocation invocation) {
|
---|
[4042] | 19 | bool userTransaction = false;
|
---|
| 20 |
|
---|
[3931] | 21 | DateTime start = DateTime.Now;
|
---|
[3578] | 22 | Logger.Info("Entering Method: " + invocation.Method.Name);
|
---|
[3011] | 23 |
|
---|
[4042] | 24 | if (!ContextFactory.IsContextNull()) {
|
---|
[3578] | 25 | Logger.Info("Not null context found - why wasn't this disposed?");
|
---|
| 26 | try {
|
---|
| 27 | Logger.Info("Trying to dispose");
|
---|
| 28 | ContextFactory.Context.Dispose();
|
---|
[4042] | 29 | }
|
---|
| 30 | catch (Exception e) {
|
---|
[3578] | 31 | Logger.Error(e);
|
---|
| 32 | }
|
---|
[3220] | 33 | ContextFactory.Context = null;
|
---|
| 34 | }
|
---|
[3931] | 35 | Logger.Info("Context info: Timeout: " + ContextFactory.Context.Connection.ConnectionTimeout + " | " +
|
---|
[4042] | 36 | ContextFactory.Context.Connection.State + " | Conn: " + ContextFactory.Context + " | HashCode is: " +
|
---|
| 37 | ContextFactory.Context.Connection.GetHashCode());
|
---|
[3220] | 38 | Object obj = null;
|
---|
| 39 |
|
---|
[4091] | 40 | Object[] attributes = invocation.Method.GetCustomAttributes(typeof(SpringTransaction), true);
|
---|
[4042] | 41 | foreach (Object o in attributes) {
|
---|
[4091] | 42 | SpringTransaction st = (SpringTransaction)o;
|
---|
[4042] | 43 | userTransaction = st.UserTransaction;
|
---|
| 44 | }
|
---|
| 45 | if (UseTransactions && !userTransaction) {
|
---|
| 46 | using (
|
---|
[4091] | 47 | TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = ApplicationConstants.ISOLATION_LEVEL_SCOPE })) {
|
---|
[3220] | 48 | try {
|
---|
[4042] | 49 | Logger.Debug("Current Transaction Isolation level is: " + Transaction.Current.IsolationLevel);
|
---|
| 50 | obj = invocation.Proceed();
|
---|
| 51 | scope.Complete();
|
---|
[3220] | 52 | }
|
---|
| 53 | catch (Exception e) {
|
---|
[4042] | 54 | Logger.Error("Exception occured during method invocation", e);
|
---|
| 55 | Logger.Error("Inner Exception: ", e.InnerException);
|
---|
[3220] | 56 | }
|
---|
| 57 | finally {
|
---|
| 58 | ContextFactory.Context.Dispose();
|
---|
[3578] | 59 | Logger.Debug("Disposed Context");
|
---|
[3220] | 60 | ContextFactory.Context = null;
|
---|
[3578] | 61 | Logger.Debug("Set Context Null");
|
---|
[4042] | 62 | }
|
---|
[3011] | 63 | }
|
---|
[4091] | 64 | } else {
|
---|
[4042] | 65 | try {
|
---|
| 66 | obj = invocation.Proceed();
|
---|
| 67 | }
|
---|
| 68 | catch (Exception e) {
|
---|
| 69 | Logger.Error("Exception occured during method invocation", e);
|
---|
| 70 | }
|
---|
| 71 | finally {
|
---|
| 72 | ContextFactory.Context.Dispose();
|
---|
| 73 | Logger.Debug("Disposed Context");
|
---|
| 74 | ContextFactory.Context = null;
|
---|
| 75 | Logger.Debug("Set Context Null");
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
[4091] | 78 |
|
---|
[3931] | 79 | TimeSpan ts = DateTime.Now - start;
|
---|
| 80 | if (ts.Seconds > 2) {
|
---|
| 81 | Logger.Warn("Invocation took: " + ts);
|
---|
[4091] | 82 | } else if (ts.Seconds > 10) {
|
---|
[3931] | 83 | Logger.Error("Invocation took: " + ts);
|
---|
[4091] | 84 | } else {
|
---|
[4042] | 85 | Logger.Info("Invocation took: " + ts);
|
---|
| 86 | ;
|
---|
| 87 | }
|
---|
[3578] | 88 | Logger.Info("Leaving Method: " + invocation.Method.Name);
|
---|
[4042] | 89 |
|
---|
[2904] | 90 | return obj;
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
[4042] | 93 | } |
---|