1

I'm changing our DAL to an async DAL.

Looking at :

await _conn.OpenAsync().ConfigureAwait(false);

I see that there's an async method for open connection.But why there's no async method for closing connection ?

  • A shared connection might used by others
  • Might return to the connection pool
  • It is an I/O operation
  • Possible delayed / time-consuming operation

(I might be wrong about all four above :-))

Question

It seems logic ( to me) that there's should be an async close method for a connection.

Is there a reason why there's not ?

PS I will obviously will use DAPPER at the end , but just for practicing , I've decided to create small mini mini dapper alike DAL.

3
  • 1
    Which specific DBConnection are you referring to?
    – i3arnon
    Commented Jul 1, 2015 at 19:49
  • @i3arnon Does it matter? SqlConnection class inherits dbconnection. my DAL should be general as possible : i.imgur.com/DE4tsP6.png
    – Royi Namir
    Commented Jul 1, 2015 at 19:51
  • 1
    It matters because the default implementation in DBConnection is not actually asynchronous. In SqlConnection however, it is.
    – i3arnon
    Commented Jul 1, 2015 at 19:58

3 Answers 3

4

A shared connection might used by others

Why would this make calling close on it take a long time? If anything this would mean that, in cases where the connection is still being used by others all that "closing" it means is indicating that you no longer needed it, and the actual underlying connection doesn't need to be closed.

It is an I/O operation

Not necessarily. As you said, if it's pooled/shared, then it's just returned to the pool, no IO would happen at all.

And what makes you think that, even if the underlying connection is being closed, that it would take a long time. All that needs to happen is to stop paying attention to the connection, possibly sending a courtesy message through the connection saying that you're done. That's not going to take a long time. You don't need to wait for any type of response to such a message, so you aren't waiting for a completed network round trip in this method.

Possible delayed / time-consuming operation

Why would it be time consuming? If it is delayed (if, for example, the connection is pooled and you're closing it when you're the last user of it) it means that that it'll likely be closed after a bit, but you aren't needing to wait for that.

Marking an object as "no longer in use" simply isn't time consuming, and at the end of the day that's all you're really doing.

0
2

In fact, IDbConnection interface (the one required to implement to develop an ADO.NET connection provider along with other interfaces like IDbCommand) doesn't provide OpenAsync.

Thus, OpenAsync is an implementation detail of DBConnection and, for example, it's the base class of System.Data.SqlClient.SqlConnection.

Why there's no CloseAsync? This should be asked to a .NET Framework Development Team member. When there's a design decision like this, usually it's motivated because of some particular requirement either in the framework itself or in some .NET-based solution developed by Microsoft or some partner. Maybe there's a potential question here: Why IDbConnection doesn't define both Open and Close asynchronous flavors? What about an IDbConnectionAsync interface in order to avoid a breaking change with exisiting code?.

BTW, I suspect opening a connection consumes more time than closing it, because closing process can be queued since the caller just expects an OK I'll do it (from the database server) while opening a connection it's not just signaling but an immediate availability of the connection itself.

Since network connectivity might slowdown the connection opening process, it seems like this is the main reason to implement it as an async operation.

5
  • @i3arnon Correction: It implements IDbConnection. Commented Jul 1, 2015 at 20:03
  • @i3arnon Does my updated text explains the issue better? Commented Jul 1, 2015 at 20:09
  • 1
    Don't really think so. IDbConnection is irrelevant to the question and asking the .Net team isn't necessary as the accepted answer shows.
    – i3arnon
    Commented Jul 1, 2015 at 21:30
  • @i3arnon I'm not agree with you. Even when closing a connection is cheaper than closing it, it might happen that the networking might be slow and sending the close command may take some time, so there should be a CloseAsync as well... Why there's no CloseAsync at all is a design decision that can be answered by a FW developer at MSFT........ Commented Jul 2, 2015 at 5:48
  • @i3arnon About the IDbConnection thing. It's relevant because it demonstrate that even opening a connection is still optional and it's an implementation detail of DBConneciton. OP is asking about why there's an open async and not a close async... Well, because both methods aren't mandatory by contract so implementers might implement them or not depending on their own needs... Commented Jul 2, 2015 at 5:51
0

As of 2019, there is a DbConnection.CloseAsync method; it was added in .NET Standard 2.1 and .NET Core 3.0, along with the IAsyncDisposable interface. The work was tracked by GitHub issue 28596.

It returns a Task, not a ValueTask, because it may be more complicated than just returning a connection to a pool; for example, there may be work that requires I/O associated with it (such as rolling back a transaction or freeing server-side resources for temporary tables or variables).

Not the answer you're looking for? Browse other questions tagged or ask your own question.