INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Connection not reconnecting after disconnection to DB

Connection not reconnecting after disconnection to DB

(OP)
Hi Guys

This one baffles me. I'm trying to make my app so that it auto reconnects after the connection has been lost. I have a timer that tries to execute a simple query such as "Select 1". If an exception is thrown I know the connection has been lost. I then handle the situation by trying to close the connection and reopening it. This will keep on going until I can successfully query the database again. The problem is this. When I step through the code when the reconnection is suppose to take place, it keeps on failing and says "login failed for user...". This is after the connection is actually active and I can query the database on the backend. Instead of closing and opening the same conneciton I've even tried to destroy the object and create a new one, with all the same connection properties as defined on the form. But still, when I open the connection it gives the same msg "Login failed for user...". I have made a test application where it actually works. So I don't know why in the actuall app it is holding the connection or something?

Here is the default properties of the connection:

procedure TfrmMainDataMod.ResetConnection;
begin
try
try
if FConnectionString = '' then
FConnectionString := DBCON.ConnectionString;
if assigned(DBCON) then
FreeAndNil(DBCON);

DBCON := TADOConnection.Create(self);
with DBCON do
begin
Close;
Connected := false;
CommandTimeout := 120;
ConnectionString := FConnectionString;
ConnectionTimeout := 120;
ConnectOptions := coConnectUnspecified;
CursorLocation := clUseClient;
IsolationLevel := ilReadUncommitted;
KeepConnection := true;
LoginPrompt := false;
Mode := cmRead;
Provider := 'SQLOLEDB.1';
tag := 0;
Name := 'DBCON';
DefaultDatabase := '';
Open; // this part does not connect successfully, not matter how many times it tries.
end;
except on e:Exception do
MessageDlg(e.message, mtError, [mbOK], 0); // Error msg: Login failed for user
end;
finally

end;
end;

It would be awesome if someone can direct me in the right direction.
Thank you!

RE: Connection not reconnecting after disconnection to DB

Maybe you can check the Windows event log and see if there's any more information as to why the connection failed.

RE: Connection not reconnecting after disconnection to DB

(OP)
I checked the applications, security and system events for any info. There is nothing there...

RE: Connection not reconnecting after disconnection to DB

Hi I'm not sure if it would work, but try it!

- Add a variabl (global)

-- Var
-- iTimer : Integer;

- Add a timer

-program for timer is as follows

--...
-- inc(iTimer);
-- when iTimer = 120
-- Do
-- Open;
--...

> procedure TfrmMainDataMod.ResetConnection;
> begin
> try
> try
> if FConnectionString = ''
> then
> FConnectionString := DBCON.ConnectionString;
> if assigned(DBCON)
> then
> FreeAndNil(DBCON);
> DBCON := TADOConnection.Create(self);
> with DBCON
> do begin
> Connected := false;
> CommandTimeout := 120;
> ConnectionString := FConnectionString;
> ConnectionTimeout := 120;
>-- //Close; // As in take it out
-- Timer.Active := True;
> ConnectOptions := coConnectUnspecified;
> CursorLocation := clUseClient;
> IsolationLevel := ilReadUncommitted;
> KeepConnection := true;
> LoginPrompt := false;
> Mode := cmRead;
> Provider := 'SQLOLEDB.1';
> tag := 0;
> Name := 'DBCON';
> DefaultDatabase := '';
>-- //Open; // I've sepecified it under the timer
>- end; // 'With' I presume
> except on e:Exception do
> MessageDlg(e.message, mtError, [mbOK], 0); // Error >
> msg: Login failed for user
> end;
> finally
> end;
> end;


Thats about it_

RE: Connection not reconnecting after disconnection to DB

Oh yeah, just check the >'Open';
just specify what should open I didn't do that.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close