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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]