public class TestConnection { public static void main(String[] args) throws ClassNotFoundException, SQLException, InterruptedException { final String urlStg ; final String userStg ; final String passwordStg ; List<Connection> list=new ArrayList<>(); ExecutorService service = Executors.newFixedThreadPool(100); CountDownLatch countDownLatch = new CountDownLatch(10000); for(int i=0;i<10000;i++){ final int a=i; service.execute(new Runnable() { @Override public void run() { try { Class.forName("com.mysql.jdbc.Driver"); Connection connection=null; try{ connection = DriverManager.getConnection(urlStg, userStg, passwordStg); }catch (Exception e){ e.printStackTrace(); } //重连机制 if(connection==null){ connection=reTryConnect(); } Statement statement = connection.createStatement(); statement.execute("SELECT * FROM cebi.kpi_appexec_daily_visitor where dt='20190826' "); //connection.close(); //list.add(connection); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } finally { countDownLatch.countDown(); System.out.println(a); } } }); //statement.close(); // connection.close(); } countDownLatch.await(); System.out.println("结束"); } public static Connection reTryConnect() throws ClassNotFoundException { Class.forName("com.mysql.jdbc.Driver"); final String urlStg ; final String userStg ; final String passwordStg ; Connection connection=null; try{ connection = DriverManager.getConnection(urlStg, userStg, passwordStg); }catch (Exception e){ e.printStackTrace(); } return connection; }
mysql数据库测试
最新推荐文章于 2024-06-17 17:19:07 发布