我有两个 javafx 应用程序,一个是应用程序,一个是更新程序。 应用使用Firebird数据库来存储一些脆弱的用户数据。数据库以嵌入模式运行(我认为这是相关的),因此这意味着同一时间只能有一个到数据库的连接(数据库创建一个锁定文件)。 更新程序更新应用程序。
整个架构如下所示:
- 用户运行应用程序 -> 应用程序正在检查是否需要更新,如果需要,则启动更新程序(使用 java ProcessBuilder)并自行关闭(Platform.exit())。
- 更新程序检查应用是否已正确终止。
- 更新程序运行命令“App --export-user-data”(也使用 ProcessBuilder)在开始更新之前导出最重要的内容(必须以这种方式完成 - 我无法将此功能移至更新程序)。
- 应用程序在第一次 session.beginTransaction() 时卡住 - 没有单一错误或异常
到目前为止我所观察到的:
- 当我启动应用程序并按 [X] 关闭它时,所有 来自“C:\ProgramData\firebird”的锁定文件被删除,但是当应用程序 启动更新程序并自行关闭,然后锁定文件保持不变。我认为这就是 Hibernate 无法开始事务的原因。
- 更新程序的进程不是应用程序的子进程(我使用进程监视器检查了这一点)
- 当我直接启动 Updater 时,它就像一个魅力 - 因此只有当应用启动 Updater 时问题才会出现。
我不能做的事情:
- 将数据库切换到其他任何数据库 - 它必须嵌入 firebird
- 将导出功能移至更新程序
即使是最奇怪的想法我也会很感激,因为我花了四天时间试图解决这个问题。
编辑: Firebird 版本:2.1 Jaybird版本:2.1.6
Updater的启动方式(只做必要的事情)
public void startUpdater(){
ProcessBuilder pb = new ProcessBuilder(updaterPath, argument)
pb.start();
Platform.exit();
}
最佳答案
经过长时间的战斗,我终于找到了解决方案。当java创建一个新进程时,子进程将从其父进程继承所有句柄。这就是为什么 firebird 锁定文件没有被删除。我通过在 cpp 中创建小应用程序并在运行更新程序时将其用作代理来解决这个问题。
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
int _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
if( argc != 2 )
{
printf("Usage: %s [cmdline]\n", argv[0]);
return 0;
}
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line)
argv[1], // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
)
{
printf( "CreateProcess failed (%d).\n", GetLastError() );
return 0;
}
}
关于Java 不会在应用程序关闭时释放所有资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40770796/