Language/Java

jdbc connection max check code

아르비스 2016. 5. 31. 14:24

jdbc 설정 이외에 직접 connection 수를 증가하면서,

connection 가능 여부를 판단 할 수 있는 code


간단하다.



import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

import java.sql.Statement;


import org.junit.Test;


public class JdbcConnectionTest {

@Test

public void connectTest() throws ClassNotFoundException, SQLException {

int connLimit = 2000;


        Connection[] conn = new Connection[connLimit];

        Class.forName("com.mysql.jdbc.Driver");


        // Get Connection

        for(int i = 0; i < connLimit; i++){

            System.out.println(i);

            conn[i] = DriverManager.getConnection("jdbc:mysql://192.168.0.1:3306/metadb?useUnicode=true&useServerPrepStmts=true&characterEncoding=UTF-8&autoReconnect=true","root","key");

        }


        // To Keep Java Program - no exit

        Statement stmt = conn[0].createStatement();

        for(int i = 0;;i++){

            stmt.execute("select sleep(5)");

            System.out.println(i+"th!!");

        }

        

}


}