oracle / dotnet-db-samples

.NET code samples for Oracle database developers #OracleDotNet
http://otn.oracle.com/dotnet
MIT License
408 stars 190 forks source link

Multiple inactive sessions are created #244

Closed koraycsharp closed 2 years ago

koraycsharp commented 2 years ago

Hello, i have problems with inactive sessions. When i open new connection in connection, 7 inactive sessions are created. My code just like that,

public void Test()
{
    string connStr = @"User Id = system; Password = Oracle123; Data Source = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (Host = LOCALHOST) (Port = 1522)))  (CONNECT_DATA = (SID = TESTSID))); ";

    using (var connection1 = new OracleConnection(connStr))
    {
        connection1.Open();

        using (var connection2 = new OracleConnection(connStr))
        {
             connection2.Open();
             using (OracleCommand command = new("SELECT count(*) FROM \"SCHEMA1\".\"TABLE1\"", connection2))
             {
                 command.ExecuteReader();
             }
             connection2.Close();
        }

        connection1.Close();
    }

}

When command ExecuteReader called, 7 inactive sessions are created. The screenshot of the session browser is below.

image

alexkeh commented 2 years ago

Pooling is on by default. You may be using two connections, but there can be idle connections in the pool. That would be my guess.

koraycsharp commented 2 years ago

Yes, but i dont understand why there are 7 inactive connections instead of 2. Always 7.

When i turn off pooling, all sessions are closed and there is no session as a result of the operation.

alexkeh commented 2 years ago

Connection pool algorithms will create a buffer of idle connections in the pool that will be ready to dispense. Those idle connections will register as inactive sessions in the database and don't go away immediately after a connection is closed. Most algorithms let pools drain idle connections over a longer period of time to prevent in case activity picks up again.

In your scenario, the ODP.NET connection pooling algorithm is creating seven connections. That leaves a buffer of five that can be dispensed when two of the connections are being used.