嵌入式码头的JDBC会话9.4版以上

问题描述

我正在尝试为嵌入式码头服务器实现jdbc会话,以便在openshift环境中跨节点共享会话(以避免用户在部署发生时丢失会话)。现有的官方文档(https://www.eclipse.org/jetty/documentation/9.4.32.v20200930/configuring-sessions-jdbc.html)仅具有Jetty发行版实施的实施详细信息。 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);

关于如何创建DatabaseAdaptor对象尚不清楚。有人可以帮忙吗?

解决方法

创建DatabaseAdaptor非常简单。这是一个示例:

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

使用驱动程序类创建一个实际上就是要做的所有事情。还有其他选择,而不是传递javax.sql.DataSource或jndi字符串以查找以获得javax.sql.DataSource的其他选项。但是使用驱动程序类和jdbc字符串很常见。

您实际上不必在DatabaseAdapter上进行任何其他设置,因为它会尝试从连接返回的元数据中找出需要的内容,以处理数据库之间发生的更改。一些在数据库之间更改的事物的示例是某些sql类型的名称,例如字符串,long和blob。例如,对于Oracle,长为number(20),但对于其他数据库为bigint。在DatabaseAdapter无法解决的情况下,您可能需要明确告诉它们这些类型名称是什么,但是在大多数情况下,DatabaseAdapter可以解决它。