In daily database use, it is often necessary to open some connections to perform various operations. However, sometimes when we are done, we need to close these connections to release resources or for security reasons. For Oracle databases, this article explains how to close a connection.
Before closing the connection, we need to check which connections are currently open. You can obtain the detailed information of the current connection through the following SQL query statement:
SELECT sid, serial#, username, program, machine, osuser, status FROM v$session;
The above query statement can display all active connection information in the current database instance, and you can obtain the Session ID, Serial#, Information such as user name, program name, machine name, operating system user name, and connection status. Among them, Session ID and Serial# are the parameters we need to use later.
For Oracle database, there are two ways to close the connection: disconnect the session and kill the process. We will introduce how to use these two methods to close the connection next.
2.1 Disconnect the session
In Oracle, we can use the following SQL statement to disconnect a session:
ALTER SYSTEM KILL SESSION '<sid>,<serial#>';
Among them, "a17fd977faf59de1b60b3f6b0a972677" and " 7071febd2b84dadd39bbccd3eef22761" are the Session ID and Serial# of the session to be closed respectively, which can be obtained through the query statement mentioned earlier.
It should be noted that this method only disconnects the connection between the client and the server, and the resources occupied by the session are not released immediately. However, if the client does not connect again within a period of time after disconnection, the resources of the session will be released.
2.2 Kill the process
If you need to immediately release the resources occupied by the session, we can use the following SQL statement to kill the process:
ALTER SYSTEM KILL SESSION '<sid>,<serial#>' IMMEDIATE;
This method is similar to Compared with the method of disconnecting the session, the resources occupied by the session will be released immediately. However, it should be noted that this method is relatively violent and may cause data loss or other abnormalities, so it is recommended to use it with caution.
Before closing the connection, you need to pay attention to the following points:
In short, closing connections is a very important function in managing databases. Correct use and avoiding misoperations can ensure the stability and security of the database. I hope the methods introduced in this article will be helpful to readers.
The above is the detailed content of oracle closes connection. For more information, please follow other related articles on the PHP Chinese website!