0

I am using embedded jetty (group: 'org.eclipse.jetty', name: 'jetty-webapp', version: '9.4.27.v20200227') and I am trying to programmatically setup it to use JDBC for session storage. All the documentation/examples I can find is about standalone jetty.

Do you know how to setup it?

Ognyan
  • 13,452
  • 5
  • 64
  • 82

1 Answers1

2

I don't know all that much about JDBC or session storage, but looking at the documentation Persistent Sessions: JDBC for standalone jetty, it is telling you to enable the module session-store-jdbc. By looking at session-store-jdbc.mod you can see that it uses etc/sessions/jdbc/session-store.xml and these XML files can be directly translated into java code.

So it looks like its adding a JDBCSessionDataStoreFactory as a bean onto the server. So some equivalent code that you could try would look something like:

// 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);
Lachlan
  • 356
  • 1
  • 7
  • @Lachian I ended up using similar approach but I am not sure if it is "canonical" one. The worst thing is that there are no examples out there for setup via code, not XML. The tricky part here is `setDatabaseAdaptor()` where you have to give it DataStore. It should be pooled data source otherwise it becomes a bottleneck. – Ognyan Mar 12 '20 at 05:56