B4J Code Snippet [Server] Persistent JDBC Session - Store session data in database

Call this sub after the server is started (srvr.Start)

B4X:
Public Sub SetPersistentJDBCSessions(ServerObject As Server, NodeName As String, DBDriver As String, DBUrl As String, SessionIdTableName As String, SessionDataTableName As String, ScavengeInterval As Long)
    ' http://www.eclipse.org/jetty/documentation/9.3.x/session-clustering-jdbc.html
    Dim joServer As JavaObject = ServerObject
    ' stop the server as some settings cannot be modified while context is in use
    joServer.GetFieldJO("server").RunMethod("stop", Null)

    ' The session ID manager ensures that session IDs are unique across all webapps hosted on a Jetty instance, and thus there can only be one session ID manager per Jetty instance.
    Dim joJDBCSessionIdManager As JavaObject
    joJDBCSessionIdManager.InitializeNewInstance("org.eclipse.jetty.server.session.JDBCSessionIdManager", Array(joServer.GetFieldJO("server")))
    joJDBCSessionIdManager.RunMethod("setWorkerName", Array(NodeName))
    joJDBCSessionIdManager.RunMethod("setDriverInfo", Array(DBDriver, DBUrl))
    joJDBCSessionIdManager.RunMethod("setScavengeInterval", Array(ScavengeInterval))
    joJDBCSessionIdManager.RunMethodJO("getSessionIdTableSchema", Null).RunMethod("setTableName", Array(SessionIdTableName))
    joJDBCSessionIdManager.RunMethodJO("getSessionTableSchema", Null).RunMethod("setTableName", Array(SessionDataTableName))
    joServer.GetFieldJO("server").RunMethod("setSessionIdManager", Array(joJDBCSessionIdManager))

    ' The session manager handles the session lifecycle (create/update/invalidate/expire) on behalf of a web application, so there is one session manager per web application instance.
    Dim joJDBCSessionManager As JavaObject
    joJDBCSessionManager.InitializeNewInstance("org.eclipse.jetty.server.session.JDBCSessionManager", Null)
    joJDBCSessionManager.RunMethod("setSessionIdManager", Array(joJDBCSessionIdManager))
    Dim joSessionHandler As JavaObject
    joSessionHandler.InitializeNewInstance("org.eclipse.jetty.server.session.SessionHandler", Array(joJDBCSessionManager))
    joServer.GetFieldJO("context").RunMethod("setSessionHandler", Array(joSessionHandler))

    ' start the server with the new settings
    joServer.GetFieldJO("server").RunMethod("start", Null)
End Sub

You can call the sub like this:
B4X:
SetPersistentJDBCSessions(srvr, "node1", "org.mariadb.jdbc.Driver", "jdbc:mariadb://your_app_database_ip_address:your_app_database_port/your_app_database_name?user=your_app_database_user&password=your_app_database_password", "session", "session_data", 600)

The sessionIds and SessionData tables will be automatically created (your database user needs to have the privileges to create tables)

This is usefull if you have many instances of an app and you want the session to be shared between them. And if you restart the server you don't lose your session data.

This is for the current B4J jetty version (9.3)
More about this: http://www.eclipse.org/jetty/documentation/9.3.x/session-clustering-jdbc.html
 
Last edited:
Top