測試 mysql 的最大連線數

guile發表於2019-03-03

測試 mysql 的最大連線數。

blog.csdn.net/shiyangxt/article/details/83264544  --- 原文連結

 

如果直接用 navicat 查詢 mysql 的連線數,查詢語句是 show status like 'Threads%';

其中的 Threads_connected 就是 mysql 的連線數

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

// 測試 mysql 的最大連線數
public class JdbcMaxTest{
	// main
	public static void main(String[] a){
		int count = 1;

		int max = 5000;
		Connection[] conn = new Connection[max];
		Statement[] stmt = new Statement[max];
		ResultSet[] rs = new ResultSet[max];

		String dbUrl = "jdbc:mysql://localhost:3306/testDB1";
		String name = "admin";
		String pwd = "123";

		try{
			Class.forName("com.mysql.jdbc.Driver").newInstance();

			for (count = 1; count < max; count++){
				conn[count] = DriverManager.getConnection(dbUrl, name, pwd);
				stmt[count] = conn[count].createStatement();
				rs[count] = stmt[count].executeQuery("SELECT * FROM user");

				while (rs[count].next()){
					//System.out.println(rs.getString(1) + " t" + rs.getString(2));
				}

				System.out.print(count + "t ");

				if(count % 20 == 0 && count > 0){
					System.out.print("\n");
				}
			}

		}catch(Exception ex){
			System.out.println("\n" + ex.toString());

			//System.out.println(count);
		} finally {
			try {
				System.out.println("\n" + "System has opened " + --count
						+ " Mysql connections.");

				System.out.println("Press Enter key to close them.");

				System.in.read();

				System.out.println("Close these Connections:");

				for(; count >= 0; count--){
					if(rs[count] != null){
						rs[count].close();
					}

					if(rs[count] != null){
						rs[count].close();
					}

					if(conn[count] != null){
						conn[count].close();

						System.out.print(count + "t ") ;

						if(count % 20 == 0 && count > 0){
							System.out.print("\n");
						}
					}
				}
			}catch(Exception ex){
				System.out.println("\n" + "Close connection exception: " + ex.toString());
			}
		}
	}
}


 

 

相關文章