sapdev logo background
sapdev logo sapdev logo
Comments

Manage and delete SAP sessions using ABAP code




Deleting and managing SAP sessions is fairly straight forward using a few standard function modules, all of which begin with TH_. The specific ones I am going to discuses here are TH_USER_INFO, TH_USER_LIST, TH_DELETE_USER.

TH_USER_INFO
TH_USER_INFO basically gets information about the current active session i.e. the session where this code is being run. It returns information such as the host address, the terminal, No. of active session, No. of max sessions, tid of session etc etc. It is the TID along with user name which is required to be able to close a SAP session.

data: ld_tid type i.
*Get current session info
 CALL FUNCTION 'TH_USER_INFO'
  EXPORTING
    CLIENT                    = sy-mandt
    USER                      = sy-uname
*    CHECK_GUI                 = 0
  IMPORTING
*    HOSTADDR                  =
*    TERMINAL                  =
*    ACT_SESSIONS              =
*    MAX_SESSIONS              =
*    MY_SESSION                =
*    MY_INTERNAL_SESSION       =
*    TASK_STATE                =
*    UPDATE_REC_EXIST          =
    TID                       = ld_tid
*    GUI_CHECK_FAILED          =
*    ADDRSTR                   =
*    RC                        =
           .


TH_USER_LIST - Get list of sessions based on TID
TH_USER_LIST returns a list of all the active session currently running on the SAP system within the current client. The resultant table contains information such as user name, transaction code, time, date, TID etc which you can use to choose the session you want. The only problem with this list is that for certain session types such as 'GUI' you can have many actual SAP sessions under one TID i.e. if you have 5 sap tcodes running these would be within a single TID.

Hopfully this image of SM04 will help explain things better. Although there is only 3 entries which each has a unique TID value there is infact 7 active sessions, 5 'GUI' and 2 'Plugin HTTPS'. The 2 HTTPS ones have there own entry in the table and there own TID but the gui ones are grouped together in under one entry/TID value.

data:  it_LIST type STANDARD TABLE OF UINFO.
*Get list of sessions
 CALL FUNCTION 'TH_USER_LIST'
   TABLES
     LIST                = it_list
*    USRLIST             =
*  EXCEPTIONS
*    AUTH_MISSSING       = 1
*    OTHERS              = 2
           .

Based on the SM04 session screen shown above it_list will conatin the following data



TH_LONG_USR_INFO - Gets all Users sessions
Gets list of all user sessions ignoring TID grouping

CALL FUNCTION 'TH_LONG_USR_INFO'
 EXPORTING
   USER            = 'ISSMJL'
  TABLES
    USER_INFO       = it_userinfo


Based on the SM04 session screen shown above it_list will conatin the following data


TH_DELETE_USER
TH_DELETE_USER is the function module that actually deletes a SAP session. You simple pass the desired user name and TID to the fm and it will be deleted.

data: ld_tid type i.
*Delete SAP session
CALL FUNCTION 'TH_DELETE_USER'
  EXPORTING
    USER                   = sy-uname
    CLIENT                 = sy-mandt
*   ONLY_POOLED_USER       = ' '
   TID                     = ld_tid
 EXCEPTIONS
   AUTHORITY_ERROR        = 1
   OTHERS                 = 2
 



comments powered by Disqus