Testing for a database connection with PQConnection lazarus / 16-Databases ---------------------------------------------------------------------- timcs · 2013-03-26 09:36 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 · 2013-03-26 10:09 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 · 2013-03-26 11:00 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. ---------------------------------------------------------------------- timcs · 2013-03-26 12:14 Quote from: BigChimp on March 26, 2013, 12:00:36 pmHow 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 · 2013-03-27 06:06 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 · 2013-03-27 12:19 Quote from: Lacak2 on March 27, 2013, 07:06:21 amMay 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 · 2013-04-10 07:20 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 · 2013-04-10 08:29 Yes I'd think it needs to be a new post. Also, it helps to ask what you want to know ---------------------------------------------------------------------- timcs · 2013-04-10 08:54 Quote from: BigChimp on April 10, 2013, 10:29:18 amYes 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 · 2013-04-10 09:17 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) ---------------------------------------------------------------------- timcs · 2013-04-10 12:47 Quote from: BigChimp on April 10, 2013, 11:17:26 amWell.. 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.