c# - SMO to Azure SQL using multiple threads -
i have multithreaded service, uses smo read table , column names , may alter database. looking azure , proceeded try code there. has been working sql server long time. in azure sql, when using 1 thread, works fine. when 2nd thread starts reading while 1st thread still executing, exception thrown:
system.invalidoperationexception: there open datareader associated command must closed first. @ system.data.sqlclient.sqlinternalconnectiontds.validateconnectionforexecute(sqlcommand command) @ system.data.sqlclient.sqlcommand.validatecommand(string method, boolean async) @ system.data.sqlclient.sqlcommand.runexecutereader(commandbehavior cmdbehavior, runbehavior runbehavior, boolean returnstream, string method, taskcompletionsource`1 completion, int32 timeout, task& task, boolean& usedcache, boolean asyncwrite, boolean inretry) @ system.data.sqlclient.sqlcommand.runexecutereader(commandbehavior cmdbehavior, runbehavior runbehavior, boolean returnstream, string method) @ system.data.sqlclient.sqlcommand.executereader(commandbehavior behavior, string method) @ system.data.sqlclient.sqlcommand.executereader() @ microsoft.sqlserver.management.common.connectionmanager.executetsql(executetsqlaction action, object execobject, dataset filldataset, boolean catchexception) @ microsoft.sqlserver.management.common.serverconnection.getexecutereader(sqlcommand command) @ microsoft.sqlserver.management.smo.executesql.getdatareader(string query, sqlcommand& command) @ microsoft.sqlserver.management.smo.dataprovider.setconnectionandquery(executesql execsql, string query) @ microsoft.sqlserver.management.smo.executesql.getdataprovider(stringcollection query, object con, statementbuilder sb, retrivemode rm) @ microsoft.sqlserver.management.smo.sqlobjectbase.filldata(resulttype resulttype, stringcollection sql, object connectioninfo, statementbuilder sb) @ microsoft.sqlserver.management.smo.sqlobjectbase.filldatawithusefailure(sqlenumresult sqlresult, resulttype resulttype) @ microsoft.sqlserver.management.smo.sqlobjectbase.buildresult(enumresult result) @ microsoft.sqlserver.management.sdk.sfc.environment.getdata() @ microsoft.sqlserver.management.sdk.sfc.environment.getdata(request req, object ci) @ microsoft.sqlserver.management.sdk.sfc.enumerator.getdata(object connectioninfo, request request) @ microsoft.sqlserver.management.smo.executionmanager.getenumeratordatareader(request req) @ microsoft.sqlserver.management.smo.sqlsmoobject.getinitdatareader(string[] fields, orderby[] orderby) @ microsoft.sqlserver.management.smo.sqlsmoobject.implinitialize(string[] fields, orderby[] orderby) @ microsoft.sqlserver.management.smo.sqlsmoobject.initialize(boolean allproperties) @ microsoft.sqlserver.management.smo.smocollectionbase.initializechildobject(objectkeybase key) @ microsoft.sqlserver.management.smo.smocollectionbase.getobjectbykey(objectkeybase key) @ microsoft.sqlserver.management.smo.tablecollection.get_item(string name)
fierce googling has not produced feasible, using smo should fine, if not share between threads. updated new official smo 2017 nuget package microsoft, did not help. after 1st thread finished, second operates fine 1 thread can reading database. appreciated.
the code use read db follows:
private database getdatabase() { //do not have db every time on same thread. if (_db != null) return _db; //getconnection returns new sqlconnection using (var conn = _executor.getconnection() sqlconnection) { conn.open(); var sconn = new serverconnection(conn); var server = new server(sconn); _db = server.databases[sconn.databasename]; _db.defaultschema = "dbo"; //problem azure: //enumerate these ensure immediate crash, when 2nd thread kicks in //and 1st thread still executing. //works fine sql server foreach (table t in _db.tables) { foreach (column c in t.columns) { } } } return _db; }
Comments
Post a Comment