How to reliably detect a database is not connected ?

Posted by cverbiest on 28-Apr-2016 06:29

The connected function is not reliable. It returns false when a database is not connected or when it is connected but a disconnect is pending.

My sample code results in

sp2k is NOT CONNECTED, trying to connect

followed by

Error : ** Warning -- database sp2k is already connected. (1012)

Is there a way to detect this situation, other than detecting the warning error ?

Is there a way to check the database parameters for that db ? If the parameters of the connected sp2k are the same as the sp2k I'm trying to connect to now I don't really mind the connect fails, but if I connect a different sp2k this condition should be fatal.

I'd rather not start to maintain my own collection of connected databases with their parameters but currently it's the only solution I see.

create a sp2k sports2000 db to run the sample.

/* main.p */
def var hlib as handle.

connect sp2k -1.
run custlib.p persistent set hlib.
disconnect sp2k.

if connected("sp2k")
then do:
    message "sp2k is connected, ok".
end.
else do:
    message "sp2k is NOT CONNECTED, trying to connect" view-as alert-box.
    connect sp2k -1.
end.

--

/* custlib.p */
function custinfo returns character (input iCustNum as integer).
    find customer where customer.custnum = iCustNum no-lock.
    return substitute("&1 &2", customer.CustNum, customer.Name).
end function.

see also

http://knowledgebase.progress.com/articles/Article/19977?q=connected+function&l=en_US&fs=Search&pn=1

Apparenly connected = true isn't reliable either, see http://knowledgebase.progress.com/articles/Article/P147806?q=connected+function&l=en_US&fs=Search&pn=1

I find it strange that neither PKB mention a bug number.

Posted by cverbiest on 28-Apr-2016 07:02

I found a solution to my problem, dbparam helps, but not connecting because dbparam ne ? results in the connection going away at the end of the execution when custlib.p is cleaned up by the procedure editor.

A new connect statement ensures the database remains connected.

If the new connect tries to connect to a different db you get a second errormessage 

Database physical names sp2k2 and sp2k do not match.  No connect. (1018)

I can solve my issue by catching the error and checking for the error numbers.

def var hlib as handle.

if not connected("sp2k") then connect sp2k.
run custlib.p persistent set hlib.
message dynamic-function("custinfo" in hlib, 1) view-as alert-box.
disconnect sp2k.

if connected("sp2k")
then do:
    message "sp2k is connected, ok" view-as alert-box.
end.
else do on error undo, leave:
    connect sp2k -ld sp2k.
    catch e as progress.lang.error:
         if e:getmessagenum(1) = 1012 and e:getmessagenum(2) = 0
         then message "Same database already connected : OK" view-as alert-box.
         else message e:getmessage(1) skip e:getmessage(2)  view-as alert-box.
    end catch.
end.

All Replies

Posted by cverbiest on 28-Apr-2016 06:36

I came up with a possible answer when investigating my second sub-question.

(dbparam("sp2k")  ne ?)

seems to be an alternative to connected("sp2k") but I'm not sure how reliable that is. In my testcase It yield correct results.

Posted by cverbiest on 28-Apr-2016 07:02

I found a solution to my problem, dbparam helps, but not connecting because dbparam ne ? results in the connection going away at the end of the execution when custlib.p is cleaned up by the procedure editor.

A new connect statement ensures the database remains connected.

If the new connect tries to connect to a different db you get a second errormessage 

Database physical names sp2k2 and sp2k do not match.  No connect. (1018)

I can solve my issue by catching the error and checking for the error numbers.

def var hlib as handle.

if not connected("sp2k") then connect sp2k.
run custlib.p persistent set hlib.
message dynamic-function("custinfo" in hlib, 1) view-as alert-box.
disconnect sp2k.

if connected("sp2k")
then do:
    message "sp2k is connected, ok" view-as alert-box.
end.
else do on error undo, leave:
    connect sp2k -ld sp2k.
    catch e as progress.lang.error:
         if e:getmessagenum(1) = 1012 and e:getmessagenum(2) = 0
         then message "Same database already connected : OK" view-as alert-box.
         else message e:getmessage(1) skip e:getmessage(2)  view-as alert-box.
    end catch.
end.

This thread is closed