Skip to content

Commit 6a61efd

Browse files
committed
Handle properly create/drop in regards to IDisposable.
1 parent 0db4528 commit 6a61efd

File tree

4 files changed

+33
-70
lines changed

4 files changed

+33
-70
lines changed

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

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public void Dispose()
154154
_eventManager = null;
155155
_serverVersion = null;
156156
_dialect = 0;
157-
_handle = 0;
157+
_handle = -1;
158158
_packetSize = 0;
159159
_warningMessage = null;
160160
_transactionCount = 0;
@@ -225,21 +225,26 @@ public virtual void Detach()
225225
{
226226
CloseEventManager();
227227

228-
if (_handle != 0)
228+
var detach = _handle != -1;
229+
if (detach)
229230
{
230231
XdrStream.Write(IscCodes.op_detach);
231232
XdrStream.Write(_handle);
232233
}
233234
XdrStream.Write(IscCodes.op_disconnect);
234235
XdrStream.Flush();
236+
if (detach)
237+
{
238+
ReadResponse();
239+
}
235240

236241
CloseConnection();
237242

238243
#warning Here
239244
_xdrStream?.Dispose();
240245

241246
_transactionCount = 0;
242-
_handle = 0;
247+
_handle = -1;
243248
_dialect = 0;
244249
_packetSize = 0;
245250
_xdrStream = null;
@@ -281,23 +286,7 @@ public virtual void CreateDatabase(DatabaseParameterBuffer dpb, string dataSourc
281286
{
282287
SendCreateToBuffer(dpb, database);
283288
XdrStream.Flush();
284-
285-
try
286-
{
287-
ProcessCreateResponse(ReadGenericResponse());
288-
289-
Detach();
290-
}
291-
catch (IscException)
292-
{
293-
try
294-
{
295-
CloseConnection();
296-
}
297-
catch
298-
{ }
299-
throw;
300-
}
289+
ProcessCreateResponse(ReadGenericResponse());
301290
}
302291
catch (IOException ex)
303292
{
@@ -332,21 +321,12 @@ public virtual void DropDatabase()
332321

333322
ReadResponse();
334323

335-
_handle = 0;
324+
_handle = -1;
336325
}
337326
catch (IOException ex)
338327
{
339328
throw IscException.ForErrorCode(IscCodes.isc_network_error, ex);
340329
}
341-
finally
342-
{
343-
try
344-
{
345-
CloseConnection();
346-
}
347-
catch
348-
{ }
349-
}
350330
}
351331

352332
#endregion
@@ -587,12 +567,7 @@ public virtual Task<int> NextOperationAsync()
587567
public virtual IResponse ReadResponse()
588568
{
589569
var response = ReadSingleResponse();
590-
591-
if (response is GenericResponse)
592-
{
593-
ProcessResponse(response);
594-
}
595-
570+
ProcessResponse(response);
596571
return response;
597572
}
598573

@@ -632,11 +607,8 @@ public virtual void ReleaseObject(int op, int id)
632607
protected virtual IResponse ReadSingleResponse()
633608
{
634609
var operation = ReadOperation();
635-
636610
var response = GdsConnection.ProcessOperation(operation, XdrStream);
637-
638611
ProcessResponseWarnings(response);
639-
640612
return response;
641613
}
642614

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -76,25 +76,9 @@ public override void CreateDatabase(DatabaseParameterBuffer dpb, string dataSour
7676
{
7777
SendCreateToBuffer(dpb, database);
7878
XdrStream.Flush();
79-
80-
try
81-
{
82-
var response = ReadResponse();
83-
response = ProcessCryptCallbackResponseIfNeeded(response, cryptKey);
84-
ProcessCreateResponse(response as GenericResponse);
85-
86-
Detach();
87-
}
88-
catch (IscException)
89-
{
90-
try
91-
{
92-
CloseConnection();
93-
}
94-
catch
95-
{ }
96-
throw;
97-
}
79+
var response = ReadResponse();
80+
response = ProcessCryptCallbackResponseIfNeeded(response, cryptKey);
81+
ProcessCreateResponse(response as GenericResponse);
9882
}
9983
catch (IOException ex)
10084
{

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ public void CreateDatabase(DatabaseParameterBuffer dpb, string dataSource, int p
167167
0);
168168

169169
ProcessStatusVector(_statusVector);
170-
171-
Detach();
172170
}
173171

174172
public void DropDatabase()
@@ -177,9 +175,9 @@ public void DropDatabase()
177175

178176
_fbClient.isc_drop_database(_statusVector, ref _handle);
179177

180-
_handle.Dispose();
181-
182178
ProcessStatusVector(_statusVector);
179+
180+
_handle.Dispose();
183181
}
184182

185183
#endregion
@@ -238,11 +236,16 @@ public void Detach()
238236
throw IscException.ForErrorCodeIntParam(IscCodes.isc_open_trans, TransactionCount);
239237
}
240238

241-
ClearStatusVector();
239+
if (!_handle.IsClosed)
240+
{
241+
ClearStatusVector();
242242

243-
_fbClient.isc_detach_database(_statusVector, ref _handle);
243+
_fbClient.isc_detach_database(_statusVector, ref _handle);
244244

245-
ProcessStatusVector(_statusVector);
245+
ProcessStatusVector(_statusVector);
246+
247+
_handle.Dispose();
248+
}
246249
}
247250

248251
#endregion

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,15 +120,19 @@ public void Dispose()
120120

121121
public void CreateDatabase(DatabaseParameterBuffer dpb)
122122
{
123-
var db = ClientFactory.CreateDatabase(_options);
124-
db.CreateDatabase(dpb, _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
123+
using (var db = ClientFactory.CreateDatabase(_options))
124+
{
125+
db.CreateDatabase(dpb, _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
126+
}
125127
}
126128

127129
public void DropDatabase()
128130
{
129-
var db = ClientFactory.CreateDatabase(_options);
130-
db.Attach(BuildDpb(db, _options), _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
131-
db.DropDatabase();
131+
using (var db = ClientFactory.CreateDatabase(_options))
132+
{
133+
db.Attach(BuildDpb(db, _options), _options.DataSource, _options.Port, _options.Database, _options.CryptKey);
134+
db.DropDatabase();
135+
}
132136
}
133137

134138
#endregion

0 commit comments

Comments
 (0)