标签:style blog http io color ar os sp java
DriverManager.getLoginTimeout():0
jdbc Connection默认是autoCommit:true
Statement查询超时时间默认是0:无限制
遍历ResultSet时Connection不能关闭
public static void setLoginTimeout(int seconds)
seconds
- the login time limit in seconds; zero means there is no limitgetLoginTimeout()
void setQueryTimeout(int seconds) throws SQLException
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).
seconds
- the new query timeout limit in seconds; zero means there is no limitSQLException
- if a database access error occurs, this method is called on a closed Statement
or the condition seconds >= 0 is not satisfiedgetQueryTimeout()
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
标签:style blog http io color ar os sp java
原文地址:http://www.cnblogs.com/softidea/p/4096684.html