Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

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

LINK TO THIS FORUM!

Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Partner With Us!

"Best Of Breed" Forums Add Stickiness To Your Site
Partner Button
(Download This Button Today!)

Feedback

"...this web site is a 'Godsend' for me. If I have a programming problem that I'm unable to solve, I'll get a sensible reply in no time. It's really great!..."

Geography

Where in the world do Tek-Tips members come from?

Connection not reconnecting after disconnection to DB

FrankPedro (Programmer)
18 Jul 12 5:36
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!
majlumbo (Programmer)
19 Jul 12 9:55
Maybe you can check the Windows event log and see if there's any more information as to why the connection failed.
FrankPedro (Programmer)
20 Jul 12 3:28
I checked the applications, security and system events for any info. There is nothing there...
MelodyRogueAngel (Programmer)
18 Aug 12 11:06
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_
MelodyRogueAngel (Programmer)
18 Aug 12 11:09
Oh yeah, just check the >'Open';
just specify what should open I didn't do that.

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!

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