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

Unexpected behaviour of copyStateTo() #2283

Open
kabanowster opened this issue Jan 16, 2025 · 0 comments
Open

Unexpected behaviour of copyStateTo() #2283

kabanowster opened this issue Jan 16, 2025 · 0 comments

Comments

@kabanowster
Copy link

kabanowster commented Jan 16, 2025

With version 5.1.0 the method copyStateTo() does a simple fields assignment from one HikariConfig object (A) to another (B) through java reflection. I'm assuming expected bahaviour of this method is to keep config A intact, i.e. as a default.
The unexpected result of field.set(other, field.get(this)); is that object references are getting passed, instead cloned instances.
This means, that changes done to i.e. config B dataSourceProperties (java.util.Properties) are reflected back to config A, which should remain intact. It leads to mixed properties for different sources. Please make it more reliable, or note this within documentation ;)

Here's custom copyStateTo() for config Properties fields:

// defaultConfig.copyStateTo(config);
// custom copy
for (var field : HikariConfig.class.getDeclaredFields()) {
    if (!Modifier.isFinal(field.getModifiers()) && field.trySetAccessible()) {
        try {
            Object value = field.get(defaultConfig);
            if (field.getType() == Properties.class) {
                field.set(config, new Properties((Properties) value));
            } else {
                field.set(config, value);
            }
        } catch (Exception e) {
            throw new RuntimeException("Failed to copy HikariConfig state: " + e.getMessage(), e);
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant