B4J Question JRDC 2 Multi database connection problem

dragonguy

Active Member
Licensed User
Longtime User
i like to connect to two database in save server.username and password also same. i spent most of many hours make it work, but i'm fail. Hope someone can give me a some advice.

here is my code of server side:
main:
B4X:
'Non-UI application (console / server application)
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region
Sub Process_Globals
    Public srvr As Server
    Public rdcConnectorA As RDCConnector
    Public rdcConnectorB As RDCConnector
    Public const VERSION As Float = 2.1
    Type DBCommand (Name As String, Parameters() As Object)
    Type DBResult (Tag As Object, Columns As Map, Rows As List)
End Sub

Sub AppStart (Args() As String)
    srvr.Initialize("")
    rdcConnectorA.Initialize
    rdcConnectorB.Initialize
    srvr.Port = 12345
    srvr.AddHandler("/rdc", "RDCHandler", False)
    srvr.Start
    Log($"jRDC is running (version = $1.2{VERSION}) Port = ${srvr.Port}"$)
    StartMessageLoop
End Sub

RDCHandler:
B4X:
Sub Class_Globals
    Private connector As RDCConnector
    Private connectors As Map = CreateMap("a":Main.rdcConnectorA,"b":Main.rdcConnectorB)
End Sub

Public Sub Initialize
   
End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)
    Dim start As Long = DateTime.Now
    Dim q As String
    Dim in As InputStream = req.InputStream
    Dim method As String = req.GetParameter("method")
    connector=connectors.Get(req.GetParameter("database")) '<==here i get the null value
    Dim con As SQL
    Try
       
        con = connector.GetConnection
        If method = "query2" Then
            q = ExecuteQuery2(con, in, resp)
        Else if method = "batch2" Then
            q = ExecuteBatch2(con, in, resp)
        Else
            Log("Unknown method: " & method)
            resp.SendError(500, "unknown method")
        End If
    Catch
        Log(LastException)
        resp.SendError(500, LastException.Message)
    End Try
    If con <> Null And con.IsInitialized Then con.Close
    Log($"Command: ${q}, took: ${DateTime.Now - start}ms, client=${req.RemoteAddress}"$)
End Sub

Private Sub ExecuteQuery2 (con As SQL, in As InputStream,  resp As ServletResponse) As String
    Dim ser As B4XSerializator
    Dim m As Map = ser.ConvertBytesToObject(Bit.InputStreamToBytes(in))
    Dim cmd As DBCommand = m.Get("command")
    Dim limit As Int = m.Get("limit")
    Dim rs As ResultSet = con.ExecQuery2(connector.GetCommand(cmd.Name), cmd.Parameters)
    If limit <= 0 Then limit = 0x7fffffff 'max int
    Dim jrs As JavaObject = rs
    Dim rsmd As JavaObject = jrs.RunMethod("getMetaData", Null)
    Dim cols As Int = rs.ColumnCount
    Dim res As DBResult
    res.Initialize
    res.columns.Initialize
    res.Tag = Null 'without this the Tag properly will not be serializable.
    For i = 0 To cols - 1
        res.columns.Put(rs.GetColumnName(i), i)
    Next
    res.Rows.Initialize
    Do While rs.NextRow And limit > 0
        Dim row(cols) As Object
        For i = 0 To cols - 1
            Dim ct As Int = rsmd.RunMethod("getColumnType", Array(i + 1))
            'check whether it is a blob field
            If ct = -2 Or ct = 2004 Or ct = -3 Or ct = -4 Then
                row(i) = rs.GetBlob2(i)
            Else if ct = 2 Or ct = 3 Then
                row(i) = rs.GetDouble2(i)
            Else
                row(i) = jrs.RunMethod("getObject", Array(i + 1))
            End If
        Next
        res.Rows.Add(row)
    Loop
    rs.Close
    Dim data() As Byte = ser.ConvertObjectToBytes(res)
    resp.OutputStream.WriteBytes(data, 0, data.Length)
    Return "query: " & cmd.Name
End Sub

