JDBC连接数据库 超时问题

来源:互联网 发布:ios 高仿今日头条源码 编辑:程序博客网 时间:2024/05/01 15:13
JDBC连接数据库,同样是基于SOCKET连接,至于说为什么要设置超时时间,想必你懂得。OK,没时间废话,直接上代码

      Properties info = new Properties();
      //连接超时时间,单位 毫秒ms 
      info.put("oracle.net.CONNECT_TIMEOUT", 6*1000);
      //读写超时时间,单位 毫秒ms 
      info.put("oracle.jdbc.ReadTimeout", 6*1000);
      info.put("user",user);  
      info.put("password",password);  

      Connection con = DriverManager.getConnection(dburl , info ); 
其他数据库类型,比如mysql,可以通过连接URL设置,
jdbc:mysql://xxx.xx.xxx.xxx:3306/database?connectTimeout=60000&socketTimeout=60000(单位 ms)
MS-SQL: 
jdbc:jtds:sqlserver://server:port/database;loginTimeout=60;socketTimeout=60(单位是s)

参考URL  :
http://www.importnew.com/2466.html
原创粉丝点击