c# - SqlConnection.Open vs SqlConnection.OpenAsync - what's different between the two beyond the obvious? -
edit: boils down why changing sqlconnection.open() await sqlconnection.openasync() within asynchronous code result in different behavior.
what's difference between sqlconnection.open call in synchronous code , await sqlconnection.openasync call in asynchronous code aside obvious asynchronous behavior? underlying connection made asynchronous database?
the documentation on openasync lite, https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.openasync%28v=vs.110%29.aspx?f=255&mspperror=-2147217396.
an asynchronous version of open, opens database connection settings specified connectionstring. method invokes virtual method openasync cancellationtoken.none.(inherited dbconnection.)
i find interesting connection string required async=true within it, while in .net 4.5+ it's no longer required. connections behave differently?
https://msdn.microsoft.com/en-us/library/hh211418(v=vs.110).aspx
beginning in .net framework 4.5, these methods no longer require asynchronous processing=true in connection string.
when happen use synchronous sqlconnection.open within asynchronous application , load heavily find performs poorly, running connection pool dry early. expected opening connection blocking, however, executing asynchronous commands (through dapper) on connections behaves differently. so, openasync doing differently?
edit:
as requested code reproduce issue (or perhaps demonstrate difference). running case open() connection timeouts encountered @ around 180 concurrent async commands executing, openasync() no exceptions encountered @ on 300 concurrent commands. can push concurrency timeout, it's doing deeper concurrent commands.
using system; using system.collections.generic; using system.data.sqlclient; using system.linq; using system.text; using system.threading; using system.threading.tasks; using dapper; using nito.asyncex; namespace asyncsqlconnectiontest { class program { public static int concurrent_counter = 0; public static int total_counter = 0; static void main(string[] args) { var listtoconsume = enumerable.range(1, 10000).tolist(); parallel.foreach(listtoconsume, new paralleloptions { }, value => { try { task.run(() => asynccontext.run(async () => { using (var conn = new sqlconnection("data source=.; database=master; trusted_connection=true;")) { interlocked.increment(ref concurrent_counter); interlocked.increment(ref total_counter); await conn.openasync(); var result = await conn.queryasync("select * master..spt_values; waitfor delay '00:00:05'"); console.writeline($"#{total_counter}, concurrent: {concurrent_counter}"); interlocked.decrement(ref concurrent_counter); } })).getawaiter().getresult(); } catch (exception e) { console.write(e.tostring()); } }); console.readline(); } } }
edit 2:
here's test finds same differences using nothing ado.net. it's worth noting dapper executes faster, that's not point here. again openasync timeout, 'later' , never if max degree of parallelism 100 (below connection pool size).
using system; using system.data.sqlclient; using system.linq; using system.threading; using system.threading.tasks; namespace asyncsqlconnectiontest { class program { public static int concurrent_counter = 0; public static int total_counter = 0; static void main(string[] args) { var listtoconsume = enumerable.range(1, 10000).tolist(); parallel.foreach(listtoconsume, new paralleloptions { }, value => { try { task.run(async () => { using (var conn = new sqlconnection("data source=.; database=master; trusted_connection=true;")) { interlocked.increment(ref concurrent_counter); interlocked.increment(ref total_counter); // (no errors) await conn.openasync(); // vs. (timeouts) //conn.open(); var cmd = new sqlcommand("select * master..spt_values; waitfor delay '00:00:05'", conn); using (var reader = await cmd.executereaderasync()) { while (await reader.readasync()) { } } console.writeline($"#{total_counter}, concurrent: {concurrent_counter}"); interlocked.decrement(ref concurrent_counter); } }).getawaiter().getresult(); } catch (exception e) { console.write(e.tostring()); } }); console.readline(); } } }
Comments
Post a Comment