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

BeginTransaction is not async results in connection to be opened async #3051

Open
ramonsmits opened this issue Apr 27, 2022 · 2 comments · May be fixed by #3052
Open

BeginTransaction is not async results in connection to be opened async #3051

ramonsmits opened this issue Apr 27, 2022 · 2 comments · May be fixed by #3052

Comments

@ramonsmits
Copy link
Contributor

BeginTransaction is not async results in connection to be opened async. I'm not entirely sure but I assume BeginTransaction will result in a transaction to be started if not already running thus a connection to be opened. I'm getting FirstChangeExceptions on my NpgSql setup

System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (10035): A non-blocking socket operation could not be completed immediately. 192.168.149.149:5433

In npgsql/npgsql#1183 it is mentioned this is because the connection is opened via .Open instead of .OpenAsync. I looked in the code and DriverConnectionProvider supports async but it seems it isn't used. Further analys shows that DriverBase doesn't have an async BeginTransaction.

I think an async BeginTransaction makes sense but maybe there is a workaround?

@ramonsmits ramonsmits linked a pull request Apr 27, 2022 that will close this issue
2 tasks
@bahusoid
Copy link
Member

bahusoid commented Apr 29, 2022

but maybe there is a workaround

You can try to force session to open async connection before calling session.BeginTransaction. Something like:

await (session.GetSessionImplementation().ConnectionManager.GetConnectionAsync(cancelationToken)).ConfigureAwait(false);

@ramonsmits
Copy link
Contributor Author

@bahusoid thanks for that tip, I've created the following extension method around it which I'll test later.

static class SessionExt
{
    public async static Task<ITransaction> BeginTransactionAsync(
        this ISession session,
        IsolationLevel isolationLevel = IsolationLevel.Unspecified,
        CancellationToken cancellationToken = default
        )
    {
        await session
            .GetSessionImplementation()
            .ConnectionManager
            .GetConnectionAsync(cancellationToken)
            .ConfigureAwait(false);

        return session.BeginTransaction(isolationLevel);
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants