Details of JDBC resource release

1. In the following example of closing the resource ( complete example ) if(conn != null) is not used to judge whether the resource has been closed.

Rather, it is used to avoid null pointer exceptions.

Reason explanation:

At the beginning of JDBC:

It defines two null values. If it is executed down, an exception will be thrown before the two variables are assigned to them. At this time, the program will catch a field and execute the statement in finally, but this time set, state, conn All are empty, and calling methods on empty objects will throw nullpointexception, and the whole program will crash at this time. So it must be empty before closing.

 

Second, what does the close method do?

What the java.sql.Connection.close() method does is to immediately release the database connection resources occupied by the connection object ( disconnect the network connection, close io ).

Not as I thought the close method would simply set the conn object to null. In fact, after calling close(), conn is still not null.

In fact, the relatively complete finally writing and sending both call close() and empty the object.

package com.javy.jdbc;  
  
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.sql.SQLException;  
import java.sql.Statement;  
  
import com.mysql.jdbc.Driver;  
  
public class JdbcDemo1 {  
  
    public static void main(String[] args) {  
        Connection conn = null;  
        Statement state = null;  
        ResultSet set = null;  
        try {  
            // 注册数据库驱动  
            DriverManager.registerDriver(new Driver());  
            Class.forName("com.mysql.jdbc.Driver");  
            // 获取数据库连接  
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "");  
            // 获取传输器对象  
            state = conn.createStatement();  
            // 利用传输器对象执行sql语句  
            set = state.executeQuery("select * from emp");  
            while (set.next()) {  
                System.out.println(set.getString("name"));  
            }  
        } catch (SQLException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } catch (ClassNotFoundException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        } finally {  
            try {  
                conn.close();  
  
            } catch (SQLException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            } finally {  
                conn = null;  
            }  
            try {  
                set.close();  
  
            } catch (SQLException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            } finally {  
                set = null;  
            }  
  
            try {  
                state.close();  
  
            } catch (SQLException e) {  
                // TODO Auto-generated catch block  
                e.printStackTrace();  
            } finally {  
                state = null;  
            }  
  
        }  
    }  
  
}  

for the code above. The reason each close() is try catch on closing is that an exception may occur on closing, so the rest can't be closed. So every one has to be captured.

 

3. The closing of ResultSet, Statement and Connection has such a relationship: closing a Statement will close all its ResultSets, and closing a Connection will close all its Statements.

But it is recommended to close ResultSet-->Statement-->Connection from the small

 

 

For more details, please refer to the article: JDBC: Details of JDBC Resource Release

                                            What does the close method of java.sql.Connection do (taking MySQL as an example)

Guess you like

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