You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "liruhui (via GitHub)" <gi...@apache.org> on 2023/03/31 03:15:00 UTC
[GitHub] [shardingsphere] liruhui opened a new issue, #24918: Properties transaction-read-query-strategy not work
liruhui opened a new issue, #24918:
URL: https://github.com/apache/shardingsphere/issues/24918
## 1. Official document
![image](https://user-images.githubusercontent.com/24687986/229012313-301c01db-1d0f-4686-8e92-5472784a49d2.png)
## 2. version 5.3.1
```
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core</artifactId>
<version>5.3.1</version>
</dependency>
</dependencies>
```
## 3. javaCode
```iava
public static void main(String[] args) throws SQLException {
//定义数据源
Map<String, DataSource> map = new HashMap<>();
HikariDataSource dataSource = new HikariDataSource();
dataSource.setDriverClassName(Driver.class.getName());
dataSource.setJdbcUrl("jdbc:mysql://192.168.191.164:3306/db_brand?serverTimezone=UTC");
dataSource.setUsername("root");
dataSource.setPassword("rootroot");
HikariDataSource dataSource1 = new HikariDataSource();
dataSource1.setDriverClassName(Driver.class.getName());
dataSource1.setJdbcUrl("jdbc:mysql://192.168.191.164:3307/db_brand?serverTimezone=UTC");
dataSource1.setUsername("root");
dataSource1.setPassword("rootroot");
HikariDataSource dataSource2 = new HikariDataSource();
dataSource2.setDriverClassName(Driver.class.getName());
dataSource2.setJdbcUrl("jdbc:mysql://192.168.191.164:3308/db_brand?serverTimezone=UTC");
dataSource2.setUsername("root");
dataSource2.setPassword("rootroot");
HikariDataSource dataSource3 = new HikariDataSource();
dataSource3.setDriverClassName(Driver.class.getName());
dataSource3.setJdbcUrl("jdbc:mysql://192.168.191.164:3309/db_brand?serverTimezone=UTC");
dataSource3.setUsername("root");
dataSource3.setPassword("rootroot");
map.put("write", dataSource);
map.put("read_0", dataSource1);
map.put("read_1", dataSource2);
map.put("read_2", dataSource3);
StaticReadwriteSplittingStrategyConfiguration staticConfiguration
= new StaticReadwriteSplittingStrategyConfiguration(
"write", Arrays.asList("read_0","read_1","read_2"));
ReadwriteSplittingDataSourceRuleConfiguration configuration =
new ReadwriteSplittingDataSourceRuleConfiguration(
"abc",staticConfiguration,null,"myBalancer");
Map<String, AlgorithmConfiguration> loadBalancers = new HashMap<>();
// thit is here
Properties prop = new Properties();
weightProp.put("transaction-read-query-strategy","FIXED_REPLICA");
loadBalancers.put("myBalancer",new AlgorithmConfiguration("RANDOM",prop));
ReadwriteSplittingRuleConfiguration readwriteSplittingRuleConfiguration =
new ReadwriteSplittingRuleConfiguration(Collections.singleton(configuration),loadBalancers);
Properties properties = new Properties();
properties.put("show-sql",true);
DataSource abc = ShardingSphereDataSourceFactory.createDataSource(map, Collections.singleton(readwriteSplittingRuleConfiguration), properties);
Connection connection = abc.getConnection();
connection.setAutoCommit(false);
for (int i = 0; i < 10; i++) {
ResultSet resultSet = connection.prepareStatement("select * from base_brand").executeQuery();
while (resultSet.next()){
long aLong = resultSet.getLong(1);
String string = resultSet.getString(2);
System.out.println(aLong+"====="+string);
}
System.out.println("===============================================");
}
connection.commit();
connection.close();
}
```
## 4. Test Result
```
set transaction-read-query-strategy=FIXED_REPLICA is In Transaction Randomly choose a fixed from slave nodes ,
but The result of my test is query from the master node every time.
```
--
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] bigno-feng commented on issue #24918: Properties transaction-read-query-strategy not work
Posted by "bigno-feng (via GitHub)" <gi...@apache.org>.
bigno-feng commented on issue #24918:
URL: https://github.com/apache/shardingsphere/issues/24918#issuecomment-1547627885
![image](https://github.com/apache/shardingsphere/assets/37724987/9644cca7-b804-41c4-9fa1-3b8cd7456f33)
![image](https://github.com/apache/shardingsphere/assets/37724987/2051ed3f-78b3-40cd-8fa0-5e6b86f27ffd)
![image](https://github.com/apache/shardingsphere/assets/37724987/56f7b35f-a01a-4ac7-a548-9b1e2303b413)
--
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] bigno-feng commented on issue #24918: Properties transaction-read-query-strategy not work
Posted by "bigno-feng (via GitHub)" <gi...@apache.org>.
bigno-feng commented on issue #24918:
URL: https://github.com/apache/shardingsphere/issues/24918#issuecomment-1547588712
兄弟,这文档哪里看的
--
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] bigno-feng commented on issue #24918: Properties transaction-read-query-strategy not work
Posted by "bigno-feng (via GitHub)" <gi...@apache.org>.
bigno-feng commented on issue #24918:
URL: https://github.com/apache/shardingsphere/issues/24918#issuecomment-1547623564
Upgrading to 5.3.2 or not working
--
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 #24918: Properties transaction-read-query-strategy not work
Posted by "FlyingZC (via GitHub)" <gi...@apache.org>.
FlyingZC commented on issue #24918:
URL: https://github.com/apache/shardingsphere/issues/24918#issuecomment-1498877072
Hi, `ransaction-read-query-strategy` is not in the `5.3.1` release version.This change is after 5.3.1 and has been removed soon, please upgrade to 5.3.2.In 5.3.1, you can use the `FIXED_REPLICA_RANDOM` instead of `RANDOM`.
--
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 #24918: Properties transaction-read-query-strategy not work
Posted by "TeslaCN (via GitHub)" <gi...@apache.org>.
TeslaCN closed issue #24918: Properties transaction-read-query-strategy not work
URL: https://github.com/apache/shardingsphere/issues/24918
--
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 #24918: Properties transaction-read-query-strategy not work
Posted by "strongduanmu (via GitHub)" <gi...@apache.org>.
strongduanmu commented on issue #24918:
URL: https://github.com/apache/shardingsphere/issues/24918#issuecomment-1501385118
Hi @liruhui, according to @FlyingZC 's suggestion, can you try to use 5.3.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 #24918: Properties transaction-read-query-strategy not work
Posted by "FlyingZC (via GitHub)" <gi...@apache.org>.
FlyingZC commented on issue #24918:
URL: https://github.com/apache/shardingsphere/issues/24918#issuecomment-1548890955
@bigno-feng Hi,I tested the following code in version 5.3.2, and it can be routed to the main library correctly.
```java
public class RwTest {
public static void main(String[] args) throws SQLException {
//定义数据源
Map<String, DataSource> map = new HashMap<>();
HikariDataSource dataSource = new HikariDataSource();
dataSource.setDriverClassName(Driver.class.getName());
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/demo_write_ds?serverTimezone=UTC");
dataSource.setUsername("root");
dataSource.setPassword("root");
HikariDataSource dataSource1 = new HikariDataSource();
dataSource1.setDriverClassName(Driver.class.getName());
dataSource1.setJdbcUrl("jdbc:mysql://localhost:3306/demo_read_ds_0?serverTimezone=UTC");
dataSource1.setUsername("root");
dataSource1.setPassword("root");
HikariDataSource dataSource2 = new HikariDataSource();
dataSource2.setDriverClassName(Driver.class.getName());
dataSource2.setJdbcUrl("jdbc:mysql://localhost:3306/demo_read_ds_1?serverTimezone=UTC");
dataSource2.setUsername("root");
dataSource2.setPassword("root");
map.put("write", dataSource);
map.put("read_0", dataSource1);
map.put("read_1", dataSource2);
StaticReadwriteSplittingStrategyConfiguration staticConfiguration
= new StaticReadwriteSplittingStrategyConfiguration(
"write", Arrays.asList("read_0","read_1"));
ReadwriteSplittingDataSourceRuleConfiguration configuration =
new ReadwriteSplittingDataSourceRuleConfiguration(
"abc",staticConfiguration,null,"myBalancer");
Map<String, AlgorithmConfiguration> loadBalancers = new HashMap<>();
Properties prop = new Properties();
prop.put("transaction-read-query-strategy","FIXED_PRIMARY");
loadBalancers.put("myBalancer",new AlgorithmConfiguration("RANDOM",prop));
ReadwriteSplittingRuleConfiguration readwriteSplittingRuleConfiguration =
new ReadwriteSplittingRuleConfiguration(Collections.singleton(configuration),loadBalancers);
Properties properties = new Properties();
properties.put("show-sql",true);
DataSource abc = ShardingSphereDataSourceFactory.createDataSource(map, Collections.singleton(readwriteSplittingRuleConfiguration), properties);
Connection connection = abc.getConnection();
connection.setAutoCommit(false);
for (int i = 0; i < 10; i++) {
ResultSet resultSet = connection.prepareStatement("select * from t_order").executeQuery();
while (resultSet.next()){
String aLong = resultSet.getString(1);
String string = resultSet.getString(2);
System.out.println(aLong+"====="+string + "route: " + resultSet.getString(3));
}
System.out.println("===============================================");
}
connection.commit();
connection.close();
}
}
```
<img width="364" alt="image" src="https://github.com/apache/shardingsphere/assets/19788130/fdd887c8-0311-4d6e-8e44-4a2c23c3103b">
--
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