强制休眠在读取之前重新加载 hsqldb 文件
force hibernate to reload the hsqldb file before reading
我有以下情况:
- 两个使用相同的应用程序
in file hsqldb
app1
仅从 db
中写入 to/deletes 而 app2
仅从中读取
app1
发出信号(通过发送事件)app2
db
更新时他应该读取新值
app2
无法读取新值,他只能看到旧值
我试过写:
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");
我有以下情况:
- 两个使用相同的应用程序
in file hsqldb
app1
仅从db
中写入 to/deletes 而app2
仅从中读取app1
发出信号(通过发送事件)app2
db
更新时他应该读取新值app2
无法读取新值,他只能看到旧值
我试过写:
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");