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

   ## About Read-Write Splitting and Health Checks for the Slave Database
   
   ### Description
   
   The scenario is as follows: My database adopts a 1-master, multiple-slaves 
design. The connection to the master database uses a VIP (Virtual IP) address, 
which provides failover capability for the master database. However, the slave 
databases do not have the same failover capability. Therefore, I would like to 
implement a custom load-balancing algorithm for the slave databases that 
includes a health check mechanism, allowing for fault isolation and automatic 
recovery.
   
   In practice, after reviewing the code, I realized that I need to obtain an 
instance of the `ContextManager` in order to check if the slave database 
connections are available. However, I found that there is no straightforward 
way to get this instance. The only option seems to be using reflection to 
retrieve the `contextManager` field from the `ShardingSphereDataSource`.
   
   ```java
   try (Connection conn = DriverStateContext.getConnection(slaveDataSource, 
contextManager)) {
       isHealthy = conn.isValid(2); // 2-second timeout
       return isHealthy;
   }
   ```
   
   ```xml
   <!-- 
https://mvnrepository.com/artifact/org.apache.shardingsphere/shardingsphere-jdbc
 -->
   <dependency>
       <groupId>org.apache.shardingsphere</groupId>
       <artifactId>shardingsphere-jdbc</artifactId>
       <version>5.5.0</version>
   </dependency>
   ```
   
   Finally, I use `DriverStateContext` to determine whether the connection is 
available. Is there a better way to achieve this?
   
   I sincerely seek your advice. I have also raised an issue regarding this.
   


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

Reply via email to