Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Datalogger auto-REconnection for PostgreSQL
#1
Hi all, i have a simple QM application that needs to store data 24h/24 on a PostGreSQL.
I have an ODBC entry in system DSN named POS_ENM.
Connection between my QM application and PostGreSQL work very well but i like to try to autoreconnect my application to database if the server goes down.

Function dlg_autoconnect
Code:
Copy      Help
\Dialog_Editor
function# hDlg message wParam lParam
if(hDlg) goto messages

Database+ db
str result
;BEGIN DIALOG
;0 "" 0x90C80AC8 0x0 0 0 223 135 "Dialog"
;3 Edit 0x54030080 0x200 68 18 96 14 "cstatus"
;4 Edit 0x54030080 0x200 68 38 96 14 "dsn"
;1 Button 0x54030001 0x4 120 116 48 14 "OK"
;2 Button 0x54030000 0x4 170 116 48 14 "Cancel"
;END DIALOG
;DIALOG EDITOR: "" 0x2030407 "" "" "" ""

str+ dbsource
str controls = "3 4"
str e3cst e4dsn
if(!ShowDialog("dlg_autoconnect" &dlg_autoconnect &controls)) ret

ret
;messages
sel message
,case WM_INITDIALOG
,dbsource.from("DSN=POS_ENM")
,db.Open(dbsource)
,,err
,,,out _error.description
,SetTimer hDlg 1 1000 0                        ;;Start a timer to check if database is connected
,case WM_TIMER
,if !db                                        ;;Verify if database is connected
,,db.Open(dbsource)                        ;;If not connected try to reconnect
,,err
,,,out _error.description
,,result.from("Not Connect")
,,result.setwintext(id(3 hDlg))
,,dbsource.setwintext(id(4 hDlg))
,else
,,result.from("Connect")
,,result.setwintext(id(3 hDlg))
,,dbsource.setwintext(id(4 hDlg))

,case WM_DESTROY
,KillTimer hDlg 1
,case WM_COMMAND goto messages2
ret
;messages2
sel wParam
,case IDOK
,case IDCANCEL
ret 1


is there a way to check to do this?
Thank's


Alessandro
#2
I forgot to write that my code work well for one times if QM application start with server down.
In this case my application connect automatically when server goes up, but when server goes down whith my application in run the exception is not caught.

Alessandro
#3
Try db.conn.State, maybe it is different when server is down. Or will be error. Or try other db.conn functions. Or even db.Query. Should be error if server is down. Or even try to Open temporarily with other Database variable.

Code:
Copy      Help
,case WM_TIMER
,out db.conn.State
In any case, I recommend to measure speed of database testing code. If too slow, try other ways, or make bigger timer period. Use function perf (microseconds) or timeGetTime (milliseconds).

I would also try db.conn events. For example Disconnect. Read about COM events in QM help.

Also, if possible, don't use global variable for Database. Change declaration from Database+ db to Database- db.


Forum Jump:


Users browsing this thread: 1 Guest(s)