强制休眠在读取之前重新加载 hsqldb 文件

force hibernate to reload the hsqldb file before reading

我有以下情况:

我试过写:

Session session = sessionFactory.getCurrentSession();
try {
   session.setFlushMode(FlushMode.AUTO);
   session.saveOrUpdate(cluster);
   session.flush();
   return true;
} catch (Exception e) {
   LOG.error("Failed saving cluster {}!", cluster.getName(), e);
   return false;
}

阅读:

Session session = sessionFactory.getCurrentSession();
    session.setCacheMode(CacheMode.REFRESH);

 try {
     return (List<Cluster>) session
              .createCriteria(Cluster.class)
              .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
              .list();
  } catch (Exception e) {
      LOG.error("Error while trying to read the clusters from file!", e);
      return new ArrayList<>();
  }

但遗憾的是这不起作用。

我也试过关闭连接然后重新打开,但还是不行。

更新[1]:

我刚刚设置:

basicDataSource.setDefaultTransactionIsolation(Isolation.READ_UNCOMMITTED.value())

app2 那边,但即便如此,app2 也无法读取更新。

HSQLDB 文档指出:

Memory tables are the default type when the CREATE TABLE command is used. Their data is held entirely in memory but any change to their structure or contents is written to the .script file. The script file is read the next time the database is opened, and the MEMORY tables are recreated with all their contents.

因为两个应用程序都使用独立内存表数据库状态可以不同步重新开始。理论上,app2 中的 hsqldb 实例可以通过使用 SHUTDOWN sql command 关闭它并随后在声明新连接时启动来强制重新加载其状态。但它看起来不是一个好的解决方案。如果两个实例试图将它们的状态保存到同一个文件中,也可能会发生冲突。

如果两个应用程序使用一个数据库,我建议在 Server mode 中使用 hsqldb 更合适。服务器模式可以在单独的 JVM 实例上启动,也可以作为 app1.

的一部分启动
Server server = new Server();

server.setDatabaseName(0, "test");
server.setDatabasePath(0, "file:test");
server.setLogWriter(new PrintWriter(System.out));
server.setErrWriter(new PrintWriter(System.err));
server.start();

app2 将能够使用

进行连接
DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/test");