Recent

Author Topic: Access violation in test for Firebird  (Read 6411 times)

cov

  • Full Member
  • ***
  • Posts: 241
Access violation in test for Firebird
« on: August 29, 2014, 09:37:16 am »
I want to check that Firebird is installed and running.

Code: [Select]
  DatabaseRunning:=True;
  try
    Form_DB.IBConnection1.DatabaseName:=dbf;
  except
    on E: Exception do
      DatabaseRunning:=False;
  end;                       

The App halts at the 'try' statement with a dialog box:

Quote
Access Violation.

Press OK to ignore and risk data corruption
Press Cancel to kill the program

Why isn't it handling the exception?

Is there a better way of telling if Firebird is running?

Firebird may be installed or embedded with DLL libraries.

Windows XP, Lazarus 1.1 r40379 FPC 2.6.1 i386-win32-win32/win64
« Last Edit: August 29, 2014, 09:38:59 am by cov »

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: Access violation in test for Firebird
« Reply #1 on: August 29, 2014, 02:00:09 pm »
Are you running your application inside the Lazarus  IDE ?

If it's the case, the most simple way to fix this is to run your program outside the IDE.

See, this link for instance for more informations:
http://forum.lazarus.freepascal.org/index.php/topic,24743.0.html

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Access violation in test for Firebird
« Reply #2 on: August 29, 2014, 02:20:58 pm »
Is there a better way of telling if Firebird is running?
You could just set up your connection and do a connect.
If it fails with an exception you could catch that exception.

If you really want a function to see if there is a server available (Embedded or on localhost) you can use the following function:
Code: [Select]
function FirebirdRunning(const Host, User, Password: string): boolean;
var
  FBAdmin: TFBAdmin;
begin
  Result := false;
  FBAdmin := TFBAdmin.Create(nil);
  try
    FBAdmin.Host := Host;
    FBAdmin.User := User;
    FBAdmin.Password := Password;
    FBAdmin.Connect;
    if FBAdmin.ServerVersion <> '' then
      Result := true;
  finally
    FBAdmin.Free;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if FirebirdRunning('', 'SYSDBA', 'masterkey') then
    ShowMessage('Firebird is running')
  else
    ShowMessage('Firebird is NOT running');
end;

When you want to check for a remote server you could provide a host-name (or ip) in the host parameter.

cov

  • Full Member
  • ***
  • Posts: 241
Re: Access violation in test for Firebird
« Reply #3 on: August 29, 2014, 03:50:02 pm »
Thanks, I'll adjust my code and see if it works.

Firebird should be installed on the local machine (embedded) or installed on the network.  Testing for a specific IP wouldn't work, I'm afraid.

The error message I quoted is from outside the IDE.

From within the IDE it just shows the exception two or three times and then stops.

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Access violation in test for Firebird
« Reply #4 on: August 29, 2014, 05:21:39 pm »
Firebird should be installed on the local machine (embedded) or installed on the network.  Testing for a specific IP wouldn't work, I'm afraid.
There lies a problem.
You can test for Firebird on your own computer (by using an empty hostname) or on the network (by hosing a specific hostname/ip).
You can't test for both (at the same time).
If you don't know if there is a Firebird server in your network and you want to know if there is one, you should test all the hosts.
There is no magic command to scan your internal network to check for a Firebird server.
You need to issue a command to a specific server to see if Firebird is running there.


cov

  • Full Member
  • ***
  • Posts: 241
Re: Access violation in test for Firebird
« Reply #5 on: September 05, 2014, 10:35:07 pm »
What I'm thinking of is a small CLI program which can be called and which can request a connection to whichever database invocation exists.

I'd like my app to ultimately be database-agnostic, if at all possible.

So my app can call the CLI program, and, depending on the response connect to whatever database is available. Obviously, the host, database name, user and password would need to be supplied, but if these are incorrectly given, I don't want my app to fall over.

Is this feasible?

I've had a quick look at writing such a CLI program, but I'm not sure how to proceed.

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Access violation in test for Firebird
« Reply #6 on: September 05, 2014, 10:55:33 pm »
So my app can call the CLI program, and, depending on the response connect to whatever database is available. Obviously, the host, database name, user and password would need to be supplied, but if these are incorrectly given, I don't want my app to fall over.
Then why a separate CLI?
If you're program is going to supply a host (or IP), database-name, user and password why not just make the connection and check for an exception? If an exception is given show the user a message the database-server is unavailable. Your program doesn't have to "fall over" but it just needs to deal with the exception gracefully. (Without the database online your program doesn't work anyway but it can handle this correctly)

What I'm thinking of is a small CLI program which can be called and which can request a connection to whichever database invocation exists.
So this is different from what you state in the other sentence, that your program needs to supply the host etc...
This sounds like you want your program to say... I want database XYZ and the CLI should respond with where the host is for XYZ ???
If that's the case you should scan your network (all known hosts/IP's) to see where there is a database server and which one has XYZ. Or you should make some other signaling method on your network to figure out the database host (i.e. the database hosts sends out a broadcast periodically over the network so clients can receive this and know there is a server on that host)

So... are you supplying a host (or IP) from your program or not?
(And what exactly do you mean by "I don't want my app to fall over" ??)

cov

  • Full Member
  • ***
  • Posts: 241
Re: Access violation in test for Firebird
« Reply #7 on: September 06, 2014, 08:04:48 am »
My original purpose was to run a database locally on the machine. Hence embedded. Or maybe on a local network.

However, it's been pointed out to me that it may be useful to have the database in the cloud, probably MySQL, but possibly others like Postgres, etc.

I'm just looking at using a CLI to check that a connection is there without falling over if there is an error. Or rather, if it does fall over, then the app remains unaffected, but aware of the error.

rvk

  • Hero Member
  • *****
  • Posts: 6111
Re: Access violation in test for Firebird
« Reply #8 on: September 06, 2014, 02:42:14 pm »
I'm just looking at using a CLI to check that a connection is there without falling over if there is an error. Or rather, if it does fall over, then the app remains unaffected, but aware of the error.
A connection is where?? (Where is there?)

And how would your application remain unaffected when there is no database available. Doesn't your application need the database?

You've also still not answered my question why you want to do this in a CLI and why your application would "fall over" if you did it there. With a simple exception handling you can just do this in your own application. There is no need for a separate CLI. (b.t.w. your original problem in your first post is due to something else so don't think that should be the reason for using a CLI)

So... are you supplying a host (or IP) from your program or not?
(And what exactly do you mean by "I don't want my app to fall over" ??)

 

TinyPortal © 2005-2018