You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by "jpenalver10 (via GitHub)" <gi...@apache.org> on 2025/12/29 10:23:11 UTC
[I] Question about StandardShardingAlgorithm as a Spring bean [shardingsphere]
jpenalver10 opened a new issue, #37572:
URL: https://github.com/apache/shardingsphere/issues/37572
Hi everyone, I have a question regarding the definition of an own CLASS_BASED sharing algorithm in a Spring Boot application.
I need to define a programatic sharding rule based on the customer ID and I want to store these configuration in a database table that will be filled based on business rules. As my application is a SpringBoot application I want to be able to inject the JPA repository into my sharding algorithm to retrieve the config from database.
I realize that using the standard configuration, ShardingSphere builds the class instance using the default constructor, so can't inject anything and the algorithm don't belongs to Spring context.
```
shardingAlgorithms:
customer-to-shard:
type: CLASS_BASED
props:
strategy: STANDARD
algorithmClassName: com.jpenalver10.dbsharding.TenantShardingAlgorithm
```
Is there any recommended/official way to do this?
Thank you so much.
--
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
Re: [I] Question about StandardShardingAlgorithm as a Spring bean [shardingsphere]
Posted by "terrymanu (via GitHub)" <gi...@apache.org>.
terrymanu closed issue #37572: Question about StandardShardingAlgorithm as a Spring bean
URL: https://github.com/apache/shardingsphere/issues/37572
--
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
Re: [I] Question about StandardShardingAlgorithm as a Spring bean [shardingsphere]
Posted by "jpenalver10 (via GitHub)" <gi...@apache.org>.
jpenalver10 closed issue #37572: Question about StandardShardingAlgorithm as a Spring bean
URL: https://github.com/apache/shardingsphere/issues/37572
--
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
Re: [I] Question about StandardShardingAlgorithm as a Spring bean [shardingsphere]
Posted by "jpenalver10 (via GitHub)" <gi...@apache.org>.
jpenalver10 commented on issue #37572:
URL: https://github.com/apache/shardingsphere/issues/37572#issuecomment-3698684243
Thanks for you answer.
Ok, I understand this behaviour is not the designed for the library, not the intended use, so perhaps I have to think another way to dinamically change the sharding algorithm (via external properties, etc).
Anyway, in my proof of concept I defined the DB table that contains the sharding information as SINGLE, so the recursion risk during the resolution is low, as sharding algorithm always query the main shard and is not recursively called itself --> `main.db_sharding.*`:
```
rules:
- !SINGLE
tables:
- main.outbox_dm_tables.*
- main.db_sharding.*
- main.non-existing
defaultDataSource: main
```
Regarding this config, if you'll allow me, I want to do a question about the definition format of tables:
- `main.outbox_dm_tables.*` this would means "All tables from outbox_dm_tables schema goes to main shard"?
- `main.db_sharding.sharding_tenant_config` this would means "Only the table sharding_tenant_config from db_sharding schema goes to main shard"?
- As I know, the SINGLE rules are the default beahviour for all tables that are not defined in other ways (not SHARDING etc) and I have defined a default data source. It means that all tables in another schema (suppose `infrastructure`) will be routing by default to main shard? In my config this happens when I add this rule `main.non-existing` that is intentionally incorrect, so all tables in a `infrastructure` schema are routing to default, but if I removed the tablas can't be located by ShardingSphere.
--
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
Re: [I] Question about StandardShardingAlgorithm as a Spring bean [shardingsphere]
Posted by "terrymanu (via GitHub)" <gi...@apache.org>.
terrymanu commented on issue #37572:
URL: https://github.com/apache/shardingsphere/issues/37572#issuecomment-3696848485
Problem Understanding
- User created a CLASS_BASED standard sharding algorithm in Spring Boot, wants to read sharding config from a DB via a JPA repository, and found the algorithm instance is built with the default constructor outside Spring, so no injection happens.
Root Cause
- Custom sharding algorithms (CLASS_BASED) are instantiated by ShardingSphere internals (ShardingAlgorithmFactory/NewInstanceServiceLoader) via reflection with a no-arg constructor, not as Spring beans. Spring beans (including JPA repositories) cannot be injected.
- Algorithms are designed as SPI, stateless, pure-function components with configuration supplied via props. Pulling data from business DB or depending on Spring beans during routing isn’t supported and risks recursion, performance hits, and init-order issues.
Analysis
- Spring Boot YAML passes algorithmClassName to ShardingSphere core, which creates the instance outside the Spring container; BeanFactory is not involved, so injection is impossible.
- Having the algorithm query JPA during routing would require database access before routing is resolved, causing circular routing risk and latency, diverging from the intended “static config, pure function” model.
- Current behavior matches official design; this is an expectation mismatch, not a product bug.
Conclusion
Follow the official pattern: implement the sharding algorithm SPI as a stateless component without Spring dependencies; pass required settings via props. For dynamic rules, load them into ShardingSphere’s supported config/governance mode at startup rather than injecting Spring beans into the algorithm.
--
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
Re: [I] Question about StandardShardingAlgorithm as a Spring bean [shardingsphere]
Posted by "jpenalver10 (via GitHub)" <gi...@apache.org>.
jpenalver10 commented on issue #37572:
URL: https://github.com/apache/shardingsphere/issues/37572#issuecomment-3698958905
Understood, thanks.
--
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
Re: [I] Question about StandardShardingAlgorithm as a Spring bean [shardingsphere]
Posted by "terrymanu (via GitHub)" <gi...@apache.org>.
terrymanu commented on issue #37572:
URL: https://github.com/apache/shardingsphere/issues/37572#issuecomment-3698899011
If the two comments are not describing the same problem, please close this issue and open a new one for discussion.
--
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