Private Sub ExecuteBatch2(con As SQL, in As InputStream, resp As ServletResponse) As String
    Dim ser As B4XSerializator
    Dim m As Map = ser.ConvertBytesToObject(Bit.InputStreamToBytes(in))
    Dim commands As List = m.Get("commands")
    Dim res As DBResult
    res.Initialize
    res.columns = CreateMap("AffectedRows (N/A)": 0)
    res.Rows.Initialize
    res.Tag = Null
    Try
        con.BeginTransaction
        For Each cmd As DBCommand In commands
            con.ExecNonQuery2(connector.GetCommand(cmd.Name), _
                cmd.Parameters)
        Next
        res.Rows.Add(Array As Object(0))
        con.TransactionSuccessful
    Catch
        con.Rollback
        Log(LastException)
        resp.SendError(500, LastException.Message)
    End Try
    Dim data() As Byte = ser.ConvertObjectToBytes(res)
    resp.OutputStream.WriteBytes(data, 0, data.Length)
    Return $"batch (size=${commands.Size})"$
End Sub

here is RDCConnector:
B4X:
Sub Class_Globals
    Private pool As ConnectionPool
    Private DebugQueries As Boolean
    Private commands As Map
    Public serverPort As Int
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    Dim config As Map = LoadConfigMap
    pool.Initialize(config.Get("DriverClass1"), config.Get("JdbcUrl1"), config.Get("User1"), _
    config.Get("Password1"))   
#if DEBUG
    DebugQueries = True
#else
    DebugQueries = False
#end if
    serverPort = config.Get("ServerPort")
    LoadSQLCommands(config)
End Sub

Private Sub LoadConfigMap As Map
    Return File.ReadMap(File.DirAssets, "config.properties")
End Sub

Public Sub GetCommand(Key As String) As String
    If commands.ContainsKey("sql." & Key) = False Then
        Log("*** Command not found: " & Key)
    End If
    Return commands.Get("sql." & Key)
End Sub

Public Sub GetConnection As SQL
    If DebugQueries Then LoadSQLCommands(LoadConfigMap)
    Return pool.GetConnection
End Sub

Private Sub LoadSQLCommands(config As Map)
    Dim newCommands As Map
    newCommands.Initialize
    For Each k As String In config.Keys
        If k.StartsWith("sql.") Then
            newCommands.Put(k, config.Get(k))
        End If
    Next
    commands = newCommands
End Sub

here is my config.properties
B4X:
#Lines starting with '#' are comments.
#Backslash character at the end of line means that the command continues in the next line.

#DATABASE CONFIGURATION
DriverClass1=com.mysql.jdbc.Driver
JdbcUrl1=jdbc:mysql://localhost:12345/data1?useSSL=false
User1=user1
Password1=pass1
DriverClass2=com.mysql.jdbc.Driver
JdbcUrl2=jdbc:mysql://localhost:12345/data2?useSSL=false
User2=user1
Password2=pass1

#Java server port
ServerPort=19980

#example of MS SQL Server configuration:
#DriverClass=net.sourceforge.jtds.jdbc.Driver
#JdbcUrl=jdbc:jtds:sqlserver://<server address>/<database>

#example of postegres configuration:
#JdbcUrl=jdbc:postgresql://localhost/test
#DriverClass=org.postgresql.Driver

#SQL COMMANDS
sql.select_checking_internet=SELECT value FROM tbl_internet_checking

and the client side code:
B4X:
Public Sub ExecuteQuery(Command As DBCommand, Limit As Int, Tag As Object,database As String)
    Dim ser As B4XSerializator
    Dim data() As Byte = ser.ConvertObjectToBytes(CreateMap("command": Command, "limit": Limit,  "version": VERSION))
    SendJob(data, Tag, "query2",database)
End Sub

Private Sub SendJob(Data() As Byte, Tag As Object, Method As String,database As String)
    Dim j As HttpJob
    j.Initialize("DBRequest", mTarget)
    j.Tag = Tag
    j.PostBytes(link & "?method=" & Method & "&database=" & database, Data)
End Sub

how can make it work?
please help me, thanks.
 

dragonguy

Active Member
Licensed User
Longtime User
When put log, I get my database name from client side. But the connector get null.
 
Last edited:
Upvote 0

dragonguy

