-
Notifications
You must be signed in to change notification settings - Fork 36
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
Applying settings to database() at runtime does not reuse database connections #75
Comments
Not sure if there is a way to create an unique hashref. @bigpresh , thoughts? |
For anyone that runs into this problem, I kludged my way around this issue by setting the database definition hash globally:
and then anywhere a database handle is needed,
The key is to avoid using this feature as shown in the documentation; otherwise, database handles will not be reused and you will quickly hit your max_connections threshold. |
Nice solution, @yahermann. Thanks for sharing! |
Let me reopen the ticket. We need to either correct the behavior or to document your approach and warn users. |
Fair enough :-) |
Hmm, sorry I managed to miss this one. Yes, agreed, we ought to work out a fix - serialising the hashref contents to generate a key sounds like the best option which would DWYM. |
I think serializing the hashref would work to generate a unique scalar key, as long as the hashref keys AND values AND subkeys/subvalues recursively (e.g. dbi_params => { ... } ) are all part of the scalar key, and sorted by each key/subkey within the hashref. There must be a Perl module that does this in a reliable and consistent way (and hopefully already existing in the dependency chain). Otherwise you would DWYM intermittently, which is much worse! Another approach, and one that I think I prefer: Require the user to manually specify a scalar key when using this option, explicitly for the purpose of reusing database handles. |
I ran into another ugly problem using It appears the |
I finally had a chance to hack on this, and got part-way, but one fun problem: the settings may contain coderefs, e.g. HandleError, whatever serialisation form is used needs to understand that. It will probably have to just ignore coderefs, so there's the risk that, if all other arguments are the same but the coderef is different, you still get the cached handle; that sounds reasonable enough if documented. I'm going to head to bed in a moment, so I'll poke at this again as soon as time permits. |
Per documentation, using database() in this way:
causes a brand new connection to be created each time, and old connections don't get disconnected, so the max gets hits quickly.
The problem appears to be in Database.pm, lines 52-54:
Even though $arg may be the same hash, i.e., { database => "mydb", username="myuser" }, unless special precautions are taken, generating it during runtime creates a different anonymous hashref each time, therefore when used as a key, Perl treats them each as different keys. Since they're different keys, a new connection gets created with each call, and old connections/handles are not reused.
The text was updated successfully, but these errors were encountered: