diff --git a/Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/Version10/GdsDatabase.cs b/Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/Version10/GdsDatabase.cs index b7033ef6..9e646860 100644 --- a/Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/Version10/GdsDatabase.cs +++ b/Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/Version10/GdsDatabase.cs @@ -58,7 +58,6 @@ public Action WarningMessage protected string _serverVersion; private short _packetSize; private short _dialect; - private bool _disposed; private XdrStream _xdrStream; #endregion @@ -142,69 +141,6 @@ public GdsDatabase(GdsConnection connection) #endregion - #region IDisposable methods - - public void Dispose() - { - if (!_disposed) - { - _disposed = true; - - if (TransactionCount > 0) - { - throw IscException.ForErrorCodeIntParam(IscCodes.isc_open_trans, TransactionCount); - } - - try - { - CloseEventManager(); - - var detach = _handle != -1; - if (detach) - { - XdrStream.Write(IscCodes.op_detach); - XdrStream.Write(_handle); - } - XdrStream.Write(IscCodes.op_disconnect); - XdrStream.Flush(); - if (detach) - { - ReadResponse(); - } - - CloseConnection(); - _xdrStream?.Dispose(); - } - catch (IOException ex) - { - try - { - CloseConnection(); - } - catch (IOException ex2) - { - throw IscException.ForErrorCode(IscCodes.isc_network_error, ex2); - } - throw IscException.ForErrorCode(IscCodes.isc_network_error, ex); - } - finally - { - _xdrStream = null; - _connection = null; - _charset = null; - _eventManager = null; - _serverVersion = null; - _dialect = 0; - _handle = -1; - _packetSize = 0; - _warningMessage = null; - _transactionCount = 0; - } - } - } - - #endregion - #region Attach/Detach Methods public virtual void Attach(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey) @@ -258,7 +194,56 @@ public virtual void AttachWithTrustedAuth(DatabaseParameterBuffer dpb, string da public virtual void Detach() { - Dispose(); + if (TransactionCount > 0) + { + throw IscException.ForErrorCodeIntParam(IscCodes.isc_open_trans, TransactionCount); + } + + try + { + CloseEventManager(); + + var detach = _handle != -1; + if (detach) + { + XdrStream.Write(IscCodes.op_detach); + XdrStream.Write(_handle); + } + XdrStream.Write(IscCodes.op_disconnect); + XdrStream.Flush(); + if (detach) + { + ReadResponse(); + } + + CloseConnection(); + _xdrStream?.Dispose(); + } + catch (IOException ex) + { + try + { + CloseConnection(); + } + catch (IOException ex2) + { + throw IscException.ForErrorCode(IscCodes.isc_network_error, ex2); + } + throw IscException.ForErrorCode(IscCodes.isc_network_error, ex); + } + finally + { + _xdrStream = null; + _connection = null; + _charset = null; + _eventManager = null; + _serverVersion = null; + _dialect = 0; + _handle = -1; + _packetSize = 0; + _warningMessage = null; + _transactionCount = 0; + } } protected void SafelyDetach() diff --git a/Provider/src/FirebirdSql.Data.FirebirdClient/Client/Native/FesDatabase.cs b/Provider/src/FirebirdSql.Data.FirebirdClient/Client/Native/FesDatabase.cs index 7e1b9141..f1e3215c 100644 --- a/Provider/src/FirebirdSql.Data.FirebirdClient/Client/Native/FesDatabase.cs +++ b/Provider/src/FirebirdSql.Data.FirebirdClient/Client/Native/FesDatabase.cs @@ -47,7 +47,6 @@ public Action WarningMessage private Charset _charset; private short _packetSize; private short _dialect; - private bool _disposed; private IntPtr[] _statusVector; private IFbClient _fbClient; @@ -126,27 +125,6 @@ public FesDatabase(string dllName, Charset charset) #endregion - #region IDisposable methods - - public void Dispose() - { - if (!_disposed) - { - _disposed = true; - Detach(); - _warningMessage = null; - _charset = null; - _serverVersion = null; - _statusVector = null; - _transactionCount = 0; - _dialect = 0; - _handle.Dispose(); - _packetSize = 0; - } - } - - #endregion - #region Database Methods public void CreateDatabase(DatabaseParameterBuffer dpb, string dataSource, int port, string database, byte[] cryptKey) @@ -251,6 +229,14 @@ public void Detach() _handle.Dispose(); } + + _warningMessage = null; + _charset = null; + _serverVersion = null; + _statusVector = null; + _transactionCount = 0; + _dialect = 0; + _packetSize = 0; } #endregion diff --git a/Provider/src/FirebirdSql.Data.FirebirdClient/Common/IDatabase.cs b/Provider/src/FirebirdSql.Data.FirebirdClient/Common/IDatabase.cs index 34c0cf41..fbe68589 100644 --- a/Provider/src/FirebirdSql.Data.FirebirdClient/Common/IDatabase.cs +++ b/Provider/src/FirebirdSql.Data.FirebirdClient/Common/IDatabase.cs @@ -21,7 +21,7 @@ namespace FirebirdSql.Data.Common { - internal interface IDatabase : IDisposable + internal interface IDatabase { Action WarningMessage { get; set; } diff --git a/Provider/src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbConnectionInternal.cs b/Provider/src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbConnectionInternal.cs index 4fda05f0..cb633a2f 100644 --- a/Provider/src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbConnectionInternal.cs +++ b/Provider/src/FirebirdSql.Data.FirebirdClient/FirebirdClient/FbConnectionInternal.cs @@ -120,7 +120,8 @@ public void Dispose() public void CreateDatabase(DatabaseParameterBuffer dpb) { - using (var db = ClientFactory.CreateDatabase(_options)) + var db = ClientFactory.CreateDatabase(_options); + try { if (string.IsNullOrEmpty(_options.UserID) && string.IsNullOrEmpty(_options.Password)) { @@ -131,11 +132,16 @@ public void CreateDatabase(DatabaseParameterBuffer dpb) db.CreateDatabase(dpb, _options.DataSource, _options.Port, _options.Database, _options.CryptKey); } } + finally + { + db.Detach(); + } } public void DropDatabase() { - using (var db = ClientFactory.CreateDatabase(_options)) + var db = ClientFactory.CreateDatabase(_options); + try { if (string.IsNullOrEmpty(_options.UserID) && string.IsNullOrEmpty(_options.Password)) { @@ -147,6 +153,10 @@ public void DropDatabase() } db.DropDatabase(); } + finally + { + db.Detach(); + } } #endregion @@ -190,7 +200,7 @@ public void Disconnect() { try { - _db.Dispose(); + _db.Detach(); } catch { }