B4J Question JRDC2 Change database?

androidappl

Active Member
Licensed User
Longtime User
How do I change the database while the app is running b4j executable ??

JdbcUrl = jdbc: jtds: sqlserver: // servername / dbname; useLOBs = false


For now I managed to make the call from the client, now I have to start your server again, overwriting the "jdbcUrl". how can I do?
 

androidappl

Active Member
Licensed User
Longtime User
See RDCConnector.Initialize code. You can create a similar method that closes the pool and initializes a new pool with any settings you like.

thanks for the prompt response.

I tried to close the pool and re-initialize it, but it does not go.
Here is the way I call to change the db with the new string.

Do you have any other suggestions?

B4X:
public Sub re_Inizializza(JDBC_URL As String)
    pool.ClosePool()
    Dim config As Map = LoadConfigMap
   
    pool.Initialize(config.Get("DriverClass"), JDBC_URL, config.Get("User"), _
        config.Get("Password"))
#if DEBUG
    DebugQueries = True
#else
    DebugQueries = False
#end if
    serverPort = config.Get("ServerPort")
    LoadSQLCommands(config)
End Sub
 
Upvote 0

androidappl

Active Member
Licensed User
Longtime User
No, because I don't know what it means that it does not go. Do you see any error?

Questo è il log:

mar 15, 2017 2:28:25 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
Informazioni: Initializing c3p0 pool... 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 -> 1bqx2rl9m1mb6gif12u6qjf|31731840, debugUnreturnedConnectionStackTraces -> false, desc...
Class not found: example.schedulazioni.main$_dbcommand, trying: b4j.example.main$_dbcommand
 
Upvote 0

androidappl

Active Member
Licensed User
Longtime User
No, because I don't know what it means that it does not go. Do you see any error?
sorry, this is log:

mar 15, 2017 2:28:25 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
Informazioni: Initializing c3p0 pool... 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 -> 1bqx2rl9m1mb6gif12u6qjf|31731840, debugUnreturnedConnectionStackTraces -> false, desc...
Class not found: example.schedulazioni.main$_dbcommand, trying: b4j.example.main$_dbcommand
Command: query: select_elencodb, took: 3879ms, client=172.22.2.8
mar 15, 2017 2:31:10 PM com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$ConnectionEventListenerImpl doCheckinResource
Avvertenza: An Exception occurred while trying to check a PooledConection into a ResourcePool.
java.sql.SQLException: An SQLException was provoked by the following failure: com.mchange.v2.resourcepool.ResourcePoolException: ResourcePool [BROKEN!]: Tried to check-in a foreign resource!
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:118)
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:77)
at com.mchange.v2.sql.SqlUtils.toSQLException(SqlUtils.java:74)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkinPooledConnection(C3P0PooledConnectionPool.java:809)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$ConnectionEventListenerImpl.doCheckinResource(C3P0PooledConnectionPool.java:908)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$ConnectionEventListenerImpl.connectionClosed(C3P0PooledConnectionPool.java:900)
at com.mchange.v2.c3p0.util.ConnectionEventSupport.fireConnectionClosed(ConnectionEventSupport.java:73)
at com.mchange.v2.c3p0.impl.NewPooledConnection.fireConnectionClosed(NewPooledConnection.java:570)
at com.mchange.v2.c3p0.impl.NewPooledConnection.markClosedProxyConnection(NewPooledConnection.java:439)
at com.mchange.v2.c3p0.impl.NewProxyConnection.close(NewProxyConnection.java:1225)
at anywheresoftware.b4j.objects.SQL.Close(SQL.java:438)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:637)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:231)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:158)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at anywheresoftware.b4j.object.JServlet$Handle.run(JServlet.java:115)
at anywheresoftware.b4a.keywords.SimpleMessageLoop.runMessageLoop(SimpleMessageLoop.java:30)
at anywheresoftware.b4a.StandardBA.startMessageLoop(StandardBA.java:26)
at anywheresoftware.b4a.ShellBA.startMessageLoop(ShellBA.java:111)
at anywheresoftware.b4a.keywords.Common.StartMessageLoop(Common.java:131)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:300)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:158)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:90)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:84)
at b4j.example.main.main(main.java:29)
Caused by: com.mchange.v2.resourcepool.ResourcePoolException: ResourcePool [BROKEN!]: Tried to check-in a foreign resource!
at com.mchange.v2.resourcepool.BasicResourcePool.checkinResource(BasicResourcePool.java:706)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.unmarkConnectionInUseAndCheckin(C3P0PooledConnectionPool.java:797)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkinPooledConnection(C3P0PooledConnectionPool.java:806)
... 36 more
 
