SQL SERVER real-time access to online user list

 When we develop the C/S management system, we often need to restrict user logins, such as single login with the same user name or single login with the same IP. The key to solving such problems is to obtain a list of real online users. Due to various reasons (such as dropped calls, network failures, crashes, etc.), the real-time online user list is difficult to obtain. We have to refresh the online user data table regularly. Users who do not refresh the data within the specified time are regarded as offline users. This method better solves the problem of obtaining the online user list, but there are two problems: 1. The real-time performance is poor, and 2. additional resources are required for maintenance.
    We know that the system table MASTER..SYSPROCESSES records some user login information. The program_name field in the table is the program name of the login management system. The content of this field has no substantive significance to most management systems. Article, more real-time access to the list of online users?
   The answer is yes. First, our management system generates a GUID at startup and converts it into a string, assuming: LinkGuid, we use the following format when writing the link string of SQL SERVER:
       'Provider=SQLOLEDB.1;Password =' + Password
        + ';Persist Security Info=True;User ID=' + USERNAME + ';Initial Catalog=' + databasename
        + ';Data Source=' + servername +';Application Name='+LinkGuid;
    Once the system is managed After successful login, we can find the login link information whose program_name field is LinkGuid string from the system table MASTER..SYSPROCESSES.
    After the login is successful, we only need to insert the login information into the data table defined by ourselves, assuming: [Data Security], the fields of the table include LinkGuid, Username, UserIp and other information, this table only needs to insert data, no need after login. maintain. Then, we can use the following SQL statement to check the current online user list at any time:
    
  select LinkGuid,Username,UserIp from [Data Security] where exists(select net_address from MASTER..SYSPROCESSES where program_name=LinkGuid)

    After a lot of tests, the real-time performance of the above-mentioned online user list is greater than that of the regular online user list that needs to be refreshed regularly, and the practicability is high, and the data does not need to be maintained regularly.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324851396&siteId=291194637