Skip to content

Commit

Permalink
IDisposable was confusing, only Detach left (DNET-865).
Browse files Browse the repository at this point in the history
  • Loading branch information
cincuranet committed Aug 1, 2019
1 parent 655bd3d commit 337276c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 91 deletions.
Expand Up @@ -58,7 +58,6 @@ public Action<IscException> WarningMessage
protected string _serverVersion;
private short _packetSize;
private short _dialect;
private bool _disposed;
private XdrStream _xdrStream;

#endregion
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
Expand Up @@ -47,7 +47,6 @@ public Action<IscException> WarningMessage
private Charset _charset;
private short _packetSize;
private short _dialect;
private bool _disposed;
private IntPtr[] _statusVector;

private IFbClient _fbClient;
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -251,6 +229,14 @@ public void Detach()

_handle.Dispose();
}

_warningMessage = null;
_charset = null;
_serverVersion = null;
_statusVector = null;
_transactionCount = 0;
_dialect = 0;
_packetSize = 0;
}

#endregion
Expand Down
Expand Up @@ -21,7 +21,7 @@

namespace FirebirdSql.Data.Common
{
internal interface IDatabase : IDisposable
internal interface IDatabase
{
Action<IscException> WarningMessage { get; set; }

Expand Down
Expand Up @@ -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))
{
Expand All @@ -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))
{
Expand All @@ -147,6 +153,10 @@ public void DropDatabase()
}
db.DropDatabase();
}
finally
{
db.Detach();
}
}

#endregion
Expand Down Expand Up @@ -190,7 +200,7 @@ public void Disconnect()
{
try
{
_db.Dispose();
_db.Detach();
}
catch
{ }
Expand Down

0 comments on commit 337276c

Please sign in to comment.