Android Question Using JdbcSQL to connect to MSSQL

mik21

Member
I am using jdbcSQL to connect MSSQL. There is problem in release mode with error NetworkOnMainThread. The solution is to use async methods (InitializeAsync, ExecQueryAsync).
I made a small test and have the same problem. Exactly it depends from number of returned rows. If result is 10 rows it is ok. If result is over 50, I have got the same error see error log:

Error log:
50. -> 1001002031      TF-601 telefax TOSHIBA                 
51. -> 1001001036      1360 kopírka TOSHIBA                   
sql$ResumableSub_tableresume (java line: 490)
android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1450)
    at java.net.SocketInputStream.read(SocketInputStream.java:169)
    at java.net.SocketInputStream.read(SocketInputStream.java:139)
    at java.io.DataInputStream.readFully(DataInputStream.java:198)
    at java.io.DataInputStream.readFully(DataInputStream.java:172)
    at net.sourceforge.jtds.jdbc.SharedSocket.readPacket(SharedSocket.java:850)
    at net.sourceforge.jtds.jdbc.SharedSocket.getNetPacket(SharedSocket.java:731)
    at net.sourceforge.jtds.jdbc.ResponseStream.getPacket(ResponseStream.java:477)
    at net.sourceforge.jtds.jdbc.ResponseStream.read(ResponseStream.java:114)
    at net.sourceforge.jtds.jdbc.ResponseStream.readLong(ResponseStream.java:345)
    at net.sourceforge.jtds.jdbc.TdsData.readData(TdsData.java:1078)
    at net.sourceforge.jtds.jdbc.TdsCore.tdsRowToken(TdsCore.java:3175)
    at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2433)
    at net.sourceforge.jtds.jdbc.TdsCore.getNextRow(TdsCore.java:805)
    at net.sourceforge.jtds.jdbc.JtdsResultSet.next(JtdsResultSet.java:611)
    at anywheresoftware.b4j.objects.SQL$ResultSetWrapper.NextRow(SQL.java:474)
    at parabens.vc.df.sql$ResumableSub_table.resume(sql.java:490)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:207)
    at anywheresoftware.b4a.BA$2.run(BA.java:387)
    at android.os.Handler.handleCallback(Handler.java:790)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6518)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
I tested a pair of queries and the error is depend of ammount returned bytes. If the result is "slim" I have got over 200 records, if the result is "fat" the crash is in 30 records.
If I use BeginTransaction there is no response.

Here is my code:
ExecQueryAsync:
Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    Log ("Start query")
    table
    Log ("End query")
End Sub

Private Sub table As ResumableSub
    Dim DriverClass As String ="net.sourceforge.jtds.jdbc.Driver"
    Dim JdbcUrl As String ="jdbc:jtds:sqlserver://" & "192.168.88.207" & "/" & "FA_CENTX"

    MSSQL.InitializeAsync("sql", DriverClass, JdbcUrl, "usr", "psw")
    Wait For sql_Ready (success As Boolean)
    If success = False Then
        Log(LastException)
        Return False
    End If
    'MSSQL.BeginTransaction
    Dim cmd As String
    cmd = "select top 100 DRCisTovaru, DROznacenie, DRMnozstvoDoklMJ, DRJednotkCena from DR"
    Dim SenderFilter As Object = MSSQL.ExecQueryAsync("SQL", cmd, Null)
    Wait For (SenderFilter) SQL_QueryComplete (success As Boolean, rs As JdbcResultSet)
    Dim i As Int = 1
    If success Then
        Do While rs.NextRow
            LogColor ($"${i}. -> ${rs.GetString("DRCisTovaru")} ${rs.GetString("DROznacenie")}"$, Colors.Blue)
            i = i + 1
        Loop
        LogColor("===== End ======", Colors.Magenta)
        rs.Close
        'MSSQL.TransactionSuccessful
        MSSQL.Close
        Return True
    Else
        Log(LastException)
        Return False
    End If

End Sub

Maybe I need to change some properties in coonection ? Does anybody have a clue ?
 
Top