Recent

Author Topic: Testing for a database connection with PQConnection  (Read 7588 times)

timcs

  • Full Member
  • ***
  • Posts: 213
Testing for a database connection with PQConnection
« on: March 26, 2013, 10:36:54 am »
Hi

  Within my program I am trying to test for a connection based on three different IP addresses . My thoughts are to use the try Expect on method but how do I do this for three attempts?

Thanks

Tim

timcs

  • Full Member
  • ***
  • Posts: 213
Re: Testing for a database connection with PQConnection
« Reply #1 on: March 26, 2013, 11:09:56 am »
Would the following work? :
Code: [Select]
try
             PQConnection1.HostName :='1.1.1.1';
             messagedlg('Trying to connect to 1.1.1.1 ',mtinformation,[mbok],0);
             SQlquery1.Active:= true;
            Except on E: EDatabaseError do
            begin
               messagedlg('Could not connect to 1.1.1.1, Trying to connect to 1.1.1.2 ',mtinformation,[mbok],0);
               SQLquery1.Active:=false;
               PQConnection1.HostName :='1.1.1.2';
               try
                   SQlquery1.Active:= true;
                Except on E: EDatabaseError do
                 begin
                    messagedlg('Could not connect to 1.1.1.2, Trying to connect to 1.1.1.3',mtinformation,[mbok],0);
                    SQLquery1.Active:=false;
                    PQConnection1.HostName :='1.1.1.3';
                    try
                         SQlquery1.Active:= true;
                        Except on E: EDatabaseError do
                        begin
                               messagedlg('Could not connect to 1.1.1.3, tried all three IP addresses - no connection found - closing program  ',mtinformation,[mbok],0);
                               SQLquery1.Active:=false;
                               form1.close;
                               application.Terminate;
                        end;

                    end;

                    end;
                 end;
                end;
            end;
                                                       

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Testing for a database connection with PQConnection
« Reply #2 on: March 26, 2013, 12:00:36 pm »
How about using a loop with a counter and a boolean flag that signals success? Much more readable and you can even try 4 times without too many changes ;)

Also, you can catch specific errors (E.errorcode or something is in recent FPC versions for PostgreSQL; hope it's in the Laz/FPC version your'e using). This will let you distinguish between e.g. incorrect username/password and no server at the other end.
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

timcs

  • Full Member
  • ***
  • Posts: 213
Re: Testing for a database connection with PQConnection
« Reply #3 on: March 26, 2013, 01:14:11 pm »
How about using a loop with a counter and a boolean flag that signals success? Much more readable and you can even try 4 times without too many changes ;)

Also, you can catch specific errors (E.errorcode or something is in recent FPC versions for PostgreSQL; hope it's in the Laz/FPC version your'e using). This will let you distinguish between e.g. incorrect username/password and no server at the other end.

I have tried the above and it does work. I do have another problem though, the reason for the three IP address check , although the above example IPs are dummy numbers the actual reason is down to three sub nets on the network. However when tested on our sub net it is quick but for some reason on another sub net it takes 10 seconds to respond and I do not know how to get around this. This is not a sub net issue as such because connecting to the IP address through a database manager responds much quicker.

Is there anything I can do to speed this up within Lazarus ?

Lacak2

  • Guest
Re: Testing for a database connection with PQConnection
« Reply #4 on: March 27, 2013, 07:06:21 am »
May be, that it is related to some "timeout"
Look f.e at "connect_timeout" http://www.postgresql.org/docs/9.0/interactive/libpq-connect.html

timcs

  • Full Member
  • ***
  • Posts: 213
Re: Testing for a database connection with PQConnection
« Reply #5 on: March 27, 2013, 01:19:29 pm »
May be, that it is related to some "timeout"
Look f.e at "connect_timeout" http://www.postgresql.org/docs/9.0/interactive/libpq-connect.html

Lack2 - I will read this and does this mean that it would only be slow for use with lazarus? what is strange is at work we have three sub nets , the one we use in our office the connection from my program is instant, the one using in another office is taking about 10 to 20 seconds before the connection finishes.

I have added a message to the program to see if the time is down to the postgres database or when the program connects to the dbase file over a UNC path. Neither are slow on the sub net that I am on but may be one or both are slow on the other sub net.

timcs

  • Full Member
  • ***
  • Posts: 213
Re: Testing for a database connection with PQConnection
« Reply #6 on: April 10, 2013, 09:20:32 am »
Well I have now been asked to do the following for this application at the point of install as we have two main sub nets at work.

I need to create an installation wizard that detects the PCs IP address and then copies the appropriate version that runs on that sub net . Furthermore check for new versions on running that would be stored over the network on a UNC path and copy the program over at that point

If this needs to be a new post let me know and I will do so.


Thanks

TimCS

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Testing for a database connection with PQConnection
« Reply #7 on: April 10, 2013, 10:29:18 am »
Yes I'd think it needs to be a new post.

Also, it helps to ask what you want to know ;)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

timcs

  • Full Member
  • ***
  • Posts: 213
Re: Testing for a database connection with PQConnection
« Reply #8 on: April 10, 2013, 10:54:17 am »
Yes I'd think it needs to be a new post.

Also, it helps to ask what you want to know ;)

No problem - should I post it under general ? it is a query on IP address information and creating an installation wizard which can copy files .

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: Testing for a database connection with PQConnection
« Reply #9 on: April 10, 2013, 11:17:26 am »
Well.. I'd split it up in 2 questions, one on the networking thread, and the other in general...'

Makes for less confusion when answering them (presuming these issues are quite separate)
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

timcs

  • Full Member
  • ***
  • Posts: 213
Re: Testing for a database connection with PQConnection
« Reply #10 on: April 10, 2013, 02:47:17 pm »
Well.. I'd split it up in 2 questions, one on the networking thread, and the other in general...'

Makes for less confusion when answering them (presuming these issues are quite separate)

Ok will do , they relate to the same project that I am working on but different parts of this project.

 

TinyPortal © 2005-2018