Skip to content

Fix transaction dispose #233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/AdoNetCore.AseClient/AdoNetCore.AseClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,22 @@
</PropertyGroup>
<PropertyGroup>
<LangVersion>7</LangVersion>
<Version>0.19.3</Version>
<FileVersion>0.19.3</FileVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<IncludeSymbols>True</IncludeSymbols>
</PropertyGroup>
<ItemGroup>
<Compile Update="Properties\Resources.Designer.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/AdoNetCore.AseClient/AseConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ public override string ConnectionString
public override ConnectionState State => InternalState;
private ConnectionState InternalState
{
get => _state;
get => _internal != null && _internal.IsDoomed ? ConnectionState.Broken : _state;
set
{
if (_isDisposed)
Expand Down
36 changes: 28 additions & 8 deletions src/AdoNetCore.AseClient/AseTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Diagnostics;

namespace AdoNetCore.AseClient
{
Expand Down Expand Up @@ -141,16 +142,33 @@ public override void Commit()
/// </summary>
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);

if (_isDisposed)
{
return;
}

Rollback();

_isDisposed = true;
try
{
// Only rollback if the transaction is still open and the connection is open. For sure do not want to
// attempt to rollback a transaction on a closed or broken connection. The only other state in the
// ConnectionState that's currently used is Connecting and it doesn't seem appropriate to attempt a
// rollback from the Connecting state.
if (!_complete && _connection.State == ConnectionState.Open)
{
ExecuteRollback();
}
}
catch (Exception ex)
{
Debug.Assert(false, "Failed to rollback transaction during dispose");
#if NETFRAMEWORK || NETSTANDARD2_0
Trace.TraceError(ex.ToString());
#endif
}
finally
{
base.Dispose(disposing);
_isDisposed = true;
}
}

internal bool IsDisposed => _isDisposed;
Expand All @@ -170,8 +188,10 @@ public override void Rollback()
return;
}

using (var command = _connection.CreateCommand())
{
ExecuteRollback();
}
private void ExecuteRollback() {
using (var command = _connection.CreateCommand()) {
command.CommandText = "ROLLBACK TRANSACTION";
command.CommandType = CommandType.Text;
command.Transaction = this;
Expand Down
64 changes: 64 additions & 0 deletions src/AdoNetCore.AseClient/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

101 changes: 101 additions & 0 deletions src/AdoNetCore.AseClient/Properties/Resources.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 1.3
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">1.3</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1">this is my long string</data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
[base64 mime encoded serialized .NET Framework object]
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
[base64 mime encoded string representing a byte array form of the .NET Framework object]
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->

<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>1.3</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.3500.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
17 changes: 17 additions & 0 deletions test/AdoNetCore.AseClient.Tests/Unit/AseConnectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,23 @@ public void RepeatedDisposal_DoesNotThrow()
connection.Dispose();
}

[Test]
public void DoomedReturnsBroken() {
var mockConnection = new Mock<IInternalConnection>();
var mockConnectionPoolManager = new Mock<IConnectionPoolManager>();

mockConnectionPoolManager
.Setup(x => x.Reserve(It.IsAny<string>(), It.IsAny<ConnectionParameters>(), It.IsAny<IInfoMessageEventNotifier>(), It.IsAny<RemoteCertificateValidationCallback>()))
.Returns(mockConnection.Object);

mockConnection.SetupGet(x => x.IsDoomed).Returns(true);

using (var connection = new AseConnection("Data Source=myASEserver;Port=5000;Database=foo;Uid=myUsername;Pwd=myPassword;", mockConnectionPoolManager.Object)) {
connection.Open();
Assert.AreEqual(ConnectionState.Broken, connection.State);
}
}

private static IConnectionPoolManager InitMockConnectionPoolManager()
{
var mockConnection = new Mock<IInternalConnection>();
Expand Down
Loading