Active Member
Licensed User
Longtime User
B4X:
Waiting for debugger to connect...
Program started.
2016-06-09 23:55:35.841:INFO::main: Logging initialized @5201ms
Jun 09, 2016 11:55:36 PM com.mchange.v2.log.MLog <clinit>
INFO: MLog clients using java 1.4+ standard logging.
Jun 09, 2016 11:55:38 PM com.mchange.v2.c3p0.C3P0Registry banner
INFO: Initializing c3p0-0.9.2.1 [built 20-March-2013 11:16:28 +0000; debug? true; trace: 10]
2016-06-09 23:55:39.802:INFO:oejs.Server:main: jetty-9.3.z-SNAPSHOT
2016-06-09 23:55:40.298:INFO:oejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@1e88b3c{/,file:///C:/Users/JEFF/Desktop/TCP%20NFC%20Location/B4J%20Working%20Data%20Analize/Objects/www,AVAILABLE}
2016-06-09 23:55:40.322:INFO:oejs.AbstractNCSARequestLog:main: Opened C:\Users\user\Desktop\TCP NFC Location\B4J Working Data Analize\Objects\logs\b4j-2016_06_09.request.log
2016-06-09 23:55:40.994:INFO:oejs.ServerConnector:main: Started ServerConnector@66f30998{HTTP/1.1,[http/1.1]}{0.0.0.0:19980}
2016-06-09 23:55:40.995:INFO:oejs.Server:main: Started @10363ms
Emulated network latency: 100ms
jRDC is running (version = 2.1) Port = 12345
data1
null
(NullPointerException) java.lang.NullPointerException
Command: , took: 37ms, client=192.168.0.117
data1
null
(NullPointerException) java.lang.NullPointerException
Command: , took: 2ms, client=192.168.0.117
 
Upvote 0

dragonguy

Active Member
Licensed User
Longtime User
I'm not sure why connectors is null. However the database name is incorrect. It should be a or b.

i just dont understand about this
B4X:
Replace rdcConnector1 with rdcConnectorA and rdcConnectorB.
Each one of them will point to a different database (you will need to take care of loading the correct configuration file).
how to point to correct configuration file?
 
Upvote 0
i follow @gragonguy tutorial above, my error on server are different :

<code>
2016-08-09 17:51:09.834:INFO::main: Logging initialized @471ms
Aug 09, 2016 5:51:09 PM com.mchange.v2.log.MLog <clinit>
INFO: MLog clients using java 1.4+ standard logging.
Aug 09, 2016 5:51:10 PM com.mchange.v2.c3p0.C3P0Registry banner
INFO: Initializing c3p0-0.9.2.1 [built 20-March-2013 11:16:28 +0000; debug? true; trace: 10]
2016-08-09 17:51:11.007:INFO:eek:ejs.Server:main: jetty-9.3.z-SNAPSHOT
2016-08-09 17:51:11.087:INFO:eek:ejsh.ContextHandler:main: Started o.e.j.s.ServletContextHandler@7c16905e{/,file:///C:/PerfLogs/jRDC2/Objects/www,AVAILABLE}
2016-08-09 17:51:11.092:INFO:eek:ejs.AbstractNCSARequestLog:main: Opened C:\PerfLogs\jRDC2\Objects\logs\b4j-2016_08_10.request.log
2016-08-09 17:51:11.297:INFO:eek:ejs.ServerConnector:main: Started ServerConnector@1ed4004b{HTTP/1.1,[http/1.1]}{0.0.0.0:17178}
2016-08-09 17:51:11.298:INFO:eek:ejs.Server:main: Started @1945ms
jRDC is running (version = 2.1) Port = 17178
(NullPointerException) java.lang.NullPointerException
Command: , took: 68ms, client=180.253.90.122
mitraibka
(MyMap) {a=[vv5=(ComboPooledDataSource) com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 20000, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1lm87j9ivm13yyti2iet|3d012ddd, debugUnreturnedConnectionStackTraces -> false, descrip...
, vv0=17178, vvv1=null], b=[vv5=(ComboPooledDataSource) com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 20000, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1lm87j9ivm13yyti2iet|c39f790, debugUnreturnedConnectionStackTraces ->...
, vv0=17178, vvv1=null]}
</code>
 
Upvote 0

A Hasbullah Huda

Member
Licensed User
please help,

i tried to configure jRDC2 using multi database. but still confused
maybe i'm not really "understand" about RDC works, but i need these tutorial.
i followed instruction (by @dragonguy) above, but i don't know how to create 2 instance jRDC
i'm sorry, but please help

thx alot
 
Upvote 0

dragonguy

Active Member
Licensed User
Longtime User
i share my code here, each database need a port to connect. Now example for two database.

first we need two config file, like below:
configA.properties
B4X:
#Lines starting with '#' are comments.
#Backslash character at the end of line means that the command continues in the next line.

#DATABASE CONFIGURATION
DriverClass=com.mysql.jdbc.Driver
JdbcUrl=jdbc:mysql://192.168.0.1:3306/testdatabaseA??characterEncoding=utf8
User=user
Password=password

#Java server port
ServerPort=19980

#SQL COMMANDS
sql.select_animal=SELECT * FROM tbl_type

configB.properties
B4X:
#Lines starting with '#' are comments.
#Backslash character at the end of line means that the command continues in the next line.

#DATABASE CONFIGURATION
DriverClass=com.mysql.jdbc.Driver
JdbcUrl=jdbc:mysql://192.168.0.1:3306/testdatabaseB??characterEncoding=utf8
User=user
Password=password

#Java server port
ServerPort=19981

#SQL COMMANDS
sql.update_gcmkey=UPDATE tbl_staff_phone_record SET gcm_key=? WHERE name=?

B4J Main code
B4X:
Sub Process_Globals
    Public serverA As Server
    Public serverB As Server
    Public rdcConnectorA1 As RDCConnectorA
    Public rdcConnectorB1 As RDCConnectorB
    Public const VERSION As Float = 2.1
    Type DBCommand (Name As String, Parameters() As Object)
    Type DBResult (Tag As Object, Columns As Map, Rows As List)
End Sub

Sub AppStart (Args() As String)
    serverA.Initialize("")
    rdcConnectorA1.Initialize
    serverA.Port = rdcConnectorA1.serverPortA
    serverA.AddHandler("/rdc_lms3", "RDCHandlerA", False)
    serverA.AddHandler("/test_lms3", "TestHandlerA", False)
    serverA.AddHandler("/exec_non_query", "ExecNonQueryHandler", False)
    serverA.Start
  
    serverB.Initialize("")
    rdcConnectorB1.Initialize
    serverB.Port = rdcConnectorB1.serverPortB
    serverB.AddHandler("/rdc_lss", "RDCHandlerB", False)
    serverB.AddHandler("/test_lss", "TestHandlerB", False)
    serverB.Start
  
    Log($"Hirise jRDC is running (version = $1.2{VERSION}) Port = ${serverA.Port}"$)
    Log($"Lift Shutdown Status jRDC is running (version = $1.2{VERSION}) Port = ${serverB.Port}"$)
    StartMessageLoop
End Sub

RDCConnectorA code:
B4X:
'Non-UI application (console / server application)
#Region  Project Attributes
    #CommandLineArgs:
    #MergeLibraries: True
#End Region
#AdditionalJar: mysql-connector-java-5.1.38-bin
#AdditionalJar: sqlite-jdbc-3.7.2

Sub Process_Globals
    Public serverA As Server
    Public serverB As Server
    Public rdcConnectorA1 As RDCConnectorA
    Public rdcConnectorB1 As RDCConnectorB
    Public const VERSION As Float = 2.1
    Type DBCommand (Name As String, Parameters() As Object)
    Type DBResult (Tag As Object, Columns As Map, Rows As List)
End Sub

Sub AppStart (Args() As String)
    serverA.Initialize("")
    rdcConnectorA1.Initialize
    serverA.Port = rdcConnectorA1.serverPortA
    serverA.AddHandler("/rdc_lms3", "RDCHandlerA", False)
    serverA.AddHandler("/test_lms3", "TestHandlerA", False)
    serverA.AddHandler("/exec_non_query", "ExecNonQueryHandler", False)
    serverA.Start
  
    serverB.Initialize("")
    rdcConnectorB1.Initialize
    serverB.Port = rdcConnectorB1.serverPortB
    serverB.AddHandler("/rdc_lss", "RDCHandlerB", False)
    serverB.AddHandler("/test_lss", "TestHandlerB", False)
    serverB.Start
  
    Log($"Hirise jRDC is running (version = $1.2{VERSION}) Port = ${serverA.Port}"$)
    Log($"Lift Shutdown Status jRDC is running (version = $1.2{VERSION}) Port = ${serverB.Port}"$)
    StartMessageLoop
End Sub

RDCConnectorB code:
B4X:
'Class module
Sub Class_Globals
    Private pool As ConnectionPool
    Private DebugQueries As Boolean
    Private commands As Map
    Public serverPortB As Int
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    Dim config As Map = LoadConfigMap
    pool.Initialize(config.Get("DriverClass"), config.Get("JdbcUrl"), config.Get("User"), _
        config.Get("Password"))
#if DEBUG
    DebugQueries = True
#else
    DebugQueries = False
#end if
    serverPortB = config.Get("ServerPort")
    LoadSQLCommands(config)
End Sub

Private Sub LoadConfigMap As Map
    Return File.ReadMap(File.DirAssets, "configB.properties")
End Sub

Public Sub GetCommand(Key As String) As String
    If commands.ContainsKey("sql." & Key) = False Then
        Log("*** Command not found: " & Key)
    End If
    Return commands.Get("sql." & Key)
End Sub

Public Sub GetConnection As SQL
    If DebugQueries Then LoadSQLCommands(LoadConfigMap)
    Return pool.GetConnection
End Sub


Private Sub LoadSQLCommands(config As Map)
    Dim newCommands As Map
    newCommands.Initialize
    For Each k As String In config.Keys
        If k.StartsWith("sql.") Then
            newCommands.Put(k, config.Get(k))
        End If
    Next
    commands = newCommands
End Sub

RDCHandlerA code:
B4X:
'Handler class
Sub Class_Globals

End Sub

Public Sub Initialize
  
End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)
    Dim start As Long = DateTime.Now
    Dim q As String
    Dim in As InputStream = req.InputStream
    Dim method As String = req.GetParameter("method")
    Dim con As SQL
    Try
        con = Main.rdcConnectorA1.GetConnection
        If method = "query2" Then
            q = ExecuteQuery2(con, in, resp)
        Else if method = "batch2" Then
            q = ExecuteBatch2(con, in, resp)
        Else
            Log("Unknown method: " & method)
            resp.SendError(500, "unknown method")
        End If
    Catch
        Log(LastException)
        resp.SendError(500, LastException.Message)
    End Try
    If con <> Null And con.IsInitialized Then con.Close
    Log($"Command: ${q}, took: ${DateTime.Now - start}ms, client=${req.RemoteAddress}"$)
End Sub

Private Sub ExecuteQuery2 (con As SQL, in As InputStream,  resp As ServletResponse) As String
    Dim ser As B4XSerializator
    Dim m As Map = ser.ConvertBytesToObject(Bit.InputStreamToBytes(in))
    Dim cmd As DBCommand = m.Get("command")
    Dim limit As Int = m.Get("limit")
        Dim rs As ResultSet = con.ExecQuery2(Main.rdcConnectorA1.GetCommand(cmd.Name), cmd.Parameters)
    If limit <= 0 Then limit = 0x7fffffff 'max int
    Dim jrs As JavaObject = rs
    Dim rsmd As JavaObject = jrs.RunMethod("getMetaData", Null)
    Dim cols As Int = rs.ColumnCount
    Dim res As DBResult
    res.Initialize
    res.columns.Initialize
    res.Tag = Null 'without this the Tag properly will not be serializable.
    For i = 0 To cols - 1
        res.columns.Put(rs.GetColumnName(i), i)
    Next
    res.Rows.Initialize
    Do While rs.NextRow And limit > 0
        Dim row(cols) As Object
        For i = 0 To cols - 1
            Dim ct As Int = rsmd.RunMethod("getColumnType", Array(i + 1))
            'check whether it is a blob field
            If ct = -2 Or ct = 2004 Or ct = -3 Or ct = -4 Then
                row(i) = rs.GetBlob2(i)
            Else if ct = 2 Or ct = 3 Then
                row(i) = rs.GetDouble2(i)
            Else
                row(i) = jrs.RunMethod("getObject", Array(i + 1))
            End If
        Next
        res.Rows.Add(row)
    Loop
  
    rs.Close
    Dim data() As Byte = ser.ConvertObjectToBytes(res)
    resp.OutputStream.WriteBytes(data, 0, data.Length)
    Return "query: " & cmd.Name
End Sub

Private Sub ExecuteBatch2(con As SQL, in As InputStream, resp As ServletResponse) As String
    Dim ser As B4XSerializator
    Dim m As Map = ser.ConvertBytesToObject(Bit.InputStreamToBytes(in))
    Dim commands As List = m.Get("commands")
    Dim res As DBResult
    res.Initialize
    res.columns = CreateMap("AffectedRows (N/A)": 0)
    res.Rows.Initialize
    res.Tag = Null
    Try
        con.BeginTransaction
        For Each cmd As DBCommand In commands
            con.ExecNonQuery2(Main.rdcConnectorA1.GetCommand(cmd.Name), _
                cmd.Parameters)
        Next
        res.Rows.Add(Array As Object(0))
        con.TransactionSuccessful
    Catch
        con.Rollback
        Log(LastException)
        resp.SendError(500, LastException.Message)
    End Try
    Dim data() As Byte = ser.ConvertObjectToBytes(res)
    resp.OutputStream.WriteBytes(data, 0, data.Length)
    Return $"batch (size=${commands.Size})"$
End Sub

RDCHandlerB code:
B4X:
'Handler class
Sub Class_Globals

End Sub

Public Sub Initialize
  
End Sub

Sub Handle(req As ServletRequest, resp As ServletResponse)
    Dim start As Long = DateTime.Now
    Dim q As String
    Dim in As InputStream = req.InputStream
    Dim method As String = req.GetParameter("method")
    Dim con As SQL
    Try
        con = Main.rdcConnectorB1.GetConnection
        If method = "query2" Then
            q = ExecuteQuery2(con, in, resp)
        Else if method = "batch2" Then
            q = ExecuteBatch2(con, in, resp)
        Else
            Log("Unknown method: " & method)
            resp.SendError(500, "unknown method")
        End If
    Catch
        Log(LastException)
        resp.SendError(500, LastException.Message)
    End Try
    If con <> Null And con.IsInitialized Then con.Close
    Log($"Command: ${q}, took: ${DateTime.Now - start}ms, client=${req.RemoteAddress}"$)
End Sub

Private Sub ExecuteQuery2 (con As SQL, in As InputStream,  resp As ServletResponse) As String
    Dim ser As B4XSerializator
    Dim m As Map = ser.ConvertBytesToObject(Bit.InputStreamToBytes(in))
    Dim cmd As DBCommand = m.Get("command")
    Dim limit As Int = m.Get("limit")
        Dim rs As ResultSet = con.ExecQuery2(Main.rdcConnectorB1.GetCommand(cmd.Name), cmd.Parameters)
    If limit <= 0 Then limit = 0x7fffffff 'max int
    Dim jrs As JavaObject = rs
    Dim rsmd As JavaObject = jrs.RunMethod("getMetaData", Null)
    Dim cols As Int = rs.ColumnCount
    Dim res As DBResult
    res.Initialize
    res.columns.Initialize
    res.Tag = Null 'without this the Tag properly will not be serializable.
    For i = 0 To cols - 1
        res.columns.Put(rs.GetColumnName(i), i)
    Next
    res.Rows.Initialize
    Do While rs.NextRow And limit > 0
        Dim row(cols) As Object
        For i = 0 To cols - 1
            Dim ct As Int = rsmd.RunMethod("getColumnType", Array(i + 1))
            'check whether it is a blob field
            If ct = -2 Or ct = 2004 Or ct = -3 Or ct = -4 Then
                row(i) = rs.GetBlob2(i)
            Else if ct = 2 Or ct = 3 Then
                row(i) = rs.GetDouble2(i)
            Else
                row(i) = jrs.RunMethod("getObject", Array(i + 1))
            End If
        Next
        res.Rows.Add(row)
    Loop
  
    rs.Close
    Dim data() As Byte = ser.ConvertObjectToBytes(res)
    resp.OutputStream.WriteBytes(data, 0, data.Length)
    Return "query: " & cmd.Name
End Sub

Private Sub ExecuteBatch2(con As SQL, in As InputStream, resp As ServletResponse) As String
    Dim ser As B4XSerializator
    Dim m As Map = ser.ConvertBytesToObject(Bit.InputStreamToBytes(in))
    Dim commands As List = m.Get("commands")
    Dim res As DBResult
    res.Initialize
    res.columns = CreateMap("AffectedRows (N/A)": 0)
    res.Rows.Initialize
    res.Tag = Null
    Try
        con.BeginTransaction
        For Each cmd As DBCommand In commands
            con.ExecNonQuery2(Main.rdcConnectorB1.GetCommand(cmd.Name), _
                cmd.Parameters)
        Next
        res.Rows.Add(Array As Object(0))
        con.TransactionSuccessful
    Catch
        con.Rollback
        Log(LastException)
        resp.SendError(500, LastException.Message)
    End Try
    Dim data() As Byte = ser.ConvertObjectToBytes(res)
    resp.OutputStream.WriteBytes(data, 0, data.Length)
    Return $"batch (size=${commands.Size})"$
End Sub
 
Upvote 0
Top