连接池Oracle ASP .NET

时间:2017-03-10 07:51:31

标签: c# asp.net oracle iis

我有一个Asp.NET Web服务,它有大约20到30种方法可供公开。对于数据库,我使用Oracle数据库,在删除重复的wordNet和单个类的DB操作的帮助下,对于每个请求我打开连接并在其工作完成后关闭,这意味着我关闭了所有的连接我的代码中没有连接泄漏。我已经检查了10次,但是当我去检查数据库(Oracle 11G)时,有许多会话处于非活动状态超过20小时。我正在V $ Session表中查看这些会话,但我很困惑,即使我关闭所有连接,它们也不会在很长一段时间内被破坏。

请与我分享您的答案,因为我可以做的就是关闭所有连接并且我做得很好但是仍有很多非活动会话超过20小时。这怎么可能 ?

TNS.ORA

XXX =   (DESCRIPTION =     (ADDRESS =       (PROTOCOL = TCP)       (HOST = X.X.X.X)       (PORT = XXXX)     )     (CONNECT_DATA =       (SERVER =共享)       (SERVICE_NAME = XXXX)     )   )

My Connection String获取将提供连接字符串的类 "用户ID = XXXXXX;数据源= XXXXX;密码= XXXXX"

public class StaticConnectionStringClass
{

    public static readonly string connectionString;
    private static readonly string ConnectionSourceWebService;

    static StaticConnectionStringClass()
    {
        string ConnectionStringType = Convert.ToString(ConfigurationManager.AppSettings["ConnectionStringType"]);
        ConnectionSourceWebService = Convert.ToString(ConfigurationManager.AppSettings["ConnectionSourceWebService"]);
        try
        {
            if (ConnectionSourceWebService == "Y")
            {
                ConnectionStringWS objConnectionStringWS = new ConnectionStringWS();
                connectionString = objConnectionStringWS.GetConnectionString(ConnectionStringType);
                objConnectionStringWS.Dispose();
                objConnectionStringWS = null;
            }
            else
            {
                connectionString = Convert.ToString(ConfigurationManager.ConnectionStrings["ConStr"]);
            }
        }
        catch (Exception ex) { }
        finally
        {
            if (string.IsNullOrEmpty(connectionString))
            {
                connectionString = Convert.ToString(ConfigurationManager.ConnectionStrings["ConStr"]);
            }

        }
    }
}

我的DBHelper类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Data;

public class OracleHelper
{
    private string strConnectionString;
    private OracleConnection oConnection;
    public OracleCommand oCommand;
    private int iTimeOut = 60;

    public enum ExpectedType
    {
        StringType = 0,
        NumberType = 1,
        DateType = 2,
        BooleanType = 3,
        ImageType = 4
    }

    public OracleHelper()
    {
        try
        {

            strConnectionString = StaticConnectionStringClass.connectionString;

            oConnection = new OracleConnection(strConnectionString);
            oCommand = new OracleCommand();
            oCommand.CommandTimeout = iTimeOut;
            oCommand.Connection = oConnection;

        }
        catch (Exception ex)
        {
            throw new Exception("Error initializing OracleHelper class." + Environment.NewLine + ex.Message);
        }
    }

    public OracleHelper(string MyConnectionString)
    {
        try
        {
            strConnectionString = MyConnectionString;
            oConnection = new OracleConnection(strConnectionString);
            oCommand   = new OracleCommand();
            oCommand.CommandTimeout = iTimeOut;
            oCommand.Connection = oConnection;
        }
        catch (Exception ex)
        {
            throw new Exception("Error initializing OracleHelper class." + Environment.NewLine + ex.Message);
        }
    }

    public void Dispose()
    {
        try
        {
            //Dispose off connection object
            if (oConnection != null)
            {
                if (oConnection.State != ConnectionState.Closed)
                {
                    oConnection.Close();
                }
                oConnection.Dispose();
            }

            //Clean Up Command Object
            if (oCommand != null)
            {
                oCommand.Dispose();

            }

        }

        catch (Exception ex)
        {
            throw new Exception("Error disposing OracleHelper class." + Environment.NewLine + ex.Message);
        }

    }

    public void CloseConnection()
    {
        if (oConnection.State != ConnectionState.Closed) oConnection.Close();
    }

    public int GetExecuteScalarByCommand(string Command)
    {

        object identity = 0;
        try
        {
            oCommand.CommandText = Command;
            oCommand.CommandTimeout = iTimeOut;
            oCommand.CommandType = CommandType.StoredProcedure;

            oConnection.Open();

            oCommand.Connection = oConnection;
            identity = oCommand.ExecuteScalar();
            CloseConnection();
        }
        catch (Exception ex)
        {
            CloseConnection();
            throw ex;
        }

        return Convert.ToInt32(identity);
    }

