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