Tuesday, July 25, 2023

ORA-02391: exceeded simultaneous SESSIONS_PER_USER limit

There are many ways to overcome this issue

Method 1 - Increase parameter
  1. Check Current connection count

  2. SQL> SELECT count(*) as connections,username FROM v$session where username='APP01' GROUP BY username;
    
    CONNECTIONS  USERNAME
    ------------ ---------
    500          APP01
    
  3. Check current configuration of profile

  4. SQL> col username for a12
    col profile for a19
    col limit for a12
    set lines 299
    select a.username,b.PROFILE,b.RESOURCE_NAME,b.limit from dba_users a , dba_profiles b where a.profile=b.profile and b.RESOURCE_NAME='SESSIONS_PER_USER' 
    and a.username='APP01';
    
    USERNAME     PROFILE             RESOURCE_NAME                    LIMIT
    ------------ ------------------- -------------------
    APP01     DEFAULT             SESSIONS_PER_USER                500
    
  5. Increase parameter value

  6. SQL> ALTER PROFILE DEFAULT LIMIT SESSIONS_PER_USER 600;
    
    Profile altered.
    
  7. Makesure parameter has been changed successfully

  8. SQL> col username for a12
    col profile for a19
    col limit for a12
    set lines 299
    select a.username,b.PROFILE,b.RESOURCE_NAME,b.limit from dba_users a , dba_profiles b where a.profile=b.profile and b.RESOURCE_NAME='SESSIONS_PER_USER' and a.username='APP01';
    
    USERNAME     PROFILE             RESOURCE_NAME                    LIMIT
    ------------ ------------------- --------------------
    APP01     DEFAULT             SESSIONS_PER_USER                600
    
Method 2 - Kill Idle sessions based on duration
  1. Check Current connection count

  2. SQL> SELECT count(*) as connections,username FROM v$session where username='APP01' GROUP BY username;
    
    CONNECTIONS  USERNAME
    ------------ ---------
    500          APP01
    
  3. Select Idle connection which are inactive more than 12 hours

  4. set heading off;
    select 'alter system kill session ''' || sid || ',' || serial# || ''' immediate;' from v$session where username='APP01' and status = 'INACTIVE' and last_call_et/60/60 >= 12;
  5. Kill Idle sessions from above output

  6. alter system kill session '100,35175' immediate;
    alter system kill session '1768,53954' immediate;
    alter system kill session '2129,15490' immediate;
    alter system kill session '2251,6956' immediate;
    alter system kill session '3331,17248' immediate;
    ...............
    
  7. Makesure Connection count after killed Idle sessions

  8. SQL> SELECT count(*) as connections,username FROM v$session where username='APP01' GROUP BY username;
    
    CONNECTIONS  USERNAME
    ------------ ---------
    120          APP01
    
Method 3 - Kill sessions which create connection from particular machine/program
  1. Check Current connection count

  2. SQL> SELECT count(*) as connections,username FROM v$session where username='APP01' GROUP BY username;
    
    CONNECTIONS  USERNAME
    ------------ ---------
    500          APP01
    
  3. Check hosts which make multiple sessions

  4. select machine,program,username,count(*)
    from v$session 
    group by machine,program,username
    order by 4 desc;
    
    machine  program  username  count(*)
    -------  -------  --------  --------
    e5a07b31a6e5	JDBC Thin Client	APP01	470
    demo3021132.isaaviation.net	JDBC Thin Client	APP02	185
    
  5. Kill from above output

  6. begin
    for s in(
        select *
        from v$session
        where machine = 'e5a07b31a6e5' and username = 'APP01'
    )
    loop
        begin
        execute immediate 'alter system kill session '''||s.sid||','||s.serial#||''' immediate';
        exception when others then
            null;
        end;
    end loop;
    end;
    
    
  7. Makesure Connection count after killed Idle sessions

  8. SQL> SELECT count(*) as connections,username FROM v$session where username='APP01' GROUP BY username;
    
    CONNECTIONS  USERNAME
    ------------ ---------
    120          APP01