Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 10 total
Thread Detecting failure to connect to database
Fri, Oct 16 2009 5:59 AMPermanent Link

"John Taylor"
DBIsam 4.27 (not client server), Delphi 2007

I know this should be simple but it's not behaving as I expect here is the
code fragment...

procedure TFmMain.ConnectDatabase;
begin
    try
      DM.DBSession.Active := true;
      DM.DBDatabase.Connected := false;
      DM.DBDatabase.Directory := AppOptions.General_DataPath;
      DM.DBDatabase.Connected := true;
      FDatabaseConnected := true;
    except
      On E: Exception do
      begin
        FDatabaseConnected := false;
        Messagedlg(Format(rs_DataConnectFail,[E.Message]),mtwarning,[mbok],0);
        exit;
      end;
    end;
    DM.DataTable_In.Active := true;
    DM.DataTable_Out.Active := true;
    DM.DataTable_JOBS.Active := true;
end;

The actual database is in a vmware virtual machine that is not running so
naturally the
database is not available.

I expect that an exception will be thrown and the except block will be
executed but not
so.  An exception is thrown and if I step through everything to get back to
this procedure
then the DM.DBDatabase.connected property is true if examined in the
debugger and the
lines execute as if no exception occurs, throwing another exception on the
first line
after the except block.

What am I doing wrong ?

TIA,

John Taylor
Fri, Oct 16 2009 7:34 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

John


I just tried it here in D6 V4.26 and I get the same result. With a nonsense directory the database still says its connected.

Roy Lambert
Fri, Oct 16 2009 7:42 AMPermanent Link

"John Taylor"
Do you have a try except block and actually gets stepped into ?

My try except block is completely ignored Frown

JT


"Roy Lambert" <roy.lambert@skynet.co.uk> wrote in message
news:470B5F7A-C214-4573-8554-2606B355C977@news.elevatesoft.com...
> John
>
>
> I just tried it here in D6 V4.26 and I get the same result. With a
> nonsense directory the database still says its connected.
>
> Roy Lambert
>
Fri, Oct 16 2009 8:33 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

John


Yup, I suspect its because no exception is raised Smiley It seems that connecting the database isn't checking for the existence of the directory. Try something like this instead

procedure TFmMain.ConnectDatabase;
begin
    if DirectoryExists(AppOptions.General_DataPath) then begin
      DM.DBSession.Active := true;
      DM.DBDatabase.Connected := false;
      DM.DBDatabase.Directory := ;
      DM.DBDatabase.Connected := true;
    end;
 try
    DM.DataTable_In.Active := true;
    DM.DataTable_Out.Active := true;
    DM.DataTable_JOBS.Active := true;
    except
On E: Exception do
begin
Messagedlg(Format(rs_DataConnectFail,[E.Message]),mtwarning,[mbok],0);
exit;
end;
end;

end;


Roy Lambert
Fri, Oct 16 2009 8:39 AMPermanent Link

"John Taylor"
I thought about that too, but the db.connected := true call could still fail
and the app comes crashing down

JT


"Roy Lambert" <roy.lambert@skynet.co.uk> wrote in message
news:9D0C2543-826A-41A2-97C8-64A5AEAD60E4@news.elevatesoft.com...
> John
>
>
> Yup, I suspect its because no exception is raised Smiley It seems that
> connecting the database isn't checking for the existence of the directory.
> Try something like this instead
>
> procedure TFmMain.ConnectDatabase;
> begin
> if DirectoryExists(AppOptions.General_DataPath) then begin
> DM.DBSession.Active := true;
> DM.DBDatabase.Connected := false;
> DM.DBDatabase.Directory := ;
> DM.DBDatabase.Connected := true;
> end;
> try
> DM.DataTable_In.Active := true;
> DM.DataTable_Out.Active := true;
> DM.DataTable_JOBS.Active := true;
> except
> On E: Exception do
> begin
> Messagedlg(Format(rs_DataConnectFail,[E.Message]),mtwarning,[mbok],0);
> exit;
> end;
> end;
>
> end;
>
>
> Roy Lambert
>
Fri, Oct 16 2009 9:13 AMPermanent Link

Roy Lambert

NLH Associates

Team Elevate Team Elevate

John


That's why I moved the exception handling to where you try and open the tables.

Roy Lambert [Team Elevate]
Fri, Oct 16 2009 11:08 AMPermanent Link

"Alessandra"
Roy Lambert wrote:
> I just tried it here in D6 V4.26 and I get the same result. With a
> nonsense directory the database still says its connected.

same here with 4.29b1.

That causes a very strange behaviour in some situations, just like the one i
found today in one of my app:

1) assign a database component to the "mydata" dir, which not exists, open
database > no exception, no warnings, nothing, database is connected

2) create the "mydata" dir

3) create some table into "mydata" dir

4) open some tables which are associated to the database component

5) try to append a record to one of these tables > you get the enigmatic
error "cannot access to a read-only dataset" or something like that

It would be better to add a check for database dir, when the app sets the
database connected valut to true.

Fri, Oct 16 2009 3:05 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

John,

<< I expect that an exception will be thrown and the except block will be
executed but not so.  An exception is thrown and if I step through
everything to get back to this procedure then the DM.DBDatabase.connected
property is true if examined in the debugger and the lines execute as if no
exception occurs, throwing another exception on the  first line
after the except block. >>

This is by-design.  The TDBISAMDatabase component is just a "pointer" to the
database location, and as such it does not actually "open" anything at the
engine level.  An exception will be thrown when you try to open a table and
the directory does not exist.

--
Tim Young
Elevate Software
www.elevatesoft.com

Fri, Oct 16 2009 3:28 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

Sandra,

<< 5) try to append a record to one of these tables > you get the enigmatic
error "cannot access to a read-only dataset" or something like that >>

A database directory is treated as read-only if a dbisam.lck lock file
cannot be created there for any reason.

<< It would be better to add a check for database dir, when the app sets the
database connected valut to true. >>

We cannot do so in a build - many applications already in the field assume
that you can assign an invalid database directory and still have the
TDBISAMDatabase open without an exception.  It's usually unintentional, but
changing it now would break these applications.  I'll consider adding it in
the next minor version update as a breaking change.

--
Tim Young
Elevate Software
www.elevatesoft.com

Sat, Oct 17 2009 11:08 PMPermanent Link

Tony Pomfrett
John,

You could just add 2 lines of code:

DM.DBDatabase.Connected := true;
DM.MyTable.Open; <-------
DM.MyTable.Close; <-------

Where MyTable was defined in DM. It could be a real table with data or just an empty table for the purpose of testing the connection.

That would check that the database directory existed and was reachable.
Image