Skip to content
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

WIP Add support for BeginTransactionAsync #3139

Open
wants to merge 32 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
59c108f
Generate Async Code of .NET Standard 2.1
hazzik Aug 29, 2022
38ae990
Add shim for DbConnection.BeginTransactionAsync
hazzik Aug 29, 2022
8b483e8
Generate async files
github-actions[bot] Aug 29, 2022
691e967
Update AsyncGenerator.yml
hazzik Aug 29, 2022
e196880
Rename AsynExtensions.cs to AsyncExtensions.cs
hazzik Aug 29, 2022
685267a
Generate async files
github-actions[bot] Aug 29, 2022
d4f9e18
Update AsyncGenerator.yml
hazzik Sep 1, 2022
6d46bf6
Generate async files
github-actions[bot] Sep 1, 2022
de5e256
Update AsyncExtensions.cs
hazzik Sep 1, 2022
d5f6eeb
Update AsyncGenerator.yml
hazzik Sep 1, 2022
85e62a5
Generate async files
github-actions[bot] Sep 1, 2022
78ffed7
Update ITransaction.cs
hazzik Sep 1, 2022
69376ba
Update AsyncGenerator.yml
hazzik Sep 1, 2022
b5a3e35
Update ITransaction.cs
hazzik Sep 1, 2022
275cdd1
Generate async files
github-actions[bot] Sep 1, 2022
d792db3
Update AsyncExtensions.cs
hazzik Sep 1, 2022
c0e48ad
Fix async extensions
hazzik Sep 1, 2022
15c32ee
Register AsyncExtensions.cs
hazzik Sep 14, 2022
dad96b8
Make IDriver not partial
hazzik Sep 14, 2022
2e79402
Update AsyncGenerator.yml
hazzik Sep 14, 2022
8e434c6
Merge branch 'master' into begin-transaction-async
hazzik Sep 14, 2022
fccd63c
Update dotnet-tools.json
hazzik Sep 15, 2022
2146533
Generate async files
github-actions[bot] Sep 15, 2022
f344e0f
Update AsyncGenerator.yml
hazzik Sep 15, 2022
0ec8c4f
Update AsyncGenerator.yml
hazzik Sep 15, 2022
c5f899d
Update AsyncGenerator.yml
hazzik Sep 15, 2022
13e53c4
Update AsyncGenerator.yml
hazzik Sep 15, 2022
7a482b9
Update AsyncGenerator.yml
hazzik Sep 15, 2022
b446b28
Update AsyncGenerator.yml
hazzik Sep 15, 2022
74415d1
Update AsyncGenerator.yml
hazzik Sep 15, 2022
5c98a54
Generate async files
github-actions[bot] Sep 15, 2022
626fa94
Merge branch 'master' into begin-transaction-async
hazzik Jan 22, 2023
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: 2 additions & 2 deletions .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"csharpasyncgenerator.tool": {
"version": "0.20.1",
"version": "0.21.0",
"commands": [
"async-generator"
]
Expand All @@ -15,4 +15,4 @@
]
}
}
}
}
24 changes: 19 additions & 5 deletions src/AsyncGenerator.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
projects:
projects:
- filePath: NHibernate/NHibernate.csproj
targetFramework: netcoreapp2.0
targetFramework: netcoreapp2.1
hazzik marked this conversation as resolved.
Show resolved Hide resolved
concurrentRun: true
applyChanges: true
suppressDiagnosticFailures:
- pattern: ^.*(Hql\.g).*$
analyzation:
methodConversion:
#TODO 6.0: Remove ignore rule for IQueryBatchItem.ProcessResults
#TODO 6.0: Remove ignore rule for IQueryBatchItem.ProcessResults
- conversion: Ignore
name: ProcessResults
containingTypeName: IQueryBatchItem
Expand Down Expand Up @@ -62,7 +62,7 @@
- conversion: Ignore
name: CloseSessionFromSystemTransaction
containingTypeName: ISessionImplementor
# TODO 6.0: Remove ignore rule for IStatelessSession.Close
# TODO 6.0: Remove ignore rule for IStatelessSession.Close
- conversion: Ignore
name: Close
containingTypeName: IStatelessSession
Expand Down Expand Up @@ -114,7 +114,7 @@
- conversion: Ignore
name: GetEnumerator
containingTypeName: IFutureEnumerable
# TODO 6.0: Consider if ComputeFlattenedParameters should remain ignored or not
# TODO 6.0: Consider if ComputeFlattenedParameters should remain ignored or not
- conversion: Ignore
name: ComputeFlattenedParameters
containingTypeName: SqlQueryImpl
Expand All @@ -124,12 +124,17 @@
- conversion: ToAsync
name: ExecuteNonQuery
containingTypeName: IBatcher
- conversion: ToAsync
name: BeginTransaction
containingTypeName: DriverBase
- conversion: ToAsync
rule: EventListener
- conversion: ToAsync
rule: Cache
- conversion: ToAsync
rule: TransactionCompletion
alwaysAwait:
- rule: BeginTransactionAsync
typeConversion:
- conversion: Ignore
name: EnumerableImpl
Expand All @@ -139,6 +144,10 @@
- conversion: Ignore
name: DependentContext
containingTypeName: AdoNetWithSystemTransactionFactory
asyncExtensionMethods:
projectFiles:
- fileName: AsyncExtensions.cs
hazzik marked this conversation as resolved.
Show resolved Hide resolved
projectName: NHibernate
ignoreSearchForAsyncCounterparts:
- name: GetFieldValue
- name: IsDBNull
Expand Down Expand Up @@ -212,6 +221,8 @@
projectFiles:
- fileName: LinqExtensionMethods.cs
projectName: NHibernate
- fileName: AsyncExtensions.cs
hazzik marked this conversation as resolved.
Show resolved Hide resolved
projectName: NHibernate
typeConversion:
- conversion: Ignore
name: ObjectAssert
Expand Down Expand Up @@ -307,6 +318,9 @@ methodRules:
- filters:
- hasAttributeName: ObsoleteAttribute
name: Obsolete
- filters:
- containingTypeName: AsyncExtensions
name: BeginTransactionAsync
typeRules:
- filters:
- containingAssemblyName: NHibernate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ public async Task ShouldNotifyAfterTransactionWithOwnConnectionAsync(bool usePre
public partial class CustomTransaction : ITransaction
{

public Task BeginAsync(CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
}

public Task BeginAsync(IsolationLevel isolationLevel, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
}

public Task CommitAsync(CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ protected DateTimeKind GetTypeKind()
}
}

public class ClientDriverWithParamsStats : IDriver
public partial class ClientDriverWithParamsStats : IDriver
{
private readonly Dictionary<SqlType, int> _usedSqlTypes = new Dictionary<SqlType, int>();
private readonly Dictionary<DbType, int> _usedDbTypes = new Dictionary<DbType, int>();
Expand Down
43 changes: 43 additions & 0 deletions src/NHibernate/AdoNet/AsyncExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#if NETFX || NETSTANDARD2_0 || NETCOREAPP2_0
using System;
using System.Data;
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;

namespace NHibernate
{
internal static class AsyncExtensions
{
public static Task<DbTransaction> BeginTransactionAsync(this DbConnection connection, CancellationToken cancellationToken = default)
hazzik marked this conversation as resolved.
Show resolved Hide resolved
{
if (cancellationToken.IsCancellationRequested)
return Task.FromCanceled<DbTransaction>(cancellationToken);

try
{
return Task.FromResult(connection.BeginTransaction());
}
catch (Exception ex)
{
return Task.FromException<DbTransaction>(ex);
}
}

public static Task<DbTransaction> BeginTransactionAsync(this DbConnection connection, IsolationLevel isolationLevel, CancellationToken cancellationToken = default)
{
if (cancellationToken.IsCancellationRequested)
return Task.FromCanceled<DbTransaction>(cancellationToken);

try
{
return Task.FromResult(connection.BeginTransaction(isolationLevel));
}
catch (Exception ex)
{
return Task.FromException<DbTransaction>(ex);
}
}
}
}
#endif
2 changes: 1 addition & 1 deletion src/NHibernate/AdoNet/ResultSetWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace NHibernate.AdoNet
/// and Postgres).
/// </summary>
/// <seealso cref="IDataRecord.GetOrdinal"/>
public class ResultSetWrapper : DbDataReader
public partial class ResultSetWrapper : DbDataReader
{
private DbDataReader rs;
private readonly ColumnNameCache columnNameCache;
Expand Down
16 changes: 16 additions & 0 deletions src/NHibernate/Async/AdoNet/ConnectionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ async Task<DbConnection> InternalGetConnectionAsync()
}
}

public async Task<ITransaction> BeginTransactionAsync(IsolationLevel isolationLevel, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
EnsureTransactionIsCreated();
await (_transaction.BeginAsync(isolationLevel, cancellationToken)).ConfigureAwait(false);
return _transaction;
}

public async Task<ITransaction> BeginTransactionAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
EnsureTransactionIsCreated();
await (_transaction.BeginAsync(cancellationToken)).ConfigureAwait(false);
return _transaction;
}

