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 | using System.Reflection;
|
---|
12 | using HeuristicLab.Hive.Server.Core;
|
---|
13 |
|
---|
14 | namespace 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 | } |
---|