    public void GetExecuteNonQueryByCommand(string Command)
    {
        try
        {
            oCommand.CommandText = Command;
            oCommand.CommandTimeout = iTimeOut;
            oCommand.CommandType = CommandType.StoredProcedure;

            oConnection.Open();

            oCommand.Connection = oConnection;
            oCommand.ExecuteNonQuery();

            CloseConnection();
        }
        catch (Exception ex)
        {
            CloseConnection();
            throw ex;
        }

    }

    public void GetExecuteNonQueryBySQL(string strSQL)
    {
        try
        {
            oCommand.CommandText = strSQL;
            oCommand.CommandTimeout = iTimeOut;
            oCommand.CommandType = CommandType.Text;

            oConnection.Open();

            oCommand.Connection = oConnection;
            oCommand.ExecuteNonQuery();

            CloseConnection();
        }
        catch (Exception ex)
        {
            CloseConnection();
            throw ex;
        }

    }

    public DataSet GetDatasetByCommand(string Command)
    {
        try
        {
            oCommand.CommandText = Command;
            oCommand.CommandTimeout = iTimeOut;
            oCommand.CommandType = CommandType.StoredProcedure;
            oConnection.Open();
            OracleDataAdapter adpt = new OracleDataAdapter(oCommand);
            DataSet ds = new DataSet();
            adpt.Fill(ds);
            return ds;
        }
        catch (Exception ex)
        {
            CloseConnection();
            throw ex;
        }

    }

    public DataSet GetDatasetBySQL(string strSQL)
    {
        try
        {
            oCommand.CommandText = strSQL;
            oCommand.CommandTimeout = iTimeOut;
            oCommand.CommandType = CommandType.Text;

            oConnection.Open();

            OracleDataAdapter Adapter = new OracleDataAdapter(strSQL, oConnection);
            DataSet ds = new DataSet();
            Adapter.Fill(ds);
            return ds;
        }
        catch (Exception ex)
        {
            CloseConnection();
            throw ex;
        }

    }


    public OracleDataReader GetReaderBySQL(string strSQL)
    {
        oConnection.Open();
        try
        {
            OracleCommand myCommand = new OracleCommand(strSQL, oConnection);
            return myCommand.ExecuteReader();
        }
        catch (Exception ex)
        {
            CloseConnection();
            throw ex;
        }

    }

    public OracleDataReader GetReaderByCmd(string Command)
    {
        OracleDataReader objOracleDataReader = null;
        try
        {
            oCommand.CommandText = Command;
            oCommand.CommandType = CommandType.StoredProcedure;
            oCommand.CommandTimeout = iTimeOut;

            oConnection.Open();
            oCommand.Connection = oConnection;

            objOracleDataReader = oCommand.ExecuteReader();
            return objOracleDataReader;
        }
        catch (Exception ex)
        {
            CloseConnection();
            throw ex;
        }
    }

    public void AddParameterToSQLCommand(string ParameterName, OracleType ParameterType)
    {
        try
        {
            oCommand.Parameters.Add(new OracleParameter(ParameterName, ParameterType));
        }

        catch (Exception ex)
        {
            throw ex;
        }
     }

    public void AddParameterToSQLCommand(string ParameterName, string value ,OracleType ParameterType, int ParameterSize, ParameterDirection parameterDirection)
    {
        try
        {
            // oCommand.Parameters.Add(new OracleParameter(ParameterName, ParameterType, ParameterSize, parameterDirection));

            oCommand.Parameters.Add(new OracleParameter 
            {
                Direction = parameterDirection,
                ParameterName = ParameterName,
                Size = ParameterSize,
                OracleType = ParameterType,
                Value = value
            });

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public void SetSQLCommandParameterValue(string ParameterName, object Value)
    {
        try
        {
            oCommand.Parameters[ParameterName].Value = Value;
        }

        catch (Exception ex)
        {
            throw ex;
        }
    }
}

我的ASp.Net Web服务方法

  [WebMethod]
    public TestList Test(string id)
    {
        string Message = string.Empty;
        TestList objTestList  = new TestList();
        OracleHelper dbHelper =null;

        try
        {
            dbHelper = new OracleHelper();
            dbHelper.AddParameterToSQLCommand("PI_1", id, OracleType.VarChar, 2000, ParameterDirection.Input);
            dbHelper.AddParameterToSQLCommand("PO_CURSOR", string.Empty, OracleType.Cursor, 2000, ParameterDirection.Output);
            dbHelper.AddParameterToSQLCommand("PO_2", string.Empty, OracleType.VarChar, 2000, ParameterDirection.Output);

            DataSet ds = dbHelper.GetDatasetByCommand("Oracle_LOCAL_PROCEDURE_NAME");
            Message = Convert.ToString(dbHelper.oCommand.Parameters["PO_MESSAGE"].Value);
            objTestList  .Message = Message;
            dbHelper.CloseConnection();
         }
        catch (Exception ex)
        {
            if (dbHelper!=null)
            dbHelper.CloseConnection();
            objTestList  .Message = ex.Message;
        }
        finally
        {
            if (dbHelper != null)
            dbHelper.Dispose();
        }
        return objTestList  ;
    }

0 个答案:

没有答案