public async Task<DbCommand> CreateCommandAsync(CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
Expand Down
61 changes: 61 additions & 0 deletions src/NHibernate/Async/Driver/DriverBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.SqlTypes;
using NHibernate.Util;
using Environment = NHibernate.Cfg.Environment;

namespace NHibernate.Driver
{
using System.Threading.Tasks;
using System.Threading;
public abstract partial class DriverBase : IDriver, ISqlParameterFormatter
{

/// <summary>
/// Begin an ADO <see cref="DbTransaction" />.
/// </summary>
/// <param name="isolationLevel">The isolation level requested for the transaction.</param>
/// <param name="connection">The connection on which to start the transaction.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
/// <returns>The started <see cref="DbTransaction" />.</returns>
public virtual Task<DbTransaction> BeginTransactionAsync(IsolationLevel isolationLevel, DbConnection connection, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<DbTransaction>(cancellationToken);
}
try
{
if (isolationLevel == IsolationLevel.Unspecified)
{
return connection.BeginTransactionAsync(cancellationToken);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@maca88 I tried to fix this by using 'alwaysAwait' option, but I could not quite figure out the syntax.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fix this change the alwaysAwait section to:

alwaysAwait:
    - name: BeginTransaction

The filter BeginTransactionAsync could also work but the containingTypeName filter would need to be changed from AsyncExtensions to DriverBase.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does it work? I thought it identifies the method that should be always awaited.

Copy link
Contributor

@maca88 maca88 Sep 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With alwaysAwait you define filters that are applied on every generated async method to check whether to force await invocations inside the method body. So if we take the above filter as an example, every generated async method that has a sync counterpart named BeginTransaction will be generated by using async/await even if some of them could be generated without it.

}
return connection.BeginTransactionAsync(isolationLevel, cancellationToken);
}
catch (Exception ex)
{
return Task.FromException<DbTransaction>(ex);
}
}

#if NETFX
#else

#endif
}
}
60 changes: 60 additions & 0 deletions src/NHibernate/Async/Driver/DriverExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------


