Closing open connection if new method connect* is called#485
Closing open connection if new method connect* is called#485javornikolov merged 1 commit intodbfit:masterfrom
Conversation
|
Hi @igieon, in what scenarios would we face the need this close on connect to be executed? Have you faced a real situation? (I'm wondering e.g. why the old connection would have been left unclosed). |
|
For example we have test for BA integration we modify Source database, than run our etl transformation and check destination db and then we want to drop destination db, but sometimes we cant drop in teardown because open connection and just form list open connection i see that connection come from dbfit. One solution is always before new connect call command close which is implemented. This is just quick fix which i think is usefull. |
|
What about using |
|
yes that is also option ;-). That i suggest but its error prone if someone forgot it. |
|
I believe it's cleaner to have explicit |
|
I think yes that is much better. |
|
But then will be better just to add to the documentation ;-). |
|
And probably for cleaner test to write |close| it will be good throw exception in case of new connect ? |
Yes, it seems a good idea to add some guidelines for such scenarios.
That's maybe not a bad option. I'm still not sure that we need to write extra code (even though it's just 1 line) in order to handle these cases. Some may not bother so much about connections being closed with some delay (by the garbage collector or when tests run ends) if it's not critical. We should think whether it makes sense to enforce a bit more discipline in these cases? |
|
For us make sense as i write how i discover this is that we before test drop and recreate basic state of DB and then run test. And problem come as we run this tests in suite then we can't drop database. SO i think it will help someone other with same use-case to avoid this problem. |
|
OK, let's implement that. |
|
Are we going to break lots of other user's existing test assets? |
That's a good question and we don't know the answer. There might be existing tests which would get broken... (not quite disciplined in terms of releasing connections, yet they'd been working so far). So what do we prefer?
One thing I'm wondering about - even if we add some checks when trying to open a new connection, what about the following scenario (maybe even more common one):
And here there is no second @igieon, how would you handle such scenario? Also how hard was it for you to find out that it was an open connection that prevented dropping the database? |
|
In first it was not so hard to find what have open connection because when drop command failed i just added before drop one command to select all active connection with connected user and there is info who (ip) what is last command (so i seee exactly last select or commit). And about drop db in tear down we have really oposit we drop db and recreate correct in setup phase, but i think that second will be problem. |
In Flow Mode I think the connection will get closed by DbFit (because DatabaseTest is based upon SequenceFixture). But in Standalone Mode it will be left hanging about. |
Right. The natural place for tearing down resources (like dropping database) is at the end of the test page (e.g. via |
|
Here are few points which may lead us to some decision:
There might be existing tests which rely on the current behaviour as per |
|
Yes i am for option 1. That mean at the end all connections is closed (that was our expectation when we use multiple connections. So i can modify patch that close at the end all connections. So that will do exactly what we need. Option 2. For me doesnt make sense in our case we do it open 1 connection make some insert, run tested program, open second connection to verify results adn we expected that all connection is closed. Probably that every one goest with that expectation. |
|
If we allow multiple connections to be created but then don't provide a way to close each of them (as is the case now) then we're relying on the DB server to time them out and, probably, roll back their transactions if appropriate, or perhaps require users to execute some DB code or utility to clean them up. I.e. there's something missing in our connections management. In the long term perhaps a variant of each of the But until then, or if ever, perhaps a generic method for closing all DbFit-initiated connections would be good. Perhaps establishing a |
|
In addition, perhaps we can limit the breaking of existing users' tests by allowing any change like this to be controlled via something like a new |
|
If we assume that nobody indeed relies on case |
There was a problem hiding this comment.
No need of that null check here - closeConnection() already takes care about that.
Should we look to do something for Standalone-mode too in this PR? |
|
What part is standalone mode ? |
Standalone is more or less |
It's where we don't start tests/pages with one of the fixtures that sub-class
May be. But if not then we're still able to create connections in this mode that we can't close or clear up. May be some users are holding connections open across different test pages, in a suite, and so we'd end up breaking other tests if we implemented something. It would be good to review this in #487. |
That should be possible e.g. via: Yet, a reasonable question is why we're not doing that in
Yes, that's a real case: https://groups.google.com/d/msg/dbfit/l2m1_c4BDkQ/T23VwKXBNEkJ, https://groups.google.com/d/msg/dbfit/AQu_qrlUBSE/3GZVvnvklAYJ |
I mean if someone were to create a second connection without closing the first. We'll currently end up with a rogue one that can't be closed (until the currently proposed patch is applied).
Not quite sure what you mean. Can you explain differently? |
Yes, in standalone mode if you want to have the connection closed, you should invoke
E.g. here in |
|
So once this patch is applied we'll have the same handling for establishing a second connection (the first one will get closed) in both modes.
Right. I see. I didn't realise we weren't closing them down. I can log an issue for this just to tidy this up. |
In standalone mode (most) often the database type (Oracle, MySql, etc.) is explicitly specified: I think in such case the patch won't have impact since a new environment instance is created.
OK. |
Ah, yes. In that case should be look to close any existing connection that's open and available the previous environment via |
I would say we shouldn't bother. In standalone mode we don't explicitly close connections - that's how it used to be for single connection too. If timely closing is important - there is a In |
Ok. Although I still think one could end up with DB locks if the DB session doesn't always get killed because the parent Java process has ended. I've certainly seen this across different DB types in the past. I guess we can revisit this all on the concurrent-connections feature work. |
|
Opening a new connection without explicit |
|
SO how we change this patche set. DO we only close or we add additional option that throw exception if you open connection without closing previous one ? |
|
Thank you @igieon. Let's keep it as it is now. Just could you please add a note to the docs (the manual is within |
|
Like this ? |
There was a problem hiding this comment.
Instead of new section (###) I think this may be formatted like:
<div class="alert alert-warning alert-block">
If you need to open a second connection it's advised to close the current one first using <code>|Close|</code> command.
</div>
|
Its ok now ? |
There was a problem hiding this comment.
Let's remove that last blank line to keep it the same as connect-using-file.md
Thanks! Looks OK, I have just one cosmetic remark about the blank line. Also could you please squash the two commits into one. |
|
Fixed and squashed |
In Flow Mode close old connection if connecting > 1 times in order to ensure that all connections are closed at the end of test run. Nevertheless - it's still recommended to have explicit |Close| if it's important a databse connection to be timely closed.
|
I just did a test run of the docs using https://jekyllrb.com/. Seems my suggestion for removing the blank line at the end was not a good idea (it's messing up the rendering of the next section). @igieon, could you please update with blank line at the end of these files. You may just checkout my version which I posted to |
|
Great. I'm merging the PR. @igieon, thank you very much for your contribution! |
Closing previous connection on new |connect|
No description provided.