-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Renew connection in PostgresChannelMessageTableSubscriber only when invalid. #9111
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
Renew connection in PostgresChannelMessageTableSubscriber only when invalid. #9111
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that this is an evolution of your previous change for the #9061 .
Does this conn.isValid(1)
still report OK in case of DB failover ?
Thanks
connectionLatch.countDown(); | ||
new Thread(() -> { | ||
try { | ||
Thread.sleep(3000); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not very happy when we create unmanaged threads, even in tests.
Isn't there other way to achieve whatever is expected here?
I'm not sure, though, what is that we try to simulate with this separate thread...
Would you mind to elaborate the logic?
Thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this to close the connection after 3 seconds so that the isValid() method returns false and the connection is renewed. Do you now a better way to wait and close the connection?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. 3 seconds
is too much for our tests to perform.
We have like 5000 tests in the project. Now imagine if we don't care about the time on them.
How about just to have a connection to be closed immediately on a first call, so the next one would have it re-opened?
Instead of new thread and 3 seconds delay just have an AtomicBoolean
flag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think using an atomic flag would work here ... As I want the same connection to first succeed on the LISTEN
call and then fail after some time. I most probably don't get your suggestion, cause I don't see how to achieve this with an AtomicBoolean....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just changed your code to this and it still works:
AtomicBoolean connectionCloseState = new AtomicBoolean();
connectionSupplier.onGetConnection = conn -> {
connectionLatch.countDown();
if (connectionCloseState.compareAndSet(false, true)) {
try {
conn.close();
}
catch (Exception e) {
//nop
}
}
};
What do I miss?
In most cases it will report false, but if it would return true it would be ok to not renew the connection. From the JDBC javadoc:
The Idea behind this PR is to renew the connection only when we need to. |
connectionLatch.countDown(); | ||
new Thread(() -> { | ||
try { | ||
Thread.sleep(3000); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. 3 seconds
is too much for our tests to perform.
We have like 5000 tests in the project. Now imagine if we don't care about the time on them.
How about just to have a connection to be closed immediately on a first call, so the next one would have it re-opened?
Instead of new thread and 3 seconds delay just have an AtomicBoolean
flag.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are OK with my change to the test, I can merge it since I have it locally.
Thanks
connectionLatch.countDown(); | ||
new Thread(() -> { | ||
try { | ||
Thread.sleep(3000); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just changed your code to this and it still works:
AtomicBoolean connectionCloseState = new AtomicBoolean();
connectionSupplier.onGetConnection = conn -> {
connectionLatch.countDown();
if (connectionCloseState.compareAndSet(false, true)) {
try {
conn.close();
}
catch (Exception e) {
//nop
}
}
};
What do I miss?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Since you don't answer, I'm merging this with my correction.
We have to release today.
Feel free to follow up with other possible improvements afterwards.
Thanks
Ah yes sorry, was quite busy. Thanks for merging! |
Fixes: spring-projects#9111 An evolution of the spring-projects#9061: renew the connection only when we need to. **Auto-cherry-pick to `6.2.x` & `6.1.x`**
No description provided.