Skip to content

Commit 4abfc74

Browse files
committed
Broken connections do not go back to the pool (DNET-818).
1 parent 57cf98b commit 4abfc74

File tree

5 files changed

+64
-6
lines changed

5 files changed

+64
-6
lines changed

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/Version10/GdsDatabase.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ public byte[] AuthData
122122
get { return _connection.AuthData; }
123123
}
124124

125+
public bool ConnectionBroken
126+
{
127+
get { return _xdrStream.IOFailed; }
128+
}
125129

126130
#endregion
127131

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/XdrStream.cs

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ private static byte[] Pad
8585
private Ionic.Zlib.ZlibCodec _inflate;
8686
private byte[] _compressionBuffer;
8787

88+
private bool _ioFailed;
8889
private int _operation;
8990

9091
#endregion
@@ -119,6 +120,15 @@ public override long Length
119120

120121
#endregion
121122

123+
#region Properties
124+
125+
public bool IOFailed
126+
{
127+
get { return _ioFailed; }
128+
}
129+
130+
#endregion
131+
122132
#region Constructors
123133

124134
public XdrStream()
@@ -151,6 +161,7 @@ public XdrStream(Stream innerStream, Charset charset, bool compression, bool own
151161
_compressionBuffer = new byte[1024 * 1024];
152162
}
153163

164+
_ioFailed = false;
154165
ResetOperation();
155166
}
156167

@@ -194,8 +205,16 @@ public override void Flush()
194205
buffer = _compressionBuffer;
195206
count = _deflate.NextOut;
196207
}
197-
_innerStream.Write(buffer, 0, count);
198-
_innerStream.Flush();
208+
try
209+
{
210+
_innerStream.Write(buffer, 0, count);
211+
_innerStream.Flush();
212+
}
213+
catch (IOException)
214+
{
215+
_ioFailed = true;
216+
throw;
217+
}
199218
}
200219

201220
public override void SetLength(long length)
@@ -228,7 +247,16 @@ public override int Read(byte[] buffer, int offset, int count)
228247
if (_inputBuffer.Length < count)
229248
{
230249
var readBuffer = new byte[PreferredBufferSize];
231-
var read = _innerStream.Read(readBuffer, 0, readBuffer.Length);
250+
var read = default(int);
251+
try
252+
{
253+
read = _innerStream.Read(readBuffer, 0, readBuffer.Length);
254+
}
255+
catch (IOException)
256+
{
257+
_ioFailed = true;
258+
throw;
259+
}
232260
if (read != 0)
233261
{
234262
if (_compression)
@@ -262,7 +290,16 @@ public override async Task<int> ReadAsync(byte[] buffer, int offset, int count,
262290
if (_inputBuffer.Length < count)
263291
{
264292
var readBuffer = new byte[PreferredBufferSize];
265-
var read = await _innerStream.ReadAsync(readBuffer, 0, readBuffer.Length).ConfigureAwait(false);
293+
var read = default(int);
294+
try
295+
{
296+
read = await _innerStream.ReadAsync(readBuffer, 0, readBuffer.Length).ConfigureAwait(false);
297+
}
298+
catch (IOException)
299+
{
300+
_ioFailed = true;
301+
throw;
302+
}
266303
if (read != 0)
267304
{
268305
if (_compression)
@@ -309,7 +346,7 @@ public byte[] ToArray()
309346
{
310347
CheckDisposed();
311348

312-
if(!(_innerStream is MemoryStream memoryStream))
349+
if (!(_innerStream is MemoryStream memoryStream))
313350
throw new InvalidOperationException();
314351
Flush();
315352
return memoryStream.ToArray();

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Native/FesDatabase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,11 @@ public IFbClient FbClient
105105
get { return _fbClient; }
106106
}
107107

108+
public bool ConnectionBroken
109+
{
110+
get { return false; }
111+
}
112+
108113
#endregion
109114

110115
#region Constructors

Provider/src/FirebirdSql.Data.FirebirdClient/Common/IDatabase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ internal interface IDatabase : IDisposable
3232
short PacketSize { get; set; }
3333
short Dialect { get; set; }
3434
bool HasRemoteEventSupport { get; }
35+
bool ConnectionBroken { get; }
3536

3637
void Attach(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey);
3738
void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey);

Provider/src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbConnection.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,18 @@ public override void Close()
523523
_innerConnection.EnableCancel();
524524
}
525525

526-
FbConnectionPoolManager.Instance.Release(_innerConnection);
526+
if (!_innerConnection.Database.ConnectionBroken)
527+
{
528+
FbConnectionPoolManager.Instance.Release(_innerConnection);
529+
}
530+
else
531+
{
532+
if (!_innerConnection.IsEnlisted)
533+
{
534+
_innerConnection.Dispose();
535+
}
536+
_innerConnection = null;
537+
}
527538
}
528539
else
529540
{

0 commit comments

Comments
 (0)