首页 > 解决方案 > oracle jdbc瘦连接中连接空闲超过5分钟时连接关闭

问题描述

此代码片段执行查询,然后程序休眠 5 分钟,然后为 isValid 方法返回 false。使用 jdbc 瘦客户端。db 上的 max_idle_time 设置为 0。对于本地计算机数据库实例,连接不会关闭。所以我猜,它一定和我的网络有关。任何帮助将不胜感激。

package com.oracle.jdbc.test;
import java.sql.*;  
public class JDBCTest {

    public static void main(String args[]){  
    try{  
    //step1 load the driver class  
    Class.forName("oracle.jdbc.driver.OracleDriver");  

    //step2 create  the connection object  
    Connection con=DriverManager.getConnection(  
    "jdbc:oracle:thin:@****","****","****");  

    //step3 create the statement object  
    Statement stmt=con.createStatement();  

    //step4 execute query  
    ResultSet rs=stmt.executeQuery("select 1 from dual");  
    while(rs.next())  
    System.out.println(rs.getInt(1));  

    //step5 close the connection object  
    Thread.sleep(300000);
    System.out.println(con.isValid(1));
    stmt.executeQuery("select 1 from dual");

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

    }  
}

enter code here

标签: javadatabaseoraclejdbcdatabase-connection

解决方案


推荐阅读