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
 

Related Articles

Beginners Guide to learning SAP development starting with logging into SAP
ABAP Programming EVENTS in SAP
ABAP Function Module basics in SAP
DATA and @DATA Inline ABAP declarations available from release 7.40 to help make your code cleaner and more readable
ABAP Workbench Programming Techniques - BC402
ABAP rules to consider before creating a bespoke abap report or program
ABAP Subroutine basics in SAP
Get access to an SAP system for individual needs
SAP Base 64 encoding and decoding using ABAP code
Call web URl from ABAP report or SAP help documentation
Direct download, downloading sap objects
SAP Icons
SAP icons and some ABAP code to display them
SAP icons list and their associated codes
Internal Program Environment diplays all internal/external objects and operations used by an SAP program
minisap installation on your local pc to allow ABAP development, your own local SE80
SAP minisap installation on your local pc to allow ABAP development for free
SAP module based information such FI, HR, MM, PM, BW etc
Need an SAP ABAP program created?
SAP repository objects - List of useful standard and bespoke SAP repository objects
Retrieve SAP objects using transport entry in SE10 to restore objects that have been deleted
SAP Help for all areas for SAP ABAP Development inc ABAP, ALV, Web dynpro, bsp, HR, BW
ABAP tutorial - Logging into an SAP system for the first time
SAP module areas
Increase & Decrease size of SAP editor text
ABAP development information and code examples for producing be-spoke SAP functionality
ABAP Development Info - Example code and information on various areas of ABAP development
SAP command field entries - box in top left corner
Force new page when printing abap source code
ABAP FIELD SYMBOL - Techniques for manupulating data using the FIELD-SYMBOL statement
Hiding ABAP Source Code so that it can not be viewed by anyone
ABAP Internal table declaration - Various methods of creating internal data structures and tables
Parameter ID - ABAP code to demonstrate how to set and get a parameter ID
RANGE statement - Example ABAP code to demonstrate the RANGE command
Change SAP logo in top right hand corner of SAP client
ABAP SELECT statement within SAP - Example ABAP code to demonstrate the SELECT command
Create desktop Shortcut to SAP function
SAP Note assistant - Using transaction SNOTE to read and apply OSS note fix
VARYING command - Example ABAP code to demonstrate the VARYING command
Creating your first helloworld ABAP report in SAP