Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server/3.2/ServiceCallInterception.cs @ 3578

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

Removed References to HiveLogging and updated the default logging mechanism (#991)

File size: 3.2 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;
11
12namespace HeuristicLab.Hive.Server {
13  internal class ServiceCallInterception : IMethodInterceptor {
14
15    private const bool UseTransactions = true;
16
17    public object Invoke(IMethodInvocation invocation) {
18      Logger.Info("Entering Method: " + invocation.Method.Name);
19
20      if(ContextFactory.Context != null) {
21        Logger.Info("Not null context found - why wasn't this disposed?");
22        try {
23          Logger.Info("Trying to dispose");
24          ContextFactory.Context.Dispose();
25        } catch (Exception e) {
26          Logger.Error(e);
27        }
28        ContextFactory.Context = null;
29      }
30
31      Object obj = null;
32
33      if (invocation.Method.Name.Equals("SendStreamedJob") || invocation.Method.Name.Equals("StoreFinishedJobResultStreamed")) {       
34        ContextFactory.Context.Connection.Open();
35        if(UseTransactions) {
36          Logger.Debug("Opening Transaction");
37          ContextFactory.Context.Transaction = ContextFactory.Context.Connection.BeginTransaction(ApplicationConstants.ISOLATION_LEVEL);
38        } else {
39          Logger.Debug("Not using a Transaction");
40        }
41        try {
42          obj = invocation.Proceed();
43          Logger.Info("leaving context open for streaming");
44        }
45        catch (Exception e) {
46          Logger.Error("Exception occured during method invocation", e);             
47          ContextFactory.Context.Dispose();
48          ContextFactory.Context = null;
49        }       
50      } else {
51        if(UseTransactions) {
52          using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = ApplicationConstants.ISOLATION_LEVEL_SCOPE })) {
53            try {
54              Logger.Debug("Current Transaction Isolation level is: " + Transaction.Current.IsolationLevel);
55              obj = invocation.Proceed();
56              scope.Complete();
57            }
58            catch (Exception e) {
59              Logger.Error("Exception occured during method invocation", e);             
60            }
61            finally {
62              ContextFactory.Context.Dispose();
63              Logger.Debug("Disposed Context");
64              ContextFactory.Context = null;
65              Logger.Debug("Set Context Null");
66            }
67          }
68        } else {
69          try {
70            obj = invocation.Proceed();           
71          }
72          catch (Exception e) {
73            Logger.Error("Exception occured during method invocation", e);                         
74          }
75          finally {
76            ContextFactory.Context.Dispose();
77            Logger.Debug("Disposed Context");
78            ContextFactory.Context = null;
79            Logger.Debug("Set Context Null");
80          } 
81        }
82      }
83      Logger.Info("Leaving Method: " + invocation.Method.Name);
84
85      return obj;
86    }
87  }
88}
89     
90 
Note: See TracBrowser for help on using the repository browser.