How can I check if connection is active?

The forum for ZeosLib 7.2 Report problems. Ask for help, post proposals for the new version and Zeoslib 7.2 features here. This is a forum that will be edited once the 7.2.x version goes into RC/stable!!

My personal intention for 7.2 is to speed up the internals as optimal a possible for all IDE's. Hope you can help?! Have fun with testing 7.2
Post Reply
Soner
Junior Boarder
Junior Boarder
Posts: 27
Joined: 12.02.2017, 17:00

How can I check if connection is active?

Post by Soner »

Hello,
in my program I have active TZConnection with Firebird datebase, then I connect with TFBAdmin to the Database server to change password from other user. When I do this TZConnection looses the connection, but the program does not know about it, ZConnection1.Connected is true and ZConnection1.Ping is also true. Now when I execute a sql command then TZConnection gives error:
"SQL Error: Invalid database handle (no active connection)..."
How can I check success full if then connection is really active?





I use Lazarus 1.6.4, fpc 3.0.2, Zeos 7.2.1-Svn 3951, Firebird 2.5 Database
marsupilami
Platinum Boarder
Platinum Boarder
Posts: 1918
Joined: 17.01.2011, 14:17

Re: How can I check if connection is active?

Post by marsupilami »

Hello Soner,

hmmm - currently the Firebird driver doesn't support pinging the server. But usually you should get an exception telling you that this operation is not supported...

But: Changing the password of a user should not kill other connections. Maybe it is a good idea to use Zeos for changing the password using the corresponding SQL from Firebird 2.5 (See corresponding part of Firebird 2.5 Language Ref. Update):
Firebird 2.5 Language Ref. Update wrote:ALTER USER username
[PASSWORD 'password']
[FIRSTNAME 'firstname']
[MIDDLENAME 'middlename']
[LASTNAME 'lastname']
[{GRANT|REVOKE} ADMIN ROLE]

-- At least one of the optional parameters must be present.
-- GRANT/REVOKE ADMIN ROLE is reserved to privileged users.
With best regards,

Jan
Soner
Junior Boarder
Junior Boarder
Posts: 27
Joined: 12.02.2017, 17:00

Re: How can I check if connection is active?

Post by Soner »

This problem is resolved with version 7.2.4.
If someone wants to check if firebird-server is available then use ping-funcktion like this:

Code: Select all

 if ZConnection1.Ping then
   Writeln('Connection exists');
 //ZConnection1.Connected  is true, even when connection lost. 
   
(Only tested with Firebird 2.5, Windows)
User avatar
aehimself
Zeos Dev Team
Zeos Dev Team
Posts: 765
Joined: 18.11.2018, 17:37
Location: Hungary

Re: How can I check if connection is active?

Post by aehimself »

Just keep in mind that not all mainstream database engines support the .Ping function. (yet?)
When I was developing an application which had to be able to communicate with ANY type of SQL server I built my own keepalive (NOOP) function by executing a simple query:
SELECT 1;
If an exception is raised - no connection. I agree that .Ping would be easier (and feels less hacky!) but the custom noop will always produce accurate results, regardless of the backend.
marsupilami
Platinum Boarder
Platinum Boarder
Posts: 1918
Joined: 17.01.2011, 14:17

Re: How can I check if connection is active?

Post by marsupilami »

Hello,

select 1; will also not work on all databases. Firebird for example will expect a table name. Oracle does the same if I remember correctly. This is why the ping function in Zeos needs to be implemented in a driver specific fashion. It is just that for some drivers nobody cared to implement it. Contributions are welcome.
Best regards,

Jan
Fr0sT
Zeos Dev Team
Zeos Dev Team
Posts: 280
Joined: 08.05.2014, 12:08

Re: How can I check if connection is active?

Post by Fr0sT »

"SELECT 1" is non-standard and every DBMS has its own syntax for selecting a constant. Moreover this dummy select consumes more resources (starts/uses a transaction, creates resultset etc) than probably other driver-specific pinging methods. F.ex., FB pings by requesting DB info which doesn't require transaction and creating a resultset.
User avatar
aehimself
Zeos Dev Team
Zeos Dev Team
Posts: 765
Joined: 18.11.2018, 17:37
Location: Hungary

Re: How can I check if connection is active?

Post by aehimself »

Fr0sT wrote:Moreover this dummy select consumes more resources (starts/uses a transaction, creates resultset etc) than probably other driver-specific pinging methods. F.ex., FB pings by requesting DB info which doesn't require transaction and creating a resultset.
This. I wish I'd have this knowledge about all databases; what action will be the quickest / smallest impact! If I would; I would have written a solution before, not a workaround :(

The app I made was only used on MySQL, MSSQL and PostgreSQL at the end and all seemed to support the constant query just fine. Maybe it was not the most resource-friendly solution; but fortunately I implemented a timer which was reset after any database interaction. This way the dummy only had to execute when nothing had anything to do with the database for 2-3 minutes.
Delphi 12.1, Zeos 8 from latest GIT snapshot
Using:
- MySQL server 8.0.18; libmariadb.dll 3.3.8
- Oracle server 11.2.0, 12.1.0, 19.0.0; oci.dll 21.13
- MSSQL 2012, 2019; sybdb.dll FreeTDS_2435
- SQLite 3.45.2
Fr0sT
Zeos Dev Team
Zeos Dev Team
Posts: 280
Joined: 08.05.2014, 12:08

Re: How can I check if connection is active?

Post by Fr0sT »

If your solution is executed from time to time that's just fine; resource consumption is very low :). Dummy select as a concept is the most simple and general solution though on the implementation level it will require different syntax for different drivers. That's why ZConnection.Ping is implemented personally for each driver.
Post Reply