Free cookie consent management tool by TermsFeed Policy Generator

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

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

improved the DAL further, changed minor details for the presentation (#830)

File size: 2.7 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;
9
10namespace HeuristicLab.Hive.Server {
11  internal class ServiceCallInterception : IMethodInterceptor {
12
13    private bool useTransactions = false;
14
15    public object Invoke(IMethodInvocation invocation) {
16      Console.WriteLine(DateTime.Now + " - " + Thread.CurrentThread.ManagedThreadId + " - Entering Method " +
17                        invocation.Method.Name);
18
19      if(ContextFactory.Context != null) {
20        Console.WriteLine("Error - Not null context found - why wasn't this disposed?");
21        ContextFactory.Context = null;
22      }
23
24      Object obj = null;
25
26      if (invocation.Method.Name.Equals("SendStreamedJob") || invocation.Method.Name.Equals("StoreFinishedJobResultStreamed")) {       
27        ContextFactory.Context.Connection.Open();
28        if(useTransactions)
29          ContextFactory.Context.Transaction = ContextFactory.Context.Connection.BeginTransaction();
30        try {
31          obj = invocation.Proceed();
32          Console.WriteLine("leaving context open for Streaming");
33        }
34        catch (Exception e) {         
35          Console.WriteLine(e);
36          ContextFactory.Context.Dispose();
37          ContextFactory.Context = null;
38        }       
39      } else {
40        if(useTransactions) {
41          using (TransactionScope scope = new TransactionScope()) {
42            try {
43              obj = invocation.Proceed();
44              scope.Complete();
45            }
46            catch (Exception e) {
47              Console.WriteLine("Exception Occured");
48              Console.WriteLine(e);
49            }
50            finally {
51              ContextFactory.Context.Dispose();
52              Console.WriteLine("setting old context null");
53              ContextFactory.Context = null;
54              Console.WriteLine("Disposing old Context");
55            }
56          }
57        } else {
58          try {
59            obj = invocation.Proceed();           
60          }
61          catch (Exception e) {
62            Console.WriteLine("Exception Occured");
63            Console.WriteLine(e);
64          }
65          finally {
66            ContextFactory.Context.Dispose();
67            Console.WriteLine("setting old context null");
68            ContextFactory.Context = null;
69            Console.WriteLine("Disposing old Context");
70          } 
71        }
72      }
73      Console.WriteLine(DateTime.Now + " - " + Thread.CurrentThread.ManagedThreadId + " - Leaving Method " +
74                          invocation.Method.Name);
75
76      return obj;
77    }
78  }
79}
80     
81 
Note: See TracBrowser for help on using the repository browser.