1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using AopAlliance.Intercept;
|
---|
6 | using System.Threading;
|
---|
7 | using HeuristicLab.Hive.Server.LINQDataAccess;
|
---|
8 | using System.Transactions;
|
---|
9 | using HeuristicLab.Hive.Contracts;
|
---|
10 | using HeuristicLab.Tracing;
|
---|
11 |
|
---|
12 | namespace 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 | |
---|