Set timeout for the database connect statement.

Sundreamz

New Member
Hi Friends,


I am trying to connect to a database named "archive". This database is available in some hosts and not available in others.
The connect statement below tries to connect to the "archive" database and returns the message as "connected" if the database connetion happens successfully.
If the the connect statement is not able to connect to the database for some hosts then it hangs for a long time then terminates with message ** Could not connect to server for database <DATABASE>, errno <ERRNO>.
It does not show it the database is connected or not with this scenario.

Now My question: Is it possible to set a timeout CONNECT Statement.
i.e. if the connect statement runs for a specified period of time then stop connecting to the dabase. I don want the system to hand for a long time bcos of connect statement.


def var mDBConnectString as char.
mDBConnectString = "archive" + " -S " + "ta1-arch1" + " -H " + "hostname".
message "connecting" view-as alert-box.
CONNECT VALUE(mDBConnectString).
IF CONNECTED ('archive') then message "connected" view-as alert-box.
ELSE message "not connected" view-as alert-box.

Thanks In Advance,
Rgds,
Sandeep
 

TomBascom

Curmudgeon
There's nothing that you can do with the CONNECT statement.

You can, however, wrap it with various tests and not attempt to CONNECT if it is obviously going to fail. Some possible tests (not all may be relevant and you may think of others) might include:

1) Using FILE-INFO to see if the target db exists.
2) Using proutil holder to see if a server is up.
3) Using telnet or curl to attempt to connect to the socket.
 

sphipp

Member
Try

Code:
CONNECT VALUE(mDBConnectString) NO-ERROR.
IF CONNECTED ('archive') then message "connected" view-as alert-box.
ELSE message "not connected" view-as alert-box.

to check if the connect statement has worked.

It might be worth checking that the service and host are set up correctly before you connect.

Try something like

Code:
function f_infile returns logical (inp_word as char, inp_file as char):
  def var temp_command as char no-undo.
  def var temp_line as char no-undo.
  case inp_file:
    when "services" then case opsys:
    when "unix" then inp_file = "/etc/services".
    when "dos" or when "win32" then
      inp_file = "C:\WINDOWS\SYSTEM32\DRIVERS\ETC\SERVICES".
    end case.
    when "hosts" then case opsys:
      when "unix" then inp_file = "/etc/hosts".
      when "dos" or when "win32" then
        inp_file = "C:\WINDOWS\SYSTEM32\DRIVERS\ETC\HOSTS".
    end case.
  end case.
  case opsys:
    when "unix" then temp_command = "grep -i".
    when "dos" or when "win32" then temp_command = "findstr /i ".
  end case.
  temp_command = 
    temp_command + " """ + inp_word + """ " + inp_file + " > grep.temp".
  unix silent value (temp_command).
  repeat on error undo, leave:
    input from grep.temp.
    import unformatted temp_line.
    input close.
    leave.
  end.
  os-delete grep.temp.
  if trim(temp_line) = "" then return no. else return yes.
end function.

 
message
  f_infile ("ta1-arch1","services")
  f_infile ("hostname","hosts")
  view-as alert-box.

Also, are you using "hostname" as your host name or should hostname be a variable containing the host name?
 
Top