2013年9月20日星期五

View Yong Chuan Chi podcast JDBC video of the "release connection " temptations

Last look at his video did not feel any problem , then he is not understand.
Today I suddenly remembered this problem, do a little test and found that his approach seems to have errors.
now to ask you next .
 //释放链接  
    public static void free(ResultSet rs, Statement st, Connection conn) {  
        try {  
            if (rs != null)  
                rs.close();  
        } catch (SQLException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                if (st != null)  
                    st.close();  
            } catch (SQLException e) {  
                e.printStackTrace();  
            } finally {  
                if (conn != null)  
                    try {  
                        conn.close();  
                    } catch (SQLException e) {  
                        e.printStackTrace();  
                    }  
                }  
            }  
        }  
    }  
      

This is his source.
http://longdechuanren.iteye.com/blog/632358
This blog is not his own , but the code is based on his video finishing.
I first mysql to set a maximum number of connections .
Then insert () method executes three times , the third time will be the implementation error ( if performed only two times , no error ) , suggesting com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Data source rejected establishment of connection, message from server: "Too many connections"
By the way , why is not performed when the 2nd error ?
public void insert(String name, String passwd) {
try {
conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
ps = conn
.prepareStatement("insert into user(username,password) values(?,?)");
ps.setString(1, name);
ps.setString(2, passwd);
ps.executeUpdate();

} catch (Exception e) {
System.out.println(e.toString());
}
}


Then I took him to free () method inserted in the second insert () method after the execution. My assumption is that has closed the connection , and then perform the third time , the equivalent of opening a new connection to execute the first time , it should not complain . But the results are still being given , too many connections.
=================================
just some time ago someone asked csdn , java exchanged two numbers is what api have it ? http://topic.csdn.net/u/20110307/21/ced90e4d-6d64-4709-b157-5be2d32ea0da.html
So this time, I took his free (ResultSet rs, Statement st, Connection conn) parameters removed, the direct use of free ().
run code run successfully.
Special note: If you are interested , please himself under test .
of two kinds of situations:
insert method of the operation of the database and put the main function of a class ; then run free ( with parameters ) and free ();
insert method of the operation of the database and the main function apart on two classes ; then run free ( with parameters ) and free ().
==============================================
experimental results , Li Yong wrong. Close the connection method in the database , you should not take parameters.
that should be free (), rather than using the free (ResultSet rs, Statement st, Connection conn)

If my experiments I have loopholes or simply wrong, or I and Li Yong are wrong , please Kuangpen and why

------ Solution ------------------------------------ --------
because your mysql installation directory my.ini set in the small number of concurrent links

link the database when you cycle

front not shut it came back

it is "too many connections"

This is the MySQL problem

not that teacher's question

watch program , written quite standardized bar.
------ Solution ---------------------------------------- ----
try {
} catch () {
} Do not complete so many
------ Solution ------------------------------- -------------
Will upstairs on the 7th floor
rs.close () throws it
Neighborhoods ye deal
------ For reference only ------------------------------- --------
of two kinds of situations:
insert method of the operation of the database and put the main function of a class ; then run free ( with parameters ) and free ();
insert method of the operation of the database and the main function apart on two classes ; then run free ( with parameters ) and free ().
================================
In the first case , if the result is successful, but in fact were unsuccessful.
So , would you please put free ( with parameters ) and free () to read as follows : increase = null, and increased print information.
try {
if (rs != null) {
System.out.println(rs);
rs.close();
rs = null;
} else {
System.out.println("rs = null");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (ps != null) {
System.out.println(ps);
ps.close();
ps = null;
} else {
System.out.println("ps = null");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (conn != null) {
System.out.println(conn);
conn.close();
conn = null;
} else {
System.out.println("conn = null");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

At this point, you can clearly see , free ( with parameters ) method performs an unsuccessful it.
------ For reference only -------------------------------------- -
you'd better call the insert method you impress , or really hard to say who is wrong
------ For reference only -------------- -------------------------

you sure you have read the article ?
------ For reference only -------------------------------------- -
no, experimental results , free () can not take parameters, and make up = null truly closed
------ For reference only ---------- -----------------------------
amount if I withdraw the 4th floor . Not set = null also closed
------ For reference only ------------------------------- --------
first question ; code written bad enough , and really thought I was in Gailou ah ;

Direct

try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (SQLException ac) {
ac.printStackTrace();
}

------ For reference only ----------------------------------- ----

I was deliberately set up a small concurrent connections .
just to prove that my free () has been completely release the connection.
------ For reference only -------------------------------------- -

laughed language
------ For reference only -------------------------- -------------
in fact , in the final analysis of this problem is to call java parameter is a value issue.
For example , in your test class called insert () method .
public void insert(String name, String passwd) {
        try {
            conn = DriverManager.getConnection(dbUrl, dbUser, dbPassword);
            ps = conn
                    .prepareStatement("insert into user(username,password) values(?,?)");
            ps.setString(1, name);
            ps.setString(2, passwd);
            ps.executeUpdate();

        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

then you simply need to declare in the test class conn, ps, you simply declare a test class to hold the insert rs returned resultset on the line.
then you call free (ResultSet rs, Statement st, Connection conn); when
Because your current class has no st, conn a statement that the program is not being given ?
So you re in the test class declaration st, conn, then , free (ResultSet rs, Statement st, Connection conn) actually close that you declared in the test class st, conn , rs
------ For reference only ------------------------------------ ---
I can not imagine your ideas , without parameters , then ask your landlord among free method , how to get dao.impl layer rs, pstmt, conn?
------ For reference only ---------------------------------------
I understand. Last year, I really was too young too naive ah
------ For reference only -------------------------- -------------



I also feel do not understand the idea of ​​the landlord . . . My understanding is that as late as possible in order to call early closing only need to pass this parameter . . . I do not know what the landlord said what the test parameters . . . But also feel very rude landlord , at least until you see the video of the talk you teach your people's code , when in his name followed by a teacher can do . . .
------ For reference only -------------------------------------- -
do not understand why you first placed inside the try-catch to determine if-else

public static void free(ResultSet rs,Statement ps,Connection conn){

      if(null!=rs){
           try{
              rs.close();
              rs=null;
           }catch(Exception e){
           }
      }

      if(null!=ps){
           try{
              ps.close();
              ps=null;
           }catch(Exception e){
           }
      }

      if(null!=conn){
           try{
              conn.close();
              conn=null;
           }catch(Exception e){
           }
      }

}

没有评论:

发表评论