You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2021/11/10 11:17:17 UTC

[GitHub] [shardingsphere] xue2lang opened a new issue #13539: multithreading use one ShardingSphereConnection instance , will cause java.sql.SQLTransientConnectionException: xx Connection is not available, request timed out after 2000ms.

xue2lang opened a new issue #13539:
URL: https://github.com/apache/shardingsphere/issues/13539


   ## Bug Report
   
   **For English only**, other languages will not accept.
   
   Before report a bug, make sure you have:
   
   - Searched open and closed [GitHub issues](https://github.com/apache/shardingsphere/issues).
   - Read documentation: [ShardingSphere Doc](https://shardingsphere.apache.org/document/current/en/overview).
   
   Please pay attention on issues you submitted, because we maybe need more details. 
   If no response anymore and we cannot reproduce it on current information, we will **close it**.
   
   Please answer these questions before submitting your issue. Thanks!
   
   ### Which version of ShardingSphere did you use?
   5.0.0-beta
   ### Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?
   ShardingSphere-JDBC
   ### Expected behavior
   execute sql right
   ### Actual behavior
   ```java
   java.sql.SQLTransientConnectionException: xx Connection is not available, request timed out after 2000ms.
   ```
   ### Reason analyze (If you can)
   ```java
   Class ShardingSphereConnection code :
   
      @Override
       public List<Connection> getConnections(final String dataSourceName, final int connectionSize, final ConnectionMode connectionMode) throws SQLException {
           DataSource dataSource = dataSourceMap.get(dataSourceName);
           Preconditions.checkState(null != dataSource, "Missing the data source name: '%s'", dataSourceName);
           Collection<Connection> connections;
          // **step:1**
           synchronized (getCachedConnections()) {
               connections = getCachedConnections().get(dataSourceName);
           }
           List<Connection> result;
           if (connections.size() >= connectionSize) {
               result = new ArrayList<>(connections).subList(0, connectionSize);
           } else if (!connections.isEmpty()) {
               result = new ArrayList<>(connectionSize);
               result.addAll(connections);
               List<Connection> newConnections = createConnections(dataSourceName, dataSource, connectionSize - connections.size(), connectionMode);
               result.addAll(newConnections);
               synchronized (getCachedConnections()) {
                   getCachedConnections().putAll(dataSourceName, newConnections);
               }
           } else {
         // **step:2**
               result = new ArrayList<>(createConnections(dataSourceName, dataSource, connectionSize, connectionMode));
               synchronized (getCachedConnections()) {
                   getCachedConnections().putAll(dataSourceName, result);
               }
           }
           return result;
       }
   ```
   
   ### Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.
   ```java
   connection pool size  min=max=2
   
   we have thread a、b、c
   - thread a get the cpu control,in step 1, CachedConnection is empty,release cpu control
   - thread b get the cpu control,in step 1, CachedConnection is empty,use execute step2 and cache connection,release cpu control
   - thread c get the cpu control,in step 1, CachedConnection is empty,use execute step2 and cache connection,release cpu control
   - thread a get cpu control ,use execute step2  ,but no datasource can get from datasource connection pool
   ```
   ### Example codes for reproduce this issue (such as a github link).
   ```java
      public void sharding删除查询() throws SQLException, InterruptedException {
   
   
   
           Connection connection = getShardingConnection();
   
           TimeUnit unit = TimeUnit.SECONDS;
           BlockingQueue workQueue = new LinkedBlockingQueue<>(10);
           ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(16, 16, 10, unit, workQueue);
           threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
   
           //删除
           int num = 500;
           CountDownLatch count = new CountDownLatch(num);
           while (num > 0) {
   
               threadPoolExecutor.execute(()->{
                   List<Object> params = new ArrayList<>();
                   params.add(nextLong());
                   params.add(nextLong());
                   try ( PreparedStatement preparedStatement = connection.prepareStatement(deleteSQL);) {
                       for (int i = 0; i < params.size(); i++) {
                           preparedStatement.setObject(i + 1, params.get(i));
                       }
   
                       preparedStatement.executeUpdate();
                   } catch (Exception e) {
                       System.out.println(e);
                   }finally {
                       count.countDown();
                   }
   
               });
   
               num--;
           }
   
           count.await();
   
           connection.close();
   
       }
   ```


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [shardingsphere] xue2lang closed issue #13539: multithreading use one ShardingSphereConnection instance , will cause java.sql.SQLTransientConnectionException: xx Connection is not available, request timed out after 2000ms.

Posted by GitBox <gi...@apache.org>.
xue2lang closed issue #13539:
URL: https://github.com/apache/shardingsphere/issues/13539


   


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [shardingsphere] xue2lang commented on issue #13539: multithreading use one ShardingSphereConnection instance , will cause java.sql.SQLTransientConnectionException: xx Connection is not available, request timed out after 2000ms.

Posted by GitBox <gi...@apache.org>.
xue2lang commented on issue #13539:
URL: https://github.com/apache/shardingsphere/issues/13539#issuecomment-966790265


   when execute PrepareStatement/Statement close method , release connection to connection pool


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [shardingsphere] xue2lang commented on issue #13539: multithreading use one ShardingSphereConnection instance , will cause java.sql.SQLTransientConnectionException: xx Connection is not available, request timed out after 2000ms.

Posted by GitBox <gi...@apache.org>.
xue2lang commented on issue #13539:
URL: https://github.com/apache/shardingsphere/issues/13539#issuecomment-965033597


   when one PreparedStatement execute finish,can we release cache connection using synchronized code ???
   
   have any idea???


-- 
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: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org