JiekerTime opened a new issue #11341:
URL: https://github.com/apache/shardingsphere/issues/11341


   ### Which version of ShardingSphere did you use?
   
   5.0.0-RC1-SNAPSHOT
   
   ### Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?
   
   ShardingSphere-JDBC 
   
   ### Expected behavior
   
   Number of database related connections is 20:
   MySQL:
   
   
![image](https://user-images.githubusercontent.com/76552510/125743583-036a78cc-ae0a-47b4-b29a-b12b00cb6478.png)
   
   PG:
   
   
![image](https://user-images.githubusercontent.com/76552510/125743740-9937fbd8-cb4c-471c-a11e-26887c5324fe.png)
   
   ### Actual behavior
   
   Number of database related connections is 30:
   
   
![image](https://user-images.githubusercontent.com/76552510/125743509-90ae5596-5815-4085-ab04-829f23d7cdba.png)
   
   PG:
   
   
![image](https://user-images.githubusercontent.com/76552510/125743797-951c6876-ff0f-4bb8-a6af-7031e6f5b2ef.png)
   
   ### Reason analyze (If you can)
   
   There is a problem with the processing of the number of connections in SS.
   
   ### Steps to reproduce the behavior, such as: SQL to execute, sharding rule 
configuration, when exception occur etc.
   
   
   **Use HikariConfig as the connection pool and set MaximumPoolSize to 2.**
   
   Execute the following code review:
   
   PG:
   
       public DataSource getDataSource(String db, Integer id) {
           HikariConfig config = new HikariConfig();
           config.setUsername("root");
           config.setPassword("root");
           config.setDriverClassName("org.postgresql.Driver");
           config.setJdbcUrl("jdbc:postgresql://127.0.0.1:5432/" + db + 
"?ApplicationName=hikari-" + id);
           config.setMaximumPoolSize(2);
           config.setTransactionIsolation("TRANSACTION_READ_COMMITTED");
           return new HikariDataSource(config);
       }
   
       public Runnable getThread(int i) {
           return () -> {
               DataSource dataSource = getDataSource("db_" + i, i);
               for (int j = 0; j < 100; j++) {
                   try (Connection connection = dataSource.getConnection()) {
                       System.out.println(connection.getClientInfo());
                       Thread.sleep(100);
                   } catch (SQLException | InterruptedException ignored) {
                   }
               }
           };
       }
   
       public Runnable getThread(DataSource dataSource) {
           return () -> {
               for (int j = 0; j < 100; j++) {
                   try (Connection connection = dataSource.getConnection()) {
                       System.out.println(connection.getClientInfo());
                       Thread.sleep(100);
                   } catch (SQLException | InterruptedException ignored) {
                   }
               }
           };
       }
   
       public Runnable getThread(DataSource dataSource, String name) {
           return () -> {
               try (Connection connection = dataSource.getConnection()) {
                   
System.out.println(connection.createStatement().execute("SELECT 1"));
                   Thread.sleep(100);
               } catch (SQLException | InterruptedException ignored) {
               }
           };
       }
   
       @Test
       public void commonConnectionTest() throws InterruptedException {
           for (int i = 0; i < 10; i++) {
               new Thread(getThread(i)).start();
           }
           Thread.sleep(100000);
       }
   
       @Test
       public void natureConnectionTest() throws InterruptedException, 
SQLException {
           Map<String, DataSource> dataSourceMap = new HashMap<String, 
DataSource>() {
               {
                   put("db_0", getDataSource("db_0", 0));
                   put("db_1", getDataSource("db_1", 1));
                   put("db_2", getDataSource("db_2", 2));
                   put("db_3", getDataSource("db_3", 3));
                   put("db_4", getDataSource("db_4", 4));
                   put("db_5", getDataSource("db_5", 5));
                   put("db_6", getDataSource("db_6", 6));
                   put("db_7", getDataSource("db_7", 7));
                   put("db_8", getDataSource("db_8", 8));
                   put("db_9", getDataSource("db_9", 9));
               }
           };
           for (String key : dataSourceMap.keySet()) {
               new Thread(getThread(dataSourceMap.get(key))).start();
           }
           Thread.sleep(100000);
       }
   
       @Test
       public void ssConnectionTest() throws InterruptedException, IOException, 
SQLException {
           Map<String, DataSource> dataSourceMap = new HashMap<String, 
DataSource>() {
               {
                   put("db_0", getDataSource("db_0", 0));
                   put("db_1", getDataSource("db_1", 1));
                   put("db_2", getDataSource("db_2", 2));
                   put("db_3", getDataSource("db_3", 3));
                   put("db_4", getDataSource("db_4", 4));
                   put("db_5", getDataSource("db_5", 5));
                   put("db_6", getDataSource("db_6", 6));
                   put("db_7", getDataSource("db_7", 7));
                   put("db_8", getDataSource("db_8", 8));
                   put("db_9", getDataSource("db_9", 9));
               }
           };
           DataSource dataSource = 
YamlShardingSphereDataSourceFactory.createDataSource(dataSourceMap, new 
File("E:\\ideaproject\\procject_for_test\\shardingsphere\\src\\test\\resources\\sharding-databases.yaml"));
           for (int i = 0; i < 100; i++) {
               new Thread(getThread(dataSource, "thread-1"), 
"thread-1").start();
           }
           System.out.println("finished!---------------");
           Thread.sleep(100000);
       }  
   
   MySQL:
   
       public DataSource getDataSource(String db, Integer id) {
           HikariConfig config = new HikariConfig();
           config.setUsername("root");
           config.setPassword("123456");
           config.setDriverClassName("com.mysql.jdbc.Driver");
           config.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/" + db + 
"?useServerPrepStmts=true&serverTimezone=UTC&useSSL=false&useLocalSessionState=true&characterEncoding=utf-8");
           config.setMaximumPoolSize(2);
           config.setTransactionIsolation("TRANSACTION_READ_COMMITTED");
           return new HikariDataSource(config);
       }
   
       public Runnable getThread(int i) {
           return () -> {
               DataSource dataSource = getDataSource("db_" + i, i);
               for (int j = 0; j < 100; j++) {
                   try (Connection connection = dataSource.getConnection()) {
                       System.out.println(connection.getClientInfo());
                       Thread.sleep(100);
                   } catch (SQLException | InterruptedException ignored) {
   
                   }
               }
           };
       }
   
       public Runnable getThread(DataSource dataSource) {
           return () -> {
               for (int j = 0; j < 100; j++) {
                   try (Connection connection = dataSource.getConnection()) {
                       System.out.println(connection.getClientInfo());
                       Thread.sleep(100);
                   } catch (SQLException | InterruptedException ignored) {
   
                   }
               }
           };
       }
   
       public Runnable getThread(DataSource dataSource, String name) {
           return () -> {
               try (Connection connection = dataSource.getConnection()) {
                   
System.out.println(connection.createStatement().execute("SELECT 1"));
                   Thread.sleep(100);
               } catch (SQLException | InterruptedException ignored) {
                   ignored.printStackTrace();
               }
           };
       }
   
       @Test
       public void commonConnectionTest() throws InterruptedException {
           for (int i = 0; i < 10; i++) {
               new Thread(getThread(i)).start();
           }
           Thread.sleep(100000);
       }
   
       @Test
       public void ssSplitConnectionTest() throws InterruptedException, 
IOException, SQLException {
           Map<String, DataSource> dataSourceMap = new HashMap<String, 
DataSource>() {
               {
                   put("db_0", getDataSource("db_0", 0));
                   put("db_1", getDataSource("db_1", 1));
                   put("db_2", getDataSource("db_2", 2));
                   put("db_3", getDataSource("db_3", 3));
                   put("db_4", getDataSource("db_4", 4));
                   put("db_5", getDataSource("db_5", 5));
                   put("db_6", getDataSource("db_6", 6));
                   put("db_7", getDataSource("db_7", 7));
                   put("db_8", getDataSource("db_8", 8));
                   put("db_9", getDataSource("db_9", 9));
               }
           };
           for (String key : dataSourceMap.keySet()) {
               new Thread(getThread(dataSourceMap.get(key))).start();
           }
           Thread.sleep(100000);
       }
   
       @Test
       public void ssConnectionTest() throws InterruptedException, IOException, 
SQLException {
           Map<String, DataSource> dataSourceMap = new HashMap<String, 
DataSource>() {
               {
                   put("db_0", getDataSource("db_0", 0));
                   put("db_1", getDataSource("db_1", 1));
                   put("db_2", getDataSource("db_2", 2));
                   put("db_3", getDataSource("db_3", 3));
                   put("db_4", getDataSource("db_4", 4));
                   put("db_5", getDataSource("db_5", 5));
                   put("db_6", getDataSource("db_6", 6));
                   put("db_7", getDataSource("db_7", 7));
                   put("db_8", getDataSource("db_8", 8));
                   put("db_9", getDataSource("db_9", 9));
               }
           };
           DataSource dataSource = 
YamlShardingSphereDataSourceFactory.createDataSource(dataSourceMap, new 
File("E:\\ideaproject\\procject_for_test\\shardingsphere\\src\\test\\resources\\sharding-databases.yaml"));
           for (int i = 0; i < 100; i++) {
               new Thread(getThread(dataSource, "thread-1"), 
"thread-1").start();
           }
           System.out.println("finished!---------------");
           Thread.sleep(100000);
       }
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to