-1

I have an ADOQuery and a ADOConnection on the form , the simplified code looks like this :

on Form.Create I simply give ADOConnection the connection parameters and make sure it is closed. After this I never open ADOConnection directly , it happens only via the ADOQuery .

try
 if ADOQuery.Active then ADOQuery.Close;
 ADOQuery.Open;
except
 Application.Terminate;
end;

I would expect this code to catch all manner of Exceptions (like a ADOQuery Timeout) and simply terminate the application.

Somehow I still receive a Query Timeout Exceeded . (But perhaps it is comming from ADOConnection itself ?) I am not 100% sure .

if I would do it like this would it solve the problem :

try
 if ADOConnection.Connected:=true then ADOConnection.Close;

 if ADOQuery.Active then ADOQuery.Close;
 ADOQuery.Open;
except
 Application.Terminate;
end;

Thank you.

UPDATE

The question is :

this is not Catching Query Timeout because the AdoConnection which it uses is not inside the try except block ? ( I don't really see any other reason )

In this example bellow ADOConnection1 is Connected on first ADOQuery.Open and stays like that till the software is done.

try
 if ADOQuery.Active then ADOQuery.Close;
 ADOQuery.Open;
except
 Application.Terminate;
end;

This except does not seem to catch all Query Timeouts...

user1937012
  • 1,031
  • 11
  • 20
  • 1
    This whole post makes no sense. There's no point in opening the connection and then closing it, only to have the ADOQuery open it again. There's also no point in opening and closing and opening and closing a connection. Open it at the beginning of the code and leave it open. If the first attempt to open the connection works, you're fine. If it doesn't, you catch it in that single spot and you're finished. – Ken White Sep 21 '19 at 13:32
  • 1
    @Ken Where do you see the code open, then close, then open again? – Jerry Dodge Sep 21 '19 at 13:39
  • 1
    @Jerry: Read the second paragraph of text. Then read the first code block,which opens the connection, closes it, and then uses the ADOQuery to open it again. And if the query auto-opens the connection, it closes it when the query is closed. Open connection->close connection->Open query and connection->Close query and connection->Open query and connection->Close query and connection just makes no sense. – Ken White Sep 21 '19 at 13:40
  • @Ken I don't see it being opened more than once - it's just making sure it's closed. Perhaps you mean it's assumed to have been opened before that? In any case, what's so wrong about that? Maybe there's a reason for it? FWIW, I've written things which need to connect to multiple different SQL servers in a loop. – Jerry Dodge Sep 21 '19 at 13:42
  • 1
    @Jerry: No. Read the text. The connection is opened and then immediately closed. Then the query (to which the connection is assigned) is opened,which **again** opens the connection. When the query is closed, the connection it opened is closed. Then the query is opened **again**, which opens the connection **again**. Rinse and repeat every time the query is closed and opened. – Ken White Sep 21 '19 at 13:44
  • 1
    Perhaps you're struggling with the fact that these components are already opened in design-time? The appropriate solution would be to not have them connected in design-time in the first place. – Jerry Dodge Sep 21 '19 at 13:45
  • 1
    @Ken I've read over the code many, many times, trying to understand what you're saying, but I only see it opened once via `ADOQuery.Open`. The rest of it is just making sure it's closed first. – Jerry Dodge Sep 21 '19 at 13:47
  • @Jerry; Do you actually think that the above represents all of the code in the poster's app? It's not even a single complete method. The database use logic is wrong in just these few lines, and I'd bet it's repeated multiple times in the app. When you're doing things wrong, you don't magically do them right in the next method or unit; you repeat the same mistakes until you learn to do it the right way. – Ken White Sep 21 '19 at 13:57
  • @Ken Of course it's not the full code, hence "simplified". I wouldn't be jumping to conclusions about the code you don't see though. Like I said, I've had good reason to close a connection and re-open it (to a different server). – Jerry Dodge Sep 21 '19 at 14:03
  • I can't possible post the Entire Program here . But for the sake of it . This entire thing is in a for loop . And since this Program is doing Synchronization between two databases , I re-connect . My question was regarding the ADOConnection , I am not sure but I think the Connection timeout which happens maybe once in a Month :) happens because the Adoconnection itself is not in the try except block . – user1937012 Sep 21 '19 at 14:07
  • @user1937012 The exception *should* be caught. Can you explain more about the behavior you're expecting versus the behavior you actually see? Could it be that you're in debug mode and you see an exception window pop up and you're thinking the exception wasn't caught? Have you stepped through the code to confirm that it never hits the exception handler? – Jerry Dodge Sep 21 '19 at 14:18
  • Have you got the Stop on Language exceptions debugger setting on? – MartynA Sep 21 '19 at 14:26
  • 1
    @KenWhite I agree with Jerry, the code shown is NOT opening and closing and opening and closing the connection. I see a bunch of closes and only 1 open. – Remy Lebeau Sep 21 '19 at 18:07
  • 1
    The fact that two of the regular contributors here, @KenWhite and Jerry Dodge are having difficulty following your q is a clear sign that it needs a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). I'm voting to close it until you add one. – MartynA Sep 21 '19 at 19:53

1 Answers1

0

You are using a Form on which these components are placed right.

So check if the Connected property of the TADOConnection is true in the IDE...

Now, make sure it’s ALWAYS false, because when you create the form, this property wil ‘activate/open’ the connection before the OnCreate event is fired.

R. Hoek
  • 916
  • 8
  • 27