-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
SapPooledConnection.cs
165 lines (139 loc) · 5.78 KB
/
SapPooledConnection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Threading;
using SapNwRfc.Exceptions;
namespace SapNwRfc.Pooling
{
/// <summary>
/// Represents a pooled connection.
/// </summary>
public sealed class SapPooledConnection : ISapPooledConnection
{
private readonly List<ISapFunction> _functions = new List<ISapFunction>();
private readonly ISapConnectionPool _pool;
private ISapConnection _connection = null;
private bool _disposed = false;
/// <summary>
/// Initializes a new instance of the <see cref="SapPooledConnection"/> class.
/// </summary>
/// <param name="pool">The connection pool.</param>
public SapPooledConnection(ISapConnectionPool pool)
{
_pool = pool;
}
/// <summary>
/// Finalizes an instance of the <see cref="SapPooledConnection"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
~SapPooledConnection()
{
Dispose();
}
/// <summary>
/// Returns the underlying <see cref="SapConnection"/> to the connection pool and make it available for reuse.
/// </summary>
public void Dispose()
{
if (_disposed)
return;
// manually dispose any open function on current connection
// when it gets returned to pool
foreach (ISapFunction function in _functions)
{
function.Dispose();
}
if (_connection != null)
{
_pool.ReturnConnection(_connection);
_connection = null;
}
_disposed = true;
}
/// <inheritdoc cref="ISapPooledConnection"/>
public void InvokeFunction(string name, CancellationToken cancellationToken = default)
{
_connection = _connection ?? _pool.GetConnection(cancellationToken);
try
{
using (ISapFunction function = _connection.CreateFunction(name))
function.Invoke();
}
catch (SapCommunicationFailedException)
{
// Let the pool collect the dead connection
_pool.ForgetConnection(_connection);
// Retry invocation with new connection from the pool
_connection = _pool.GetConnection(cancellationToken);
using (ISapFunction function = _connection.CreateFunction(name))
function.Invoke();
}
}
/// <inheritdoc cref="ISapPooledConnection"/>
public void InvokeFunction(string name, object input, CancellationToken cancellationToken = default)
{
_connection = _connection ?? _pool.GetConnection(cancellationToken);
try
{
using (ISapFunction function = _connection.CreateFunction(name))
function.Invoke(input);
}
catch (SapCommunicationFailedException)
{
// Let the pool collect the dead connection
_pool.ForgetConnection(_connection);
// Retry invocation with new connection from the pool
_connection = _pool.GetConnection(cancellationToken);
using (ISapFunction function = _connection.CreateFunction(name))
function.Invoke(input);
}
}
/// <inheritdoc cref="ISapPooledConnection"/>
public TOutput InvokeFunction<TOutput>(string name, CancellationToken cancellationToken = default)
{
_connection = _connection ?? _pool.GetConnection(cancellationToken);
try
{
// no using here to prevent function from being disposed
// when possibly an IEnumerable is used in TOutput
// and rows are accessed after InvokeFunction
ISapFunction function = _connection.CreateFunction(name);
_functions.Add(function);
return function.Invoke<TOutput>();
}
catch (SapCommunicationFailedException)
{
// Let the pool collect the dead connection
_pool.ForgetConnection(_connection);
// Retry invocation with new connection from the pool
_connection = _pool.GetConnection(cancellationToken);
ISapFunction function = _connection.CreateFunction(name);
_functions.Add(function);
return function.Invoke<TOutput>();
}
}
/// <inheritdoc cref="ISapPooledConnection"/>
public TOutput InvokeFunction<TOutput>(string name, object input, CancellationToken cancellationToken = default)
{
_connection = _connection ?? _pool.GetConnection(cancellationToken);
try
{
// no using here to prevent function from being disposed
// when possibly an IEnumerable is used in TOutput
// and rows are accessed after InvokeFunction
ISapFunction function = _connection.CreateFunction(name);
_functions.Add(function);
return function.Invoke<TOutput>(input);
}
catch (SapCommunicationFailedException)
{
// Let the pool collect the dead connection
_pool.ForgetConnection(_connection);
// Retry invocation with new connection from the pool
_connection = _pool.GetConnection(cancellationToken);
ISapFunction function = _connection.CreateFunction(name);
_functions.Add(function);
return function.Invoke<TOutput>(input);
}
}
}
}