Search code examples
c#oracledatabase-connectionoraclecommand

System.ObjectDisposedException: Cannot access a disposed object. Object name: 'OracleConnection' Reuse of OracleConnection object


I would to reuse a OracleConnection object for more queries so I wrote a simple class:

public static class DbConnectionsManager
    {
        /// <summary>
        /// 
        /// </summary>
        private static OracleConnection _dbConnection = null;


        /// <summary>
        /// 
        /// </summary>
        /// <param name="aConnectionString"></param>
        /// <returns></returns>
        public static OracleConnection GetDatabaseConnection(string aConnectionString) 
        {
            try
            {
                if (_dbConnection == null)
                {
                    _dbConnection = new OracleConnection(aConnectionString);
                    _dbConnection.Open();
                    return _dbConnection;
                }

                if (_dbConnection.State == System.Data.ConnectionState.Closed)
                {
                    _dbConnection.ConnectionString = aConnectionString;
                    _dbConnection.Open();
                    return _dbConnection;
                }

                if (!_dbConnection.ConnectionString.Equals(aConnectionString))
                {
                    _dbConnection.Close();
                    _dbConnection.ConnectionString = aConnectionString;
                    _dbConnection.Open();
                    return _dbConnection;
                }

                return null;
            }
            catch (Exception e)
            {

                return null;
            }
        }
    }

in this way I can use the connection several times:

  using (OracleConnection connection = 
           DbConnectionsManager.GetDatabaseConnection(aDbConnectionString))
            {
                OracleCommand command = connection.CreateCommand();
                string sql = "SELECT * FROM MYTABLE";
                command.CommandText = sql;

                OracleDataReader reader = command.ExecuteReader();
                while (reader.Read())
                {
                    string myField = (string)reader["EXAMPLE"];
                    Console.WriteLine(myField);
                }
            }

When I call the method the first time everything works fine. If I recall the method the static object is != null but the connection result closed! I never close the connection!

When you try to reopen the connection I have this exception

....
if (_dbConnection.State == System.Data.ConnectionState.Closed)
                {
                    _dbConnection.ConnectionString = aConnectionString;
                    _dbConnection.Open();
                    return _dbConnection;
                }
...

Error

Message = "Cannot access a disposed object.\r\nObject name: 'OracleConnection'."

Solution

  • As the error says it's a disposed object. That means you need to remove the using ( clause; this clause disposed your connection object and that's why you cannot use this object outside using (. That means you need to create a new object of class if you want to use it outside using (.

    See: C# Using Statement