B4J Question [jSQL] Problem connecting to the database

Similar threads

B4J Tutorial SQL Tutorial
B4J Code Snippet Opening MS Access databases (mdb)
B4A Tutorial [B4X] SQL with Wait For
Games [XUI2D] Example Pack
B4J Tutorial DBUtils Example

Omar Moreno

Member
Licensed User
Hi.
I have this problem
I have a WEBAPP that works very well with the SQL Server 2008 R2 database using the driver: mssql-jdbc-6.4.0.jre9.jar.
Sometimes the electrical fluid is cut, we create a task in the Windows Server to automatically start the WEBAPP on the server, but apparently the WEBAPP loads fast and can not find the SQL Server 2008 service.
We are testing with this code that it repeats five attempts of connection, but it stops in the attempt number 1:
They could give me some guidance that I am doing wrong.
Thank you.

B4X:
'Non-UI application (console / server application)
#Region Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
    #AdditionalJar: mssql-jdbc-6.4.0.jre9.jar
#End Region

Sub Process_Globals
    '
    Public  SQLServer As SQL
    Private IntentConectDataBase As Int = 1
    Private Conecto As Boolean = False
    '
End Sub

Sub AppStart (Args() As String)
    '
    Try
        '
        Do While IntentConectDataBase <= 5
            '
            Log($"IntentConectDataBase:  ${IntentConectDataBase}  [${DateTime.Date(DateTime.Now)} ${DateTime.Time(DateTime.Now)}]"$)
            '
            ConectarBaseSQL
            Wait For ConectarBaseSQL_Complete
            '
            IntentConectDataBase = IntentConectDataBase + 1
            '
        Loop
        '
        If Conecto = False Then
            Log($"No se puedo contar a la base de datos en ${IntentConectDataBase - 1} intentos...!!!"$)
        End If
        '
        StartMessageLoop
        '
    Catch
        Log($"AppStart: ${LastException}"$)
    End Try

End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Private Sub ConectarBaseSQL
    Try
        '
        Dim ServidorDB As String= "OMAR-PC-1"
        Dim InstanciaDB As String= "MSSQL2008R2E"
        '
        SQLServer.Initialize("com.microsoft.sqlserver.jdbc.SQLServerDriver" ,$"jdbc:sqlserver://${ServidorDB};instanceName=${InstanciaDB};databaseName=BD_Mensajero;Trusted_Connection=yes;user=user1;password=123456789"$)
        '
        Log("***")
        Log("SERVIDOR INICIADO EL: " & DateTime.Date(DateTime.Now) & " " & DateTime.Time(DateTime.Now))
        If SQLServer.IsInitialized Then
            Log("CONECTADO A LA BASE DE DATOS:  OK")
            '
            IntentConectDataBase = 5
            Conecto = True
            '
        Else
            Log("NO SE CONECTADO A LA BASE DE DATOS:  VERIFIQUE")
        End If
        Log("***")              
        '
    Catch
        Log($"Error in ConectarBaseSQL  [${DateTime.Date(DateTime.Now)} ${DateTime.Time(DateTime.Now)}]: ${LastException}"$)
    End Try
    '
    Sleep(10000)
    Log($"Pause 1 min ok  [${DateTime.Date(DateTime.Now)} ${DateTime.Time(DateTime.Now)}] "$)
    CallSubDelayed(Me,"ConectarBaseSQL_Complete")
    '
End Sub
 

OliverA

Expert
Licensed User
If you call Wait For in the AppStart of a Non-UI app, the app just stops. Think about it: When Wait For is encountered, the current method is "paused" and the method that called the current method continues executing. In your case, you are pausing and then exiting AppStart and with it, your application. In your case, move the Do While and the following If statement to another function/method and call that method before StartMessageLoop.
B4X:
Sub AppStart (Args() As String)
    '
    Try
        '
        TryConnectingToTheDB
        '
        StartMessageLoop
        '
    Catch
        Log($"AppStart: ${LastException}"$)
    End Try

End Sub

Sub TryConnectingToTheDB
     'Put the code you had previously in the AppStart method here
End Sub
 

Omar Moreno

Member
Licensed User
Thank you very much OliverA.

Modify the code according to your suggestion and now if it is working as required.
 
Top