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/17 06:09:32 UTC

[GitHub] [shardingsphere] TeslaCN opened a new issue #13656: Transaction status incorrect when set autocommit=0 in MySQL Proxy

TeslaCN opened a new issue #13656:
URL: https://github.com/apache/shardingsphere/issues/13656


   ## Bug Report
   
   ### Which version of ShardingSphere did you use?
   master - 3f7a17c572418de3664c9f6747564ffc0902490d
   
   ### Which project did you use? ShardingSphere-JDBC or ShardingSphere-Proxy?
   ShardingSphere Proxy
   
   ### Expected behavior
   Transaction operation succeed.
   
   ### Actual behavior
   Changes cannot be rollbacked.
   
   ### Reason analyze (If you can)
   
   The MySQL JDBC Driver using `set autocommit=0` instead of `begin` when requires transaction, which makes the transaction status in ShardingSphere Proxy incorrect.
   
   ### Steps to reproduce the behavior, such as: SQL to execute, sharding rule configuration, when exception occur etc.
   
   ```SQL
   create table sbtest1
   (
       id  int                  not null
           primary key,
       k   int       default 0  not null,
       c   char(120) default '' not null,
       pad char(60)  default '' not null
   );
   
   create index k_1
       on sbtest1 (k);
   ```
   
   ```yaml
   schemaName: sbtest_direct
   
   dataSources:
     ds_0:
       url: jdbc:mysql://127.0.0.1:3306/sbtest_direct?serverTimezone=UTC&useSSL=false&prepStmtCacheSize=8192
       username: root
       password: root
       connectionTimeoutMilliseconds: 1000
       idleTimeoutMilliseconds: 60000
       maxLifetimeMilliseconds: 1800000
       maxPoolSize: 192
       minPoolSize: 0
   
   rules:
   - !READWRITE_SPLITTING
     dataSources:
       pr_ds:
         writeDataSourceName: ds_0
         readDataSourceNames: [ds_0]
   
   ```
   
   ### Example codes for reproduce this issue (such as a github link).
   
   ```java
   public class MySQLProxyTransaction {
       
       public static void main(String[] args) throws Exception {
           int id = ThreadLocalRandom.current().nextInt(10000) + 1;
           try (Connection connection = getConnection()) {
               connection.setAutoCommit(false);
               for (int i = 0; i < 2; i++) {
                   PreparedStatement ps1 = connection.prepareStatement("select k from sbtest1 where id = ?");
                   PreparedStatement ps2 = connection.prepareStatement("update sbtest1 set k = k + 1 where id = ?");
                   ps1.setInt(1, id);
                   ps2.setInt(1, id);
                   int previousK;
                   try (ResultSet resultSet = ps1.executeQuery()) {
                       resultSet.next();
                       previousK = resultSet.getInt(1);
                   }
                   ps2.executeUpdate();
                   try (ResultSet resultSet = ps1.executeQuery()) {
                       resultSet.next();
                       int currentK = resultSet.getInt(1);
                       if (currentK == previousK) {
                           throw new IllegalArgumentException();
                       }
                   }
                   connection.rollback();
                   try (ResultSet resultSet = ps1.executeQuery()) {
                       resultSet.next();
                       int currentK = resultSet.getInt(1);
                       if (currentK != previousK) {
                           throw new IllegalArgumentException();
                       }
                   }
               }
           }
       }
       
       @SneakyThrows
       private static Connection getConnection() {
           return DriverManager.getConnection("jdbc:mysql://127.0.0.1:13306/sbtest_direct?useServerPrepStmts=true", "root", "root");
       }
   }
   ```
   
   Result:
   ```
   Exception in thread "main" java.lang.IllegalArgumentException
   	at icu.wwj.hello.jdbc.MySQLProxyTransaction.main(MySQLProxyTransaction.java:40)
   ```
   


-- 
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] TeslaCN closed issue #13656: Transaction status incorrect when set autocommit=0 in MySQL Proxy

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


   


-- 
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] tristaZero closed issue #13656: Transaction status incorrect when set autocommit=0 in MySQL Proxy

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


   


-- 
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