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

add support to get and set db credentials in an atomic operation #2189

Merged

Conversation

benapple
Copy link
Contributor

@benapple benapple commented Mar 26, 2024

This pull request attempts to address points raised in #1442 (comment), #2011 (comment), and #1196 (comment) about rotating both username and password atomically.

Currently, you can call the setUsername and setPassword methods on the MBean or subclass the HikariDataSource to dynamically fetch credentials. Either way this is done presents a (tiny) window where the credentials used to connect may be in flux. In the case of updating via the MBean, a new connection may be created in between the call to setUsername and setPassword. And when subclassing the data source to dynamically provide username and password, the credentials may have changed between PoolBase's call to getUsername and getPassword.

To close these windows, I have introduced a new Credentials pojo that is essentially an immutable pair of username and password and replaced the HikariConfig's username and password fields with an AtomicReference to a Credential. It should be noted that even with these changes, you are still able to individually get and set the username and password, however if you need things to be atomic you should make use of the new API in HikariConfig, getCredentials and setCredentials. PoolBase now makes use of the getCredentials to atomically get the pair. Additionally, there is an extra method on the HikariConfigMXBean to atomically set the pair.

@IRus
Copy link

IRus commented Aug 20, 2024

@benapple, is AtomicReference necessary here? Since it’s not using CAS, you could consider using var handles or just @volatile instead.

@brettwooldridge brettwooldridge merged commit d43c272 into brettwooldridge:dev Sep 23, 2024
1 check passed
quaff added a commit to quaff/HikariCP that referenced this pull request Sep 24, 2024
it's not necessary to use AtomicReference here since the result of atomic method `updateAndGet` is unused, volatile is more lightweight than AtomicReference.

See brettwooldridgeGH-2189
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

Successfully merging this pull request may close these issues.

3 participants