Android Question [Solved What do I need to modify on the client side for jRDC2 Multi to work?

Sergio Castellari

Active Member
Licensed User
Situation:

I am currently developing an APP where I use jRDC2 (with their respective jRDC2.bas and DBRequestManager.bas in B4A) and it works PERFECT.

But I need to connect with the same jRDC2 Server to multiple Databases.
@Erel I published last year this version of jRDC2 Multi that supports multiple Databases:
https://www.b4x.com/android/forum/threads/mysql-connector-for-b4i.116617/#post-729095
At the time, ask about the changes to be made in the local APP (B4A) to be able to use jRDC2 Multi.

My ignorance does not allow me to realize what I must modify in jRDC2.bas / DBRequestManager.bas so that I can send from the APP in B4A, the Database that I want to use in jRDC2 Multi.

Greetings
 
Solution
Let say, I have 2 databases named "db1" and "db2".
So my JRDC server:
B4X:
Sub AppStart (Args() As String)
    srvr.Initialize("")
    Connectors = srvr.CreateThreadSafeMap
    Dim dbs As List = Array("db1", "db2") 'read from a file
    .
    .
    .
    Log($"jRDC is running (version = $1.2{VERSION})"$)
    StartMessageLoop
End Sub

At client side, add modify line #3 and #4:
DBRequestManager:
Private Sub SendJob(j As HttpJob, Data() As Byte, Tag As Object, Method As String) As HttpJob
    j.Tag = Tag
    If link.Contains("?") Then link = link & "&" Else link = link & "?"
    j.PostBytes(link & "method=" & Method , Data)
    Return j
End Sub

In "request" page or activity:
B4X:
Sub GetRecord (UseDb As String)
    Dim req As DBRequestManager =...

aeric

Expert
Licensed User
Longtime User
I think you can pass a parameter to the server. The server can read the parameter then change the connection to another database.
 
Upvote 1

aeric

Expert
Licensed User
Longtime User
Let say, I have 2 databases named "db1" and "db2".
So my JRDC server:
B4X:
Sub AppStart (Args() As String)
    srvr.Initialize("")
    Connectors = srvr.CreateThreadSafeMap
    Dim dbs As List = Array("db1", "db2") 'read from a file
    .
    .
    .
    Log($"jRDC is running (version = $1.2{VERSION})"$)
    StartMessageLoop
End Sub

At client side, add modify line #3 and #4:
DBRequestManager:
Private Sub SendJob(j As HttpJob, Data() As Byte, Tag As Object, Method As String) As HttpJob
    j.Tag = Tag
    If link.Contains("?") Then link = link & "&" Else link = link & "?"
    j.PostBytes(link & "method=" & Method , Data)
    Return j
End Sub

In "request" page or activity:
B4X:
Sub GetRecord (UseDb As String)
    Dim req As DBRequestManager = CreateRequest(UseDb)
    Dim cmd As DBCommand = CreateCommand("select_message", Null)
    Wait For (req.ExecuteQuery(cmd, 0, Null)) JobDone(j As HttpJob)
    If j.Success Then
        req.HandleJobAsync(j, "req")
        Wait For (req) req_Result(res As DBResult)
        'work with result
        req.PrintTable(res)
    Else
        Log("ERROR: " & j.ErrorMessage)
    End If
    j.Release
End Sub

Sub CreateRequest (Db As String) As DBRequestManager
    Dim Req As DBRequestManager
    Req.Initialize(Me, rdcLink & "?DBName=" & Db)
    Return Req
End Sub

Then call like this:
B4X:
Sub Button1_Click
    GetRecord("db1")
End Sub

Private Sub Button2_Click
    GetRecord("db2")
End Sub
 
Upvote 2
Solution

Sergio Castellari

Active Member
Licensed User
@aeric
Thanks a lot!!!

With your contribution I was able to solve the situation, although I made a small adaptation to indicate the Database through a public variable and thus not modify the calls to the commands. This can be enhanced to further hide the DB name.

CreateRequest:
Sub CreateRequest As DBRequestManager
  'Por cada solicitud, se crea un nuevo DBRequest...
  Dim Req As DBRequestManager
  Req.Initialize(Me, rdcLink & "?DBName=" & Main.pDBName )   'Aqui va la DBName a utilizar!!!
  Return Req
End Sub

Main:
Sub Process_Globals
    'Variables PUBLICAS del Sistema/Cliente [Se DEBEN cambiar según mi cliente]
    Public pEmpresa = "SDC Soluciones Informáticas" As String
    Public pDBName = "dbnombre"                        As String

DbRequestManager:
Private Sub SendJob(j As HttpJob, Data() As Byte, Tag As Object, Method As String) As HttpJob
    j.Tag = Tag
    If link.Contains("?") Then link = link & "&" Else link = link & "?"
    j.PostBytes(link & "method=" & Method , Data)
    Return j
End Sub

On the Server side, using:
https://www.b4x.com/android/forum/threads/mysql-connector-for-b4i.116617/#post-729095

Thank you for understanding my need and helping me. Apparently, it was not something complex, but my ignorance did not allow me to know what to modify.
A gigantic hug!
Have a beautiful weekend!
 
Upvote 0
Top