Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Server/3.3/ServiceCallInterception.cs @ 4091

Last change on this file since 4091 was 4091, checked in by cneumuel, 14 years ago

resolved issues with 3.3, hive-server now executable (1096)

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