-
Jul 14th, 2021, 04:01 PM
#1
Thread Starter
Addicted Member
[RESOLVED] Weird error right after the db server goes up after being down.
Hi,
So I have a TableAdapter (created by the designer) that fills a table like this.
Code:
Try
cmd.Connection.Open()
MyTableAdapter.Fill(MyDataSet.MyTable)
cmd.Connection.Close()
Catch ex As Exception
cmd.Connection.Close()
Throw
End Try
When the db server is up it works fine.
When the db server is down it gives out this error:
MySqlException was caught:
Unable to connect to any of the specified MySQL hosts.
Which is fine too as it's expected.
The problem is when the server goes down and the above error gets thrown and then the server goes up again. When the code gets executed then it gives out this error:
MySqlException was caught:
Fatal error encountered during command execution.
This error happens only the first time the code executed right after the server goes up. When it gets executed the second time (and all the times that follow) it works fine. Which is wired.
Any enlightenment would be very appreciated.
-
Jul 14th, 2021, 04:18 PM
#2
Re: Weird error right after the db server goes up after being down.
I'm not on a VS PC right now but it may be an issue of the server awaken after the shutdown.
In that case, i believe there is a cmd.timeout or cmd.commandtimeout? Maybe give it a value depending on how fast the error happens.
So let's say it happens in 0,2 sec, then give it a value of 500.
I haven't use MySql so this is just a guess.
ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·
-
Jul 14th, 2021, 04:35 PM
#3
Thread Starter
Addicted Member
Re: Weird error right after the db server goes up after being down.
Originally Posted by sapator
I'm not on a VS PC right now but it may be an issue of the server awaken after the shutdown.
In that case, i believe there is a cmd.timeout or cmd.commandtimeout? Maybe give it a value depending on how fast the error happens.
So let's say it happens in 0,2 sec, then give it a value of 500.
I haven't use MySql so this is just a guess.
Thanks for the guess but I don't think it's about the timeout. I manually reproduced the error. I shut down the server and let it shut for say 3 minutes and then start it again and let it say for 3 minutes then I execute the code and it gives the same weird error the first time I execute it. My users don't see the error of course as it's being trapped but they always have to click the button twice to get it to work which is annoying.
-
Jul 15th, 2021, 07:09 AM
#4
Re: Weird error right after the db server goes up after being down.
Just because the server is back online doesn't mean the service is back online. I've got a docker container that runs an oracle database, the container itself only takes a minute or two to come online, but it takes 5 minutes for the database and the listener to come online. What happens if you wait 5 or 10 minutes?
-tg
-
Jul 15th, 2021, 07:40 AM
#5
Re: Weird error right after the db server goes up after being down.
Not related to your issue but that code needs changing. Firstly, closing the connection in both the Try and Catch blocks is wrong. You should only be closing once, in a Finally block. Finally exists specifically to do cleanup that need to be done whether an exception is thrown or not, e.g. closing a connection. If you really are doing nothing else in the Catch block but rethrowing, the Catch block is pointless:
vb.net Code:
Try
cmd.Connection.Open()
MyTableAdapter.Fill(MyDataSet.MyTable)
Finally
cmd.Connection.Close()
End Try
More than that though, unless there are actually other database operations in the Try block as well, there's no need or point to explicitly opening and closing the connection. The Fill method will do it implicitly so you can get rid of all that code other than the Fill call:
vb.net Code:
MyTableAdapter.Fill(MyDataSet.MyTable)
That is functionally equivalent to the code you have.
-
Jul 15th, 2021, 11:54 AM
#6
Thread Starter
Addicted Member
Re: Weird error right after the db server goes up after being down.
Originally Posted by techgnome
Just because the server is back online doesn't mean the service is back online. I've got a docker container that runs an oracle database, the container itself only takes a minute or two to come online, but it takes 5 minutes for the database and the listener to come online. What happens if you wait 5 or 10 minutes?
-tg
Hi tg, I appreciate your input.
I started the server and waited for 15 minutes; the problem persists!
If the operation just fails forever it would be easier. But it fails the first time only and then works just fine the next time and that's what makes it very weird to me.
I'd like to add something; It happens only in the .NET project and with the TapleAdapter specifically. I tried in MySql Workbench CE and with a simple MySqlCommand.ExecuteNonQuery method within my .NET project and it works fine. No errors whatsoever!
-
Jul 15th, 2021, 12:03 PM
#7
Thread Starter
Addicted Member
Re: Weird error right after the db server goes up after being down.
Hello John,
As you have noticed, the code snippet above was taken from a larger region of my code that has other db operations. I gave you the code snippet and removed the other operations where I have to use MySqlCommand.ExecuteNonQuery method which needs to open a connection first (e.g. to check for the server app version and force my users to update theirs).
Well, The connection I use with these operations is the same one the TapleAdapter use. I always open it, do some other db operations, and then I call the TableAdapter Fill method and then I close the connection. This has never caused a conflict before, but not this time. But after what you said about the non-necessity of opening a connection for the TableAdapter and although I don't open it for the TableAdapter but for the other db operation I got extremely specious, so, I followed your tips, and declared a new connection/mysqlcommand for the other operations, and guess what? The TapleAdapter worked from the first time and the error disappeared.
Oh my God John! that was a non related input and it has fixed my problem, what if it was related! Thank you, Thank you, Thank you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|