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 2022/09/05 06:36:19 UTC

[GitHub] [shardingsphere] nimo10050 opened a new issue, #20789: If there is write operation in the same thread and database connection, all the following read operations are not from the master database to ensure data consistency.

nimo10050 opened a new issue, #20789:
URL: https://github.com/apache/shardingsphere/issues/20789

   ## Question
   
   If there is write operation in the same thread and database connection, **all the following read operations are not from the master database to ensure data consistency.**
   
   case:
   
    ```java
   public class AccountService {
       
       @Transactional
       public void test(String userId, BigDecimal num) {
           // step1,  in current transaction,  insert a data
           accountMapper.insertAccount(new Account(userId, num));
   
           // step2,  start a new transaction,  query all data
           accountService2.test();
   
           // step3, query all again.
           accountMapper.selectAll();
       }
   }
   
   public class AccountService2 {
   
       @Transactional(propagation = Propagation.REQUIRES_NEW)
       public void test() {
           accountMapper.selectAll();
       }
   
   }
   ```
   
   
   result:
   
   step1:insert data to master db
   step2:data from master db
   step3: query data from slave db
   
   when step1 and step3 in the same connection,step3 does not query data from master db。
   
   why ? @terrymanu 


-- 
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.apache.org

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


[GitHub] [shardingsphere] strongduanmu commented on issue #20789: If there is write operation in the same thread and database connection, all the following read operations are not from the master database to ensure data consistency.

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

   Since this issue has been fixed by https://github.com/apache/shardingsphere/pull/20814, I will close it now.


-- 
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] FlyingZC commented on issue #20789: If there is write operation in the same thread and database connection, all the following read operations are not from the master database to ensure data consistency.

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

   Now tansactionHolder(or TransactionConnectionContext) is at the thread level. When the inner transaction commits, the inTransaction flag will be cleared, which will cause the inTransaction read by the outer transaction to be false, resulting in routing to the slave database.


-- 
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] github-actions[bot] closed issue #20789: If there is write operation in the same thread and database connection, all the following read operations are not from the master database to ensure data consistency.

Posted by GitBox <gi...@apache.org>.
github-actions[bot] closed issue #20789: If there is write operation in the same thread and database connection, all the following read operations are not from the master database to ensure data consistency.
URL: https://github.com/apache/shardingsphere/issues/20789


-- 
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] strongduanmu closed issue #20789: If there is write operation in the same thread and database connection, all the following read operations are not from the master database to ensure data consistency.

Posted by GitBox <gi...@apache.org>.
strongduanmu closed issue #20789: If there is write operation in the same thread and database connection, all the following read operations are not from the master database to ensure data consistency.
URL: https://github.com/apache/shardingsphere/issues/20789


-- 
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] FlyingZC commented on issue #20789: If there is write operation in the same thread and database connection, all the following read operations are not from the master database to ensure data consistency.

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

   I want to investigate it, please assign to me. 


-- 
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] nimo10050 commented on issue #20789: If there is write operation in the same thread and database connection, all the following read operations are not from the master database to ensure data consistency.

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

   > The same with issue #20614.
   
   yes, he's the one who really saw the problem.        


-- 
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] strongduanmu commented on issue #20789: If there is write operation in the same thread and database connection, all the following read operations are not from the master database to ensure data consistency.

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

   Hi @FlyingZC , could we finish this issue in 5.2.1?


-- 
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] nimo10050 commented on issue #20789: If there is write operation in the same thread and database connection, all the following read operations are not from the master database to ensure data consistency.

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

   > The same with issue #20614.
   
   for shardingsphere 5.1.0 , the souce code for determining routes are as follows:
   
   ```java
       /**
         * ReadwriteSplittingDataSourceRouter
         * if return true,  route to the master database
         * else, route to the slave database
         **/
       private boolean isPrimaryRoute(final SQLStatementContext<?> sqlStatementContext) {
           SQLStatement sqlStatement = sqlStatementContext.getSqlStatement();
           return containsLockSegment(sqlStatement) || !(sqlStatement instanceof SelectStatement) || isHintWriteRouteOnly(sqlStatementContext) || TransactionHolder.isTransaction();
       }
   ```
   
   
   for the above  case: https://github.com/apache/shardingsphere/issues/20789#issue-1361469079
   
   For step 3, It seems like that the code that determine whether it routes master database is: 
   
   ```TransactionHolder.isTransaction()```
   
   So, when will it return true?
   
   In our case, when encountering ```@Transaction``` annotation, the following code will be executed:
   
   ```java
   // DataSourceTransactionManager
   protected void doBegin(Object transaction, TransactionDefinition definition) {
       if (con.getAutoCommit()) {
           con.setAutoCommit(false);
       }
   
   }
   
   // ShardingSphereConnection
   public void setAutoCommit(final boolean autoCommit) throws SQLException {
      
       if (!autoCommit) {
           TransactionHolder.setInTransaction();
       }
   }
   
   // TransactionHolder
   public static void setInTransaction() {
       TRANSACTION.set(true);
   }
   
   ```
   
   for code ```TransactionHolder.isTransaction()```,   
   step1  and step2 must be return true, then route to master database
   step3 must be return false, , then route to slave database
   


-- 
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] strongduanmu commented on issue #20789: If there is write operation in the same thread and database connection, all the following read operations are not from the master database to ensure data consistency.

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

   Since this task is not complete, I will move it to 5.2.2.


-- 
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] FlyingZC commented on issue #20789: If there is write operation in the same thread and database connection, all the following read operations are not from the master database to ensure data consistency.

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

   The same with issue #20614.


-- 
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] FlyingZC commented on issue #20789: If there is write operation in the same thread and database connection, all the following read operations are not from the master database to ensure data consistency.

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

   This bug has been fixed by #20814.Because the ConnectionContext is at the connection level, when the inner transaction commits, it will not affect the value of `inTransaction` in the outer transaction.But only local transactions support nesting transaction, xa transactions do not support nesting transaction, this is because xa transaction managers such as atomikos and narayana do not support this feature themselves.


-- 
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] github-actions[bot] commented on issue #20789: If there is write operation in the same thread and database connection, all the following read operations are not from the master database to ensure data consistency.

Posted by GitBox <gi...@apache.org>.
github-actions[bot] commented on issue #20789:
URL: https://github.com/apache/shardingsphere/issues/20789#issuecomment-1272348615

   Hello , this issue has not received a reply for several days.
   This issue is supposed to be closed.


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