Skip to main content

Distributed JDBC session for Dropwizard 1.2.2

Following code illustrates how to configure distributed JDBC sessions in Dropwizard V1.2.2.

Application.java file,

@Override
public void run(TestDropwizardConfiguration configuration, Environment environment)
throws Exception
{
env.lifecycle().addLifeCycleListener(new AbstractLifeCycleListener()
{
@Override
public void lifeCycleStarting(LifeCycle event)
{
if (!(event instanceof Server))
{
return;
}
Server server = (Server) event;
DataSourceFactory dataSourceFactory = configuration.getDataSourceFactory();
DatabaseAdaptor databaseAdaptor = new DatabaseAdaptor();
databaseAdaptor.setDriverInfo(
dataSourceFactory.getDriverClass(),
dataSourceFactory.getUrl()
+ ";user=" + dataSourceFactory.getUser()
+ ";password=" + dataSourceFactory.getPassword());
JDBCSessionDataStore sessionDataStore = new JDBCSessionDataStore();
sessionDataStore.setDatabaseAdaptor(databaseAdaptor);
DefaultSessionIdManager sessionIdManager = new DefaultSessionIdManager(server);
sessionIdManager.setWorkerName(configuration.getNodeName());
SessionHandler sessionHandler = new SessionHandler();
sessionHandler.setSessionIdManager(sessionIdManager);
// Idle HTTP servlet sessions expire after 24 hours
sessionHandler.setMaxInactiveInterval(60 * 60 * 24);
sessionHandler.setSessionCookie("LOGIN_SESSION1");
sessionHandler.setHttpOnly(true);
env.servlets().setSessionHandler(sessionHandler);
}
});
}

Comments