shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

How to obtain main thread information in HintManager thread pool scenario

Open penweizgx opened this issue 10 months ago • 1 comments

In a multi-tenant project implemented using Spring Boot, JPA, and ShardingSphere JDBC 5.2.1, I need to access the corresponding table based on the requested tenant ID. For instance, when user 'a' under a certain system tenant accesses student information, the SQL should be routed to the table t_student_{tenant_id}. Currently, this works as expected when called from the main thread. However, when invoked via asynchronous threads or parallel calls using Stream (with ForkJoinPool), since HintManager relies on ThreadLocal for context propagation and fails to inherit the parent thread's information, what approaches can be taken to achieve this goal?

penweizgx avatar Apr 28 '24 07:04 penweizgx

Can you try the new 5.5.0 version? This version may solve this problem.

strongduanmu avatar Apr 28 '24 07:04 strongduanmu

Since this issue has long time no response, I will close it.

strongduanmu avatar May 09 '24 05:05 strongduanmu

I looked at the new implementation and it doesn't seem to have changed much

penweizgx avatar May 11 '24 06:05 penweizgx

I looked at the new implementation and it doesn't seem to have changed much

Hi @penweizgx I checked the code again and there is indeed no modification here. I think we can try to use TransmittableThreadLocal to solve this problem.

strongduanmu avatar May 11 '24 07:05 strongduanmu

yes

penweizgx avatar May 13 '24 09:05 penweizgx

Hi @penweizgx, can you test master branch?

strongduanmu avatar May 19 '24 08:05 strongduanmu

@strongduanmu My local jar package has not been upgraded, but HintManager has been changed to this, which can solve the problems I encountered.

penweizgx avatar May 28 '24 05:05 penweizgx