JDBC

DriverManager.getLoginTimeout():0
jdbc Connection默认是autoCommit:true
Statement查询超时时间默认是0:无限制
遍历ResultSet时Connection不能关闭

java.sql
Class DriverManager

setLoginTimeout

public static void setLoginTimeout(int seconds)
Sets the maximum time in seconds that a driver will wait while attempting to connect to a database.

 

Parameters:
seconds - the login time limit in seconds; zero means there is no limit
See Also:
getLoginTimeout()



java.sql
Interface Statement
setQueryTimeout

void setQueryTimeout(int seconds)
                     throws SQLException
Sets the number of seconds the driver will wait for a Statement object to execute to the given number of seconds. If the limit is exceeded, an SQLException is thrown. A JDBC driver must apply this limit to the execute, executeQuery and executeUpdate methods. JDBC driver implementations may also apply this limit to ResultSet methods (consult your driver vendor documentation for details).

 

Parameters:
seconds - the new query timeout limit in seconds; zero means there is no limit
Throws:
SQLException - if a database access error occurs, this method is called on a closed Statement or the condition seconds >= 0 is not satisfied
See Also:
getQueryTimeout()

 

 

 

package oracle.maxconnection;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import oracle.Common;

public class CloseConnectionAfterGetResultset {

    public static void main(String[] args) throws SQLException {

        Connection conn=Common.getConnection();
        System.out.println("conn.getAutoCommit():"+conn.getAutoCommit());
        
        String sql="select * from dba_tables";
        try {
            PreparedStatement  ps=conn.prepareStatement(sql);
            System.out.println("ps.getQueryTimeout():"+ps.getQueryTimeout());
            
            ResultSet rs=ps.executeQuery();
            
            ResultSetMetaData rsm=rs.getMetaData();
            
            while (rs.next()) {

                
                for (int i = 1,count=rsm.getColumnCount(); i <=count; i++) {
                    System.out.println("Column:"+rsm.getColumnLabel(i)+",Value:"+rs.getString(i));
                }
                
                if (conn.isClosed()==false) {
                    conn.close();
                    System.out.println("Connection has closed");
                }
                
            }
            
        } catch (SQLException e) {
            e.printStackTrace();
        }
        
        
        
    }

}

输出:

conn.getAutoCommit():true
ps.getQueryTimeout():0
Column:OWNER,Value:SYS
Column:TABLE_NAME,Value:ICOL$
Column:TABLESPACE_NAME,Value:SYSTEM
Column:CLUSTER_NAME,Value:C_OBJ#
Column:IOT_NAME,Value:null
Column:STATUS,Value:VALID
Column:PCT_FREE,Value:0
关闭Connection后next会报错:
java.sql.SQLException: 关闭的连接: next
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:111)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:145)
    at oracle.jdbc.driver.OracleResultSetImpl.next(OracleResultSetImpl.java:180)
    at oracle.maxconnection.CloseConnectionAfterGetResultset.main(CloseConnectionAfterGetResultset.java:27)

 

 

http://jingyan.baidu.com/article/fc07f98922615a12ffe519ce.html

 

http://www.admin10000.com/document/1360.html

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。