How would you know if a connection is alive or dead before executing a query? I’m using ecto
You can’t. Even if you were able to check the status of the connection socket used by Ecto, the connection may die by the time you send the query. You instead should check the result of the query after running it.
But if the database is down, when run Repo.transaction it generates the following error: “connection not available because of disconnection” and I can not find the way to check the result of Repo.transaction
Repo.transaction
should return either an :ok
tuple or an :error
tuple, so you should be able to check like you do everywhere else.
Perhaps you can show some code?
Also when the database is unavailable, this is usually an error which I classify as “unrecoverable” and just let the process crash. Of course there may be scenarios in which you want to report proper error instead of crashing
Perhaps you elaborate about your use case as well?
And that is why I’m still surprised to see every ORM under the sun do a “SELECT 1” before sending the actual query. Just ship the friggin’ query and do some proper error handling, including retries.
It doesn’t directly answer the question but you can do something like this as soon as you connect
execute a query
For Mairaex:
after_connect: {Mariaex, :query!, [“SET variable = value”, []]}
https://hexdocs.pm/ecto/Ecto.Adapters.MySQL.html#module-after-connect-callback
Wait what? Many do that? Why?! o.O