Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.Hive.Server/3.2/ServiceCallInterception.cs @ 4042

Last change on this file since 4042 was 4042, checked in by kgrading, 14 years ago

#828 added various improvements to the plugin cache manager, the execution engine, the transaction handling on the serverside and the server console

File size: 3.4 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,
48                                                        new TransactionOptions
49                                                        {IsolationLevel = ApplicationConstants.ISOLATION_LEVEL_SCOPE})) {
50          try {
51            Logger.Debug("Current Transaction Isolation level is: " + Transaction.Current.IsolationLevel);
52            obj = invocation.Proceed();
53            scope.Complete();
54          }
55          catch (Exception e) {
56            Logger.Error("Exception occured during method invocation", e);
57            Logger.Error("Inner Exception: ", e.InnerException);
58          }
59          finally {
60            ContextFactory.Context.Dispose();
61            Logger.Debug("Disposed Context");
62            ContextFactory.Context = null;
63            Logger.Debug("Set Context Null");
64          }
65        }
66      }
67      else {
68        try {
69          obj = invocation.Proceed();
70        }
71        catch (Exception e) {
72          Logger.Error("Exception occured during method invocation", e);
73        }
74        finally {
75          ContextFactory.Context.Dispose();
76          Logger.Debug("Disposed Context");
77          ContextFactory.Context = null;
78          Logger.Debug("Set Context Null");
79        }
80      }
81      //}       
82      TimeSpan ts = DateTime.Now - start;
83      if (ts.Seconds > 2) {
84        Logger.Warn("Invocation took: " + ts);
85      }
86      else if (ts.Seconds > 10) {
87        Logger.Error("Invocation took: " + ts);
88      }
89      else {
90        Logger.Info("Invocation took: " + ts);
91        ;
92      }
93      Logger.Info("Leaving Method: " + invocation.Method.Name);
94
95      return obj;
96    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.