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/08/27 08:12:18 UTC

[GitHub] [shardingsphere] TeslaCN opened a new issue #12052: isSerial in JDBCExecutor is related to when the autoCommit is set

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


   ## Question
   
   Is there any problem that parallelly executing statements in transaction?
   
   ### Code
   
   ```java
   import org.apache.shardingsphere.driver.executor.DriverJDBCExecutor;
   import org.apache.shardingsphere.driver.jdbc.core.statement.ShardingSpherePreparedStatement;
   import org.apache.shardingsphere.infra.executor.sql.execute.engine.driver.jdbc.JDBCExecutor;
   
   import javax.sql.DataSource;
   import java.lang.reflect.Field;
   import java.sql.Connection;
   import java.sql.PreparedStatement;
   
   public class SerialExecute {
       
       private static final DataSource DATA_SOURCE = ShardingSpheres.createShardingSphereDataSource("/shardingsphere-jdbc/sysbench-multi.yaml");
       
       public static void main(String[] args) throws Exception {
           setAutoCommitBeforePrepareStatement();
           setAutoCommitAfterPrepareStatement();
       }
       
       private static void setAutoCommitBeforePrepareStatement() throws Exception {
           try (Connection connection = DATA_SOURCE.getConnection()) {
               connection.setAutoCommit(false);
               PreparedStatement preparedStatement = connection.prepareStatement("select c from sbtest1 limit 1");
               preparedStatement.execute();
               connection.rollback();
               System.out.println("setAutoCommitBeforePrepareStatement isSerial: " + isSerialExecute(preparedStatement));
               preparedStatement.close();
           }
       }
       
       private static void setAutoCommitAfterPrepareStatement() throws Exception {
           try (Connection connection = DATA_SOURCE.getConnection()) {
               PreparedStatement preparedStatement = connection.prepareStatement("select c from sbtest1 limit 1");
               connection.setAutoCommit(false);
               preparedStatement.execute();
               connection.rollback();
               System.out.println("setAutoCommitAfterPrepareStatement isSerial: " + isSerialExecute(preparedStatement));
               preparedStatement.close();
           }
       }
       
       private static boolean isSerialExecute(PreparedStatement preparedStatement) throws Exception {
           Field driverJDBCExecutorField = ShardingSpherePreparedStatement.class.getDeclaredField("driverJDBCExecutor");
           driverJDBCExecutorField.setAccessible(true);
           DriverJDBCExecutor driverJDBCExecutor = (DriverJDBCExecutor) driverJDBCExecutorField.get(preparedStatement);
           Field jdbcExecutorField = DriverJDBCExecutor.class.getDeclaredField("jdbcExecutor");
           jdbcExecutorField.setAccessible(true);
           JDBCExecutor jdbcExecutor = (JDBCExecutor) jdbcExecutorField.get(driverJDBCExecutor);
           Field serialField = JDBCExecutor.class.getDeclaredField("serial");
           serialField.setAccessible(true);
           return (boolean) serialField.get(jdbcExecutor);
       }
   }
   ```
   
   ### Result
   ```
   setAutoCommitBeforePrepareStatement isSerial: true
   setAutoCommitAfterPrepareStatement isSerial: false
   ```


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