Upvote 0

androidappl

Active Member
Licensed User
Longtime User
Try it without closing the old pool. Do you get the same error? Are there any open connections when you replace the pool?
It is exactly the previous one .... is there a way to close it and reopen it in a planned way with the new pool?

logical flow I should do:

1) START SERVER (db1)
2) CLIENT: send post request to change db.
3) SERVER: receives the request, it closes the old pool opens a new one with the method that you saw above. (New pool with db2)
4) The server works with the new pool (db2)

Point 4) I'm not going to be the mistake that I posted you.
 
Upvote 0

androidappl

Active Member
Licensed User
Longtime User
What happens after the crash? Does it work or continue to show this error?

ah another thing, when the server gives me this error on start server.

But the first call that requires the list of databases work the same.

2017-03-15 14:55:10.443:INFO:eek:ejs.Server:main: Started @789ms
Emulated network latency: 100ms
Error occurred on line: 31
java.lang.IllegalStateException: Multiple servlets map to path: /: org.eclipse.jetty.servlet.DefaultServlet-7593da36,org.eclipse.jetty.servlet.DefaultServlet-454b70a9
at org.eclipse.jetty.servlet.ServletHandler.updateMappings(ServletHandler.java:1466)
at org.eclipse.jetty.servlet.ServletHandler.setServletMappings(ServletHandler.java:1563)
at org.eclipse.jetty.servlet.ServletHandler.addServletWithMapping(ServletHandler.java:972)
at org.eclipse.jetty.servlet.ServletHandler.addServletWithMapping(ServletHandler.java:937)
at org.eclipse.jetty.servlet.ServletContextHandler.addServlet(ServletContextHandler.java:353)
at anywheresoftware.b4j.object.ServerWrapper.Start(ServerWrapper.java:131)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:637)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:231)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:158)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
 
Upvote 0

androidappl

Active Member
Licensed User
Longtime User
It will be useful to post the working solution...

Good morning, then the server side:

in RDCHANDLER form I added another method in the sub Handle.


B4X:
con = Main.rdcConnector1.GetConnection
        If method = "query2" Then
            q = ExecuteQuery2(con, in, resp)
        Else if method = "cambiadb" Then

           
            q = cambiadbAG(con, in, resp)
            'cmd.Parameters
           
            Main.rdcConnector1.DB = q

the method in question is "cambiadb" then in my configuration file, in the .config I will.

# Check on the DB CHANGE
sql.cambia_db = SELECT name FROM master.dbo.sysdatabases WHERE name =?

the "cambiadbAG" method will only return the parameter and then the name db passed from the client, and this will be saved in a variable of the main module.

variable: Main.rdcConnector1.DB

by this time all the queries that will be sent to the server from the client, and will present this special character "$" will be corrected automatically.

for example: in the config file

sql.select_people = SELECT * FROM $people

When the server will find this special character "$" will replace it with the correct db name, stored in the variable.
B4X:
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")
   
   
    '*********************MOD****************************************
    Dim querMod As String = Main.rdcConnector1.GetCommand(cmd.Name)
    Log(querMod)
    If Main.rdcConnector1.DB.Length > 0  Then
        querMod = querMod.Replace("$",Main.rdcConnector1.DB & ".dbo.")   
    End If
    Log(querMod)
    '*********************MOD****************************************
   
    Dim rs As ResultSet = con.ExecQuery2(querMod, 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)
   
    ' Dim data() As Byte = bc.StringToBytes(js.ToString,"utf-8" )
    resp.OutputStream.WriteBytes(data, 0, data.Length)
    Return "query: " & cmd.Name
End Sub
 
Upvote 0
Top