JDBC session for embedded jetty (Version above 9.4)

438 Views Asked by At

I am trying to implement jdbc session for embedded jetty server in order to share the sessions across nodes(TO avoid users losing their sessions when a deployment happens) in an openshift environment. The existing official documentations (https://www.eclipse.org/jetty/documentation/9.4.32.v20200930/configuring-sessions-jdbc.html) have implementation details for Jetty distribution implementation only. There is a similar but incomplete solution at How to setup embeded Jetty to use JDBC sessions

// Configure a JDBCSessionDataStoreFactory.
JDBCSessionDataStoreFactory sessionDataStoreFactory = new JDBCSessionDataStoreFactory();
sessionDataStoreFactory.setGracePeriodSec(3600);
sessionDataStoreFactory.setSavePeriodSec(0);
sessionDataStoreFactory.setDatabaseAdaptor(...);

JDBCSessionDataStore.SessionTableSchema schema = new JDBCSessionDataStore.SessionTableSchema();
schema.setAccessTimeColumn("accessTime");
schema.setContextPathColumn("contextPath");
// ... more configuration here
sessionDataStoreFactory.setSessionTableSchema(schema);

// Add the SessionDataStoreFactory as a bean on the server.
server.addBean(sessionDataStoreFactory);

Here it was not clear on how to create a DatabaseAdaptor object. Could someone help on this?

1

There are 1 best solutions below

0
On

Creating the DatabaseAdaptor is pretty easy. Here's an example:

DatabaseAdaptor driverAdaptor = new DatabaseAdaptor();
driverAdaptor.setDriverInfo("com.mysql.jdbc.Driver",
                            "jdbc:mysql://127.0.0.1:3306/sessions?user=sessionsadmin");

That's really all you have to do to create one using the driver class. There's also other options for passing a javax.sql.DataSource instead, or a jndi string to lookup to obtain the javax.sql.DataSource. But using the driver class and jdbc string is very common.

You don't really have to setup anything else on DatabaseAdapter, as it tries to work out what it needs from the metadata returned from a connection for things that change between databases. Some examples of things that change between database is the name of some of the sql types, like strings, longs and blobs. For example, for Oracle, a long is number(20), but for other databases it is bigint. In the case where the DatabaseAdapter can't work it out, you may need to explicitly tell it what these type names are, but in most cases, the DatabaseAdapter is able to work it out.