using System.Data;
using System.Data.Common;
using NHibernate.AdoNet;
using NHibernate.SqlTypes;
using NHibernate.Util;

namespace NHibernate.Driver
{
using System.Threading.Tasks;
using System.Threading;
public static partial class DriverExtensions
{

// 6.0 TODO: merge into IDriver
/// <summary>
/// Begin an ADO <see cref="DbTransaction" />.
/// </summary>
/// <param name="driver">The driver.</param>
/// <param name="isolationLevel">The isolation level requested for the transaction.</param>
/// <param name="connection">The connection on which to start the transaction.</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
/// <returns>The started <see cref="DbTransaction" />.</returns>
public static Task<DbTransaction> BeginTransactionAsync(this IDriver driver, IsolationLevel isolationLevel, DbConnection connection, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<DbTransaction>(cancellationToken);
}
try
{
if (driver is DriverBase driverBase)
{
return driverBase.BeginTransactionAsync(isolationLevel, connection, cancellationToken);
}

// So long for custom drivers not deriving from DriverBase, they will have to wait for 6.0 if they
// need the feature.
if (isolationLevel == IsolationLevel.Unspecified)
{
return connection.BeginTransactionAsync(cancellationToken);
}
return connection.BeginTransactionAsync(isolationLevel, cancellationToken);
}
catch (System.Exception ex)
{
return Task.FromException<DbTransaction>(ex);
}
}
}
}
14 changes: 13 additions & 1 deletion src/NHibernate/Async/ITransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,26 @@
using System;
using System.Data;
using System.Data.Common;
using System.Threading.Tasks;
using NHibernate.Transaction;

namespace NHibernate
{
using System.Threading.Tasks;
using System.Threading;
public partial interface ITransaction : IDisposable
{
/// <summary>
/// Begin the transaction with the default isolation level.
/// </summary>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
Task BeginAsync(CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Begin the transaction with the specified isolation level.
/// </summary>
/// <param name="isolationLevel">Isolation level of the transaction</param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
Task BeginAsync(IsolationLevel isolationLevel, CancellationToken cancellationToken = default(CancellationToken));

/// <summary>
/// Flush the associated <c>ISession</c> and end the unit of work.
Expand Down
Loading