Android Question MSMariaDB/MSMySQL check if database is connected and try again if not!

Magma

Expert
Licensed User
Longtime User
Hi there i was reading this thread about check_connection but i can't find the point i want.

When the tablet/phone is disconnected from wifi and want to connects at db ofcourse can't connected... I am having a label that saying is not connected at internet but i wanna app try every 15 seconds to get connected so i believed that i must use the following:

Somewhere read that check_connection is like a ping and if not connected at the db will try again..

B4X:
Sub Timer2_Tick
    db.check_connection
end sub

but getting this:
MySQL Database not connected!
ReConnecting
main_timer2_tick (java line: 867)
java.lang.NullPointerException: Attempt to invoke interface method 'void java.sql.Connection.close()' on a null object reference
at de.donmanfred.b4a.MariaDB.auto_reconnect(MariaDB.java:1097)
at de.donmanfred.b4a.MariaDB.check_connection(MariaDB.java:1142)
at mvt.client.main._timer2_tick(main.java:867)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.objects.Timer$TickTack.run(Timer.java:105)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5624)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754)

What's the best solution to try to connect again to the db if not connected from the start (because device was not connected to the internet) ?
 

DonManfred

Expert
Licensed User
Longtime User
check_connection should only be used after a sucessfully connect to do something like a ping.
Make sure you are connected before you start the timer
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
I ve found a solution (i think is not the best because at start always trying - not know if will make that a problem)...

So now if someone open app and server is down or wifi/3g/4g off - app will always try until connected...

If "data" enabled (2g/3g/4g/wifi) or server is up app will connected to db and do the job..

I don;t know if the following code will always work or will give an error or something... anyone can help optimize it ?


B4X:
Sub Activity_Create(FirstTime As Boolean)

'i know i can use firsttime - this is an example so don't use all  space of community
SERVERON=False
'not connected yet...
    connect2db
end sub

Sub connect2db
    db.DisableReconnect
'let's try
    db.Initialize("MySQL","myipormyhost:myport","myusername","mypass","mydatabase") 
   
End Sub

Sub MySQL_Status(Connected As Boolean, ReConnecting As Boolean, RetriesLeft As Int)
    Log($"Connected = ${Connected}"$)
    If Connected = True Then
'if connected then open table...
        db.QueryASync("select.....blahblah;","nameofjob")
'enable reconnect!!!!
        db.EnableReconnect
        SERVERON=True
        Else
'try reconnection
        SERVERON=False
        connect2db
    End If
End Sub
 
Upvote 0
Top