日期:2014-05-20  浏览次数:20912 次

测试oracle连接池最大连接数,这样有什么问题吗?测试中一直没报错。
Java code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBC_Test {
    public static void main(String args[]) {

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;

        try {
            String driverName = "oracle.jdbc.driver.OracleDriver";

            Class.forName(driverName);

            String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
                for (int i = 0; i < 1000; i++) {
                System.out.println("连接到oracl成功!----"+i++);
                connection= DriverManager.getConnection(url, "bschis", "bschis");
                statement= connection.createStatement();
                statement.executeQuery("select * from EHR_HealthRecord");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
    }
}


但是如果换成数组的,就ok了!如下:
Java code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBC_Test {
    public static void main(String args[]) {

        Connection[] connection = new Connection[1000];
        Statement[] statement = new Statement[1000];
        ResultSet resultSet = null;

        try {
            String driverName = "oracle.jdbc.driver.OracleDriver";

            Class.forName(driverName);

            String url = "jdbc:oracle:thin:@127.0.0.1:1521:orcl";
                for (int i = 0; i < 1000; i++) {
                System.out.println("连接到oracl成功!----"+i++);
                connection[i] = DriverManager.getConnection(url, "bschis", "bschis");
                statement[i] = connection[i].createStatement();
                statement[i].executeQuery("select * from EHR_HealthRecord");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                    }
    }
}


这是为什么呢?不知问题在哪里?

补充:
对于hibernate 如何获得当前连接数呢?


------解决方案--------------------
探讨
Java code
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBC_Test {
public static vo……

------解决方案--------------------
LZ可以好好学习下java的垃圾回收机制