首页 .NET/Web 本地正常,放在IIS服务器上面偶尔会出现 列""不属于表Table

本地正常,放在IIS服务器上面偶尔会出现 列""不属于表Table

作者:胡同里的砖头 围观群众:1042 更新于:2013-12-19

 

这一类的错误,仔细查看代码,发现下面的问题:

 

下面的代码是负责创建数据库连接,同时打开或者不打开数据库事务(下面的代码是客户的错误代码)。

 

namespace haierp.DataAccessLayer
{
  public sealed class SqlHelper
    {
        private static SqlConnection conn = null;
        private static SqlCommand command = null;
        private static SqlTransaction tran = null;

        public static SqlCommand GetCommand(string connectstr)
        {
            conn = new SqlConnection(connectstr);
            conn.Open();
            command = conn.CreateCommand();
            command.CommandTimeout = 60000;
            return command;
        }

        public static bool EndCommand()
        {
            bool state = false;
            try
            {

                command.Connection.Close();
                state = true;
            }
            catch
            {
                state = false;
                conn.Close();
            }
            return state;
        }

        public static bool SqlCommit(SqlCommand command)
        {
            bool state = false;
            try
            {
                command.Transaction.Commit();
                state = true;
            }
            catch
            {
                state = false;
            }
            finally
            {
                command.Connection.Close();
                command.Connection = null;
                command = null;
            }
            return state;
        }

        public static bool SqlRollback(SqlCommand command)
        {
            bool state = false;
            try
            {
                command.Transaction.Rollback();
                state = true;
            }
            catch
            {
                state = false;
            }
            finally
            {
                command.Connection.Close();
                command.Connection = null;
                command = null;
            }
            return state;
        }
    }
}

 

由于这是用于ASP .NET的共用代码,因此存在多个客户并发访问的问题,同时由于使用了上面红色标注的静态

私有变量,因此多个客户ASP线程是共享SqlConnection,SqlCommand,SqlTransaction,于是一个线程

生成的conn,就可能被别的线程关闭,一个线程生成的Transaction,就可能被别的线程提交或者回滚。

 

正确的做法是:取消使用这里的静态私有变量,采用动态对象,或者函数参数的形式,以保证各线程使用自己私有的

SqlConnection,SqlCommand,SqlTransaction对象,避免彼此干扰。如下所示是正确的代码:

 

namespace haierp.DataAccessLayer
{

    public sealed class SqlHelper
    {

        //private static SqlConnection conn = null;
        //private static SqlCommand command = null;
        //private static SqlTransaction tran = null;

        public static SqlCommand GetCommand(string connectstr)
        {
            SqlConnection conn = new SqlConnection(connectstr);
            conn.Open();
            SqlCommand command = conn.CreateCommand();
            command.CommandTimeout = 60000;
            return command;
        }

        public static SqlCommand GetTranCommand(string connectionString)
        {
            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();
            SqlCommand command = conn.CreateCommand();
            SqlTransaction tran = conn.BeginTransaction();
            command.Transaction = tran;
            command.CommandTimeout = 60000;

            return command;
        }

        public static void EndCommand(SqlCommand command)
        {
            if (command.Connection.State == ConnectionState.Open)
            {
                command.Connection.Close();
            }
        }

        public static void SqlCommit(SqlCommand command)
        {
            command.Transaction.Commit();
            if (command.Connection.State == ConnectionState.Open)
            {
                command.Connection.Close();
            }
        }

        public static void SqlRollback(SqlCommand command)
        {
            command.Transaction.Rollback();
            if (command.Connection.State == ConnectionState.Open)
            {
                command.Connection.Close();
            }
        }
    }
}

  • 本文标题: 本地正常,放在IIS服务器上面偶尔会出现 列""不属于表Table
  • 文章分类:【.NET/Web】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8