【问题标题】:Oracle.DataAccess.Client.OracleException - <error: an exception of type: {System.NullReferenceException} occurred> stringOracle.DataAccess.Client.OracleException - <错误:发生类型异常:{System.NullReferenceException} > 字符串
【发布时间】:2016-07-02 09:45:36
【问题描述】:

我需要您的帮助来解决当您尝试打开与数据库的 oracle 连接时发生的此错误。 Connection.Open ();

连接字符串值:

"Data Source=dbora1;Max Pool Size=50;Min Pool Size=1;Connection Lifetime=120;Enlist=true;User Id=slu;Password=slu_4d332;"   string

异常为null,strack trace如下:

StackTrace  "   
at Oracle.DataAccess.Client.OracleException.HandleErrorHelper(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, String procedure)
at Oracle.DataAccess.Client.OracleException.HandleError(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, Object src)
at Oracle.DataAccess.Client.OracleConnection.Open()
at Oracle.DataAccess.Client.OracleConnection.Open()
at Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase.OpenConnection() in c:\\v\\enterpriselibrary\\front_end\\fuentes\\data\\oracle\\oracledatabase.cs:line 444" string

他们还留下了image of the quickview of the variable that captured the exepcion.

这里是初始化数据库连接的完整代码:

protected override IDbConnection OpenConnection()
        {
            OracleConnection connection = (OracleConnection)GetConnection();
            try
            {
                //Test the connection context mark.
                if ( connection.State == ConnectionState.Closed )
                {
                    connection.Open();
                }
                this.instrumentation.ConnectionOpened(ConnectionStringNoCredentials);
                return connection;
            }

            catch (System.ObjectDisposedException)
            {
                connection = (OracleConnection)GetConnection( true );

                try
                {
                    connection.Open();
                    return connection;
                }
                catch
                {
                    connection.Close();
                    throw;
                }
            }
            catch(System.InvalidOperationException e)
            {
                // Log in eventviewer
                LogConnectionPoolTimeOutEvent(e);

                connection.Close();
                this.instrumentation.ConnectionFailed(ConnectionStringNoCredentials);
                throw;

            }
            catch (System.OutOfMemoryException)
            {
                connection.Close();
                this.instrumentation.ConnectionFailed(ConnectionStringNoCredentials);
                throw;
            }
            catch(Exception ex)
            {
                connection.Close();
                this.instrumentation.ConnectionFailed(ConnectionStringNoCredentials);
                throw;
            }
        }

而方法代码GetConnection()为:

public override IDbConnection GetConnection()
{
    return GetConnection( false );
}

protected IDbConnection GetConnection( bool renew )
{
    IDictionary connectionHolder;
    OracleConnection tempConn;
    //Test the connection context mark.
    if ( ConnectionContext.CachedConnectionContext )
    {//If the mark is in the callcontext
        //Get the connection holder
        connectionHolder = CallContext.GetData(ConnectionContext.CALLCONTEXTKEY) as IDictionary;
        //If the connection holder does not exists
        if ( connectionHolder == null )
        {
            //create a conection holder
            connectionHolder = new Hashtable();
            //create the initial connection
            tempConn = new OracleConnection(base.ConnectionString);
            //add the connection to the holder
            connectionHolder.Add(base.ConnectionString, tempConn);
            //save the holder in the call context
            CallContext.SetData(ConnectionContext.CALLCONTEXTKEY, connectionHolder);
        }
        else
        {
            //get the connection from the holder
            tempConn = connectionHolder[base.ConnectionString] as OracleConnection;
            if ( tempConn == null )
            { //if the connection was not in the holder
                //create a new connection
                tempConn = new OracleConnection(base.ConnectionString);
                //add the connection to the holder
                connectionHolder.Add(base.ConnectionString, tempConn);
            }
            else
            { //if the connection exists
                if ( renew )
                { //if should renew the connection

                    //TODO:Delete
                    HealthModel.Trace.TraceToken token = HealthModel.Trace.TraceHelper.Start( "+++++++ R e n e w", "", "", 0 );


                    //closes the connection
                    tempConn.Close();
                    //create a new connection
                    tempConn = new OracleConnection(base.ConnectionString);
                    //add the connection to the holder
                    connectionHolder[base.ConnectionString] = tempConn;

                    //TODO:Delete
                    HealthModel.Trace.TraceHelper.End( token );
                }
            }
        }
    }
    else
    {//if the mark was not in the call context
        //create a connection
        tempConn = new OracleConnection(base.ConnectionString);
    }
    return tempConn;
}

我希望你能帮助我。 问候 胡安·巴勃罗。

【问题讨论】:

  • 能否请您显示您初始化和打开连接的完整代码?
  • 我刚刚添加了请求的代码。非常感谢 Wernfried!
  • 什么是GetConnection()?我假设它返回导致错误的null
  • 我刚刚添加了GetConnection的代码,你可以查看它。

标签: c# .net oracle oracle10g c#-2.0


【解决方案1】:

为什么要使用IDbConnection 接口?

试试这个:

string connectString = "Data Source=dbora1;Max Pool Size=50;Min Pool Size=1;Connection Lifetime=120;Enlist=true;User Id=slu;Password=slu_4d332";

OracleConnection connection = new OracleConnection(connectString);
connection.Open();

【讨论】:

  • 我只是将代码更改为不使用界面,但错误仍然存​​在。 :(
【解决方案2】:

维弗里德

我只是将代码更改为不使用界面,但错误仍然存​​在。

    protected override IDbConnection OpenConnection()
    {
        string cs = "Data Source=dbora1;Max Pool Size=50;Min Pool Size=1;Connection Lifetime=120;Enlist=true;User Id=slu;Password=slu_4d332";;
        OracleConnection connection = new OracleConnection(cs);
        try{
        connection.Open();
            }catch(Exception ex)
            {                   
                ex.ToString();
            }

        return connection;
    }

    #endregion
}

Deputy exception image 堆栈跟踪:

StackTrace  "   at Oracle.DataAccess.Client.OracleException.HandleErrorHelper(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, OpoSqlValCtx* pOpoSqlValCtx, Object src, String procedure)\r\n   at Oracle.DataAccess.Client.OracleException.HandleError(Int32 errCode, OracleConnection conn, IntPtr opsErrCtx, Object src)\r\n   at Oracle.DataAccess.Client.OracleConnection.Open()\r\n   at Oracle.DataAccess.Client.OracleConnection.Open()\r\n   at Microsoft.Practices.EnterpriseLibrary.Data.Oracle.OracleDatabase.OpenConnection() in c:\\v\\enterpriselibrary\\front_end\\fuentes\\data\\oracle\\oracledatabase.cs:line 441"    string

感谢您的宝贵时间

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多