# Disconnect From The TiDB Server Instance
+ use **`Connection.close()`**  
```
...
finally{
            if(connection != null){
                try{
                    // Release the resources in cascade
                    ~!@connection.close()@!~;
                    System.out.println("Connection closed");
                }
                catch(Exception e){
                    System.out.println("Error at disconnecting: "+e);
                }
            }
        }
```

------------------------------------------------------------
Class demo scripts:

For macOS and Linux: https://github.com/pingcap/tidb-course-201-lab/blob/master/scripts/09-demo-jdbc-connection-01-show.sh

For Windows: https://github.com/pingcap/tidb-course-201-lab/blob/master/scripts/09-demo-jdbc-connection-01-show.bat


When your program has finished using the TiDB server, terminate the connection. As a best practice, to free up resources immediately, you must explicitly close the connection.

To disconnect from the TiDB server, use the Connection object's close() method.

The code in the example in the slide continues from the code in the previous slide. It attempts to disconnect from the TiDB server in the finally{} block. The code in finally{} block executes regardless of whether the code used to connect to the database in the previous slide succeeded or not. close() method relies on the connection object, so, firstly we make sure there is a valid Connection object, if connection not equals to null.

If there is no instance of connection, the program ends. If there is an instance of connection, then it attempts to call the connection object's close() method. If that succeeds, the program reports that the connection has been closed. If it fails to close  the connection for any reason, then the program executes the code in the catch{} block, which reports the error to the user before exiting.

```
...
finally{
            if(connection != null){
                try{
                    // Release the resources in cascade
                    connection.close();
                    System.out.println("Connection closed");
                }
                catch(Exception e){
                    System.out.println("Error disconnecting: "+e.toString());
                }
            }
        }
```
