首页 新闻 会员 周边 捐助

不允许启动新事务,因为有其他线程正在该会话中运行

0
悬赏园豆:50 [待解决问题]

各位,我遇到的问题如题。这个问题一直没解决,每当数据库连接有问题的时候,插入数据不成功。再插入第二条数据的时候就会报这个错误,以后再插入数据也是这样,很是郁闷。而且,在本机调试很难再现,各位帮看一下。这是我用到事务的部分代码。

                    #region tlq接受消息处理
                    if (uMsg.MsgType == TLQ_CONST.FILE_MSG)
                    {
                        try
                        {
                            string getTableName = uMsg.MsgName;  //存储接收到的表名

                            ResouttextBox.Text += DateTime.Now.ToString("yyyy-MM-ddHH:mm:ss.fff") + " 接收到新表:" + getTableName + "\r\n";
                            LOG_OP.WriteLog(" 接收到新表:" + getTableName);

                            //获取当前工作目录
                            string strAppDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);

                            //将接收失败xml文件保存到当前工作目录下的xml文件夹下
                            string destFilename = strAppDir + "\\xml\\" + getTableName;

                            //获取接收文件的存放路径
                            string fileName = TlqRcvFilesDir + getTableName;

                            FileStream fs = new FileStream(fileName, FileMode.Open,FileAccess.Read);   //创建流文件
                            XmlTextReader xtr = new XmlTextReader(fs);  //创建阅读器
                            DataSet ds = new DataSet();     //创建数据集
                            ds.ReadXml(xtr, XmlReadMode.ReadSchema);    //将xml中数据读入到ds中
                            fs.Close();

                            string destTblName = getTableName.Substring(0, getTableName.LastIndexOf("_"));//去掉接收到的xml文件的"_ID"后缀,得到表的名字
                            DataTable dt = ds.Tables[destTblName];  //将表中数据放到dt中

                            SqlCommand command = sqlcn.CreateCommand();
                            SqlTransaction sqlTransaction = sqlcn.BeginTransaction();   //开始数据库事务
                            command.Connection = sqlcn;
                            command.Transaction = sqlTransaction;
                            string getSql = null;
                            int i = 0;
                            try
                            {
                                if (dt.TableName == "TBL_ETC_CONTRAST")
                                {
                                    #region TBL_ETC_CONTRAST表
                                    for (i = 0; i < dt.Rows.Count; i++)
                                    {
                                        string getContrastData = " values('" + dt.Rows[i]["LISTNO"].ToString() + "'," + dt.Rows[i]["EXROADID"].ToString() + "," + dt.Rows[i]["EXCOMPANYID"].ToString() + "," + dt.Rows[i]["EXOWNERID"].ToString() + "," + dt.Rows[i]["EXSTATIONID"].ToString() + ",'" + dt.Rows[i]["TRADDATE"].ToString() + "','" + dt.Rows[i]["STARTCDATE"].ToString() + "'," +dt.Rows[i]["EXITTRADNUM"].ToString() + "," + dt.Rows[i]["EXITTRADMONEY"].ToString() + "," +dt.Rows[i]["FINALMONEY"].ToString() + "," + dt.Rows[i]["BACKMONEY"].ToString() + "," + dt.Rows[i]["TRANSFERTAG"].ToString() + "," + dt.Rows[i]["OKEXITTRADNUM"].ToString() + "," +dt.Rows[i]["OKEXITTRADMONEY"].ToString() + "," + dt.Rows[i]["OKDEPUTEDTRADNUM"].ToString() + "," + dt.Rows[i]["OKDEPUTEDTRADMONEY"].ToString() + "," + dt.Rows[i]["FAILTRADNUM"].ToString() + "," +
dt.Rows[i]["FAILTRADMONEY"].ToString() + ",'" + dt.Rows[i]["BAKE1"].ToString() + "','" + dt.Rows[i]["ENDCDATE"].ToString() + "'," +dt.Rows[i]["BACKNUM"].ToString() + ")";

                                        string getContrastInsert = "insert into TBL_ETC_CONTRAST(LISTNO,EXROADID,EXCOMPANYID,EXOWNERID,EXSTATIONID,TRADDATE,STARTCDATE,EXITTRADNUM," +"EXITTRADMONEY,FINALMONEY,BACKMONEY,TRANSFERTAG,OKEXITTRADNUM,OKEXITTRADMONEY,OKDEPUTEDTRADNUM,OKDEPUTEDTRADMONEY,FAILTRADNUM,FAILTRADMONEY," +" BAKE1,ENDCDATE,BACKNUM)";

                                        getSql = getContrastInsert + getContrastData;

                                        sql_DealAll.sqlInsert(getSql, command);
                                    }
                                    if (i == dt.Rows.Count)
                                    {
                                        sqlTransaction.Commit();
                                        ResouttextBox.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " " + getTableName + " 表下发到" + RevName + "数据库成功\r\n";
                                        LOG_OP.WriteLog(getTableName + " 表下发到" + RevName + "数据库成功");

                                        File.Delete(fileName);
                                    }
                                    #endregion
                                }
                            }
                            catch (Exception e)
                            {
                                sqlTransaction.Rollback();  //下发失败,回滚
                                ResouttextBox.Text += e + "\r\n";
                                ResouttextBox.Text += DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " " + getTableName + "表下发到" + RevName + "数据库失败,数据库回滚\r\n";
                                LOG_OP.WriteLog(e.Message);
                                LOG_OP.WriteLog(getTableName + "表下发到" + RevName + "数据库失败,数据库回滚");


                                if (!File.Exists(destFilename))
                                {
                                    File.Move(fileName, destFilename);
                                }
                                else
                                {
                                    File.Delete(fileName);
                                }
                            }
                        }
                        catch (Exception getEx)
                        {
                            LOG_OP.WriteLog(getEx.ToString());
                            LOG_OP.WriteLog(getEx.Message);
                        }
                    }

问题补充:

错误信息:

System.Data.SqlClient.SqlException: 不允许启动新事务,因为有其他线程正在该会话中运行。
   在 System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   在 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   在 System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
   在 System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
   在 System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest, String name, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
   在 System.Data.SqlClient.SqlInternalConnection.BeginSqlTransaction(IsolationLevel iso, String transactionName)
   在 System.Data.SqlClient.SqlConnection.BeginTransaction(IsolationLevel iso, String transactionName)
   在 System.Data.SqlClient.SqlConnection.BeginTransaction()
   在 TJETCDataClient.TJETCDataClient.Data_AllMsg() 位置 C:\Programe\TJETCData-20110129\TJETCDataClient.cs:行号 287

上面的错误信息每次都是跟在下面的错误之后而出现,真的是不太明白了。

System.Data.SqlClient.SqlException: 超时时间已到。在操作完成之前超时时间已过或服务器未响应。
   在 System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   在 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   在 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   在 System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   在 System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)
   在 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
   在 System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   在 TJETCDataClient.SQL_OP.sqlUpdate(String col, String tablename) 位置 C:\Programe\TJETCData-20110129\SQL_OP.cs:行号 100
   在 TJETCDataClient.TJETCDataClient.TBL_ETC_EXIT() 位置 C:\Programe\TJETCData-20110129\TJETCDataClient.cs:行号 793

sunnyhaizi的主页 sunnyhaizi | 初学一级 | 园豆:80
提问于:2011-06-13 16:09
< >
分享
所有回答(1)
0

使用一个新的 SqlConnection 试试。

鹤冲天 | 园豆:2379 (老鸟四级) | 2011-07-13 08:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册