Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE#12022] Fix nacos datasource plugin ClassCastException bug (#12… #12031

Merged
merged 6 commits into from May 15, 2024

Conversation

mikolls
Copy link
Contributor

@mikolls mikolls commented Apr 25, 2024

What is the purpose of the change

Fix nacos datasource plugin ClassCastException bug https://github.com/alibaba/nacos/issues/12022

Brief changelog

Modify the clazz.getInterfaces() method in com.alibaba.nacos.plugin.datasource.proxy.MapperProxy#createProxy so that clazz can obtain the correct Class<?>[] interface

Verifying this change

After repair, nacos can run normally without ClassCastException and does not affect the original mysql mapper.

postgersql:
image

mysql:
image

@@ -43,7 +43,15 @@ public class MapperProxy implements InvocationHandler {

public <R> R createProxy(Mapper mapper) {
this.mapper = mapper;
return (R) Proxy.newProxyInstance(MapperProxy.class.getClassLoader(), mapper.getClass().getInterfaces(), this);
Class<?> clazz = mapper.getClass();
while (clazz.getInterfaces().length == 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个逻辑是否也有风险? 现在这个实现的逻辑是找到第一个interfaces不为0的类/或父类, 如果插件实现的时候自己定义了一个接口,然后那个接口也不是Mapper的子类或接口,应该也会出现ClassCase异常。

看下是否把所有的这个传入的mapper的所有interface都获取出来,然后让Proxy代理上?

如果没必要的话, 只需要找到实现了Mapper的对应class,然后再获取这个类的interfaces来动态代理。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

确实,这点是我考虑不周了,如果插件在实现的时候有自己的接口,这里也会报错。

拿实现了Mapper的对应class的interfaces,目前看来足够使用了,nacos中调用findMapper()也用不到插件自己实现的接口。

@mikolls mikolls requested a review from KomachiSion May 3, 2024 10:06
KomachiSion
KomachiSion previously approved these changes May 6, 2024
@KomachiSion KomachiSion added kind/bug Category issues or prs related to bug. plugin labels May 6, 2024
@KomachiSion
Copy link
Collaborator

CI seems can't pass, please check and fix them

@mikolls mikolls requested a review from KomachiSion May 6, 2024 09:37
@KomachiSion KomachiSion merged commit 9a6ec0c into alibaba:develop May 15, 2024
7 checks passed
@KomachiSion KomachiSion added this to the 2.4.0 milestone May 15, 2024
@mikolls mikolls deleted the develop-issue-12022 branch May 15, 2024 03:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Category issues or prs related to bug. plugin
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Nacos reports an error when using nacos-postgresql-datasource-plugin-ext: java.lang.ClassCastException
2 participants