Skip to content
Open
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
4 changes: 4 additions & 0 deletions source/AliaSQL.Core/AliaSQL.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Transactions" />
<Reference Include="System.Xml" />
Expand All @@ -69,7 +70,10 @@
<Compile Include="Model\ConnectionSettings.cs" />
<Compile Include="Model\TaskAttributes.cs" />
<Compile Include="RequestedDatabaseAction.cs" />
<Compile Include="Services\Impl\ApplicationSettings.cs" />
<Compile Include="Services\Impl\DatabaseBaseliner.cs" />
<Compile Include="Services\IApplicationSettings.cs" />
<Compile Include="Services\Impl\TransactionProvider.cs" />
<Compile Include="Services\ITestDataScriptExecutor.cs" />
<Compile Include="Services\IChangeScriptExecutor.cs" />
<Compile Include="Services\IConnectionStringGenerator.cs" />
Expand Down
9 changes: 9 additions & 0 deletions source/AliaSQL.Core/Services/IApplicationSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace AliaSQL.Core.Services
{
internal interface IApplicationSettings
{
TimeSpan? Timeout();
}
}
25 changes: 25 additions & 0 deletions source/AliaSQL.Core/Services/Impl/ApplicationSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Configuration;

namespace AliaSQL.Core.Services.Impl
{
internal class ApplicationSettings : IApplicationSettings
{
public TimeSpan? Timeout()
{
var timeoutValue = ConfigurationManager.AppSettings["aliasql.transaction.timeout"];

if (string.IsNullOrEmpty(timeoutValue))
{
return null;
}

if (!TimeSpan.TryParse(timeoutValue, out var time))
{
return null;
}

return time;
}
}
}
2 changes: 1 addition & 1 deletion source/AliaSQL.Core/Services/Impl/QueryExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void ExecuteNonQuery(ConnectionSettings settings, string sql, bool includ
public void ExecuteNonQueryTransactional(ConnectionSettings settings, string sql)
{
//do all this in a single transaction
using (var scope = new TransactionScope())
using (var scope = new TransactionProvider().CreateTransactionScope())
{
string connectionString = _connectionStringGenerator.GetConnectionString(settings, true);
using (var connection = new SqlConnection(connectionString))
Expand Down
34 changes: 34 additions & 0 deletions source/AliaSQL.Core/Services/Impl/TransactionProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Reflection;
using System.Transactions;

namespace AliaSQL.Core.Services.Impl
{
internal class TransactionProvider
{
private readonly IApplicationSettings _applicationSettings;

public TransactionProvider()
{
_applicationSettings = new ApplicationSettings();
}

private void SetTransactionManagerField(string fieldName, object value)
{
typeof(TransactionManager).GetField(fieldName, BindingFlags.NonPublic | BindingFlags.Static)?.SetValue(null, value);
}

public TransactionScope CreateTransactionScope()
{
var timeout = _applicationSettings.Timeout();

if (timeout == null)
{
return new TransactionScope();
}

SetTransactionManagerField("_cachedMaxTimeout", true);
SetTransactionManagerField("_maximumTimeout", timeout.GetValueOrDefault());
return new TransactionScope(TransactionScopeOption.RequiresNew, timeout.GetValueOrDefault());
}
}
}