Java HSQLDB-无法重新启动加密数据库
我在Tomcat中运行了一个Java web应用程序,它嵌入了一个HSQLDB服务器数据库。我正在尝试加密此数据库 我第一次启动Tomcat时,数据库创建良好,文件显示加密,应用程序按预期运行。但是,当我关闭Tomcat并重新启动时,会出现以下错误:Java HSQLDB-无法重新启动加密数据库,java,database,encryption,hsqldb,hypersql,Java,Database,Encryption,Hsqldb,Hypersql,我在Tomcat中运行了一个Java web应用程序,它嵌入了一个HSQLDB服务器数据库。我正在尝试加密此数据库 我第一次启动Tomcat时,数据库创建良好,文件显示加密,应用程序按预期运行。但是,当我关闭Tomcat并重新启动时,会出现以下错误: 5990 [main] DEBUG uk.co.my.db.HyperSqlDbServer - Starting HSQL server... [Server@e79eb3]: [Thread[main,5,main]]: checkRunni
5990 [main] DEBUG uk.co.my.db.HyperSqlDbServer - Starting HSQL server...
[Server@e79eb3]: [Thread[main,5,main]]: checkRunning(false) entered
[Server@e79eb3]: [Thread[main,5,main]]: checkRunning(false) exited
[Server@e79eb3]: [Thread[main,5,main]]: setDatabasePath(0,file:C:\path\to\my\db\dbname;crypt_key=6f841e23d0976a695e7bc7d122c1927d;crypt_type=AES)
[Server@e79eb3]: [Thread[main,5,main]]: checkRunning(false) entered
[Server@e79eb3]: [Thread[main,5,main]]: checkRunning(false) exited
[Server@e79eb3]: [Thread[main,5,main]]: setDatabaseName(0,dbname)
[Server@e79eb3]: [Thread[main,5,main]]: checkRunning(false) entered
[Server@e79eb3]: [Thread[main,5,main]]: checkRunning(false) exited
[Server@e79eb3]: Initiating startup sequence...
[Server@e79eb3]: Server socket opened successfully in 0 ms.
[Server@e79eb3]: [Thread[HSQLDB Server @e79eb3,5,main]]: Database [index=0, db=file:C:\path\to\my\db\dbname, alias=dbname] did not open: org.hsqldb.HsqlException: java.io.IOException: Not in GZIP format
[Server@e79eb3]: Startup sequence completed in 344 ms.
[Server@e79eb3]: 2015-11-03 10:51:32.137 HSQLDB server 2.2.4 is online on port 9001
[Server@e79eb3]: To close normally, connect and execute SHUTDOWN SQL
[Server@e79eb3]: From command line, use [Ctrl]+[C] to abort abruptly
我的启动和关闭代码的相关部分如下:
/*
* (non-Javadoc)
* @see org.springframework.context.Lifecycle#start()
*/
public void start() {
if (server == null) {
logger.debug("Starting HSQL server...");
server = new Server();
// NB: explicitly setting path and name as it didn't work having them in properties file
server.setDatabasePath(0, properties.getProperty("server.database.0"));
server.setDatabaseName(0, properties.getProperty("server.dbname.0"));
try {
server.setProperties(properties);
server.start();
running = true;
} catch(Exception e) {
logger.error("Error starting HSQL server", e);
throw new ConfigurationException(e);
}
}
}
/*
* (non-Javadoc)
* @see org.springframework.context.Lifecycle#stop()
*/
public void stop() {
logger.debug("Stopping HSQL server...");
ShutdownDAO shutdown = context.getBean(ShutdownDAO.class);
if (server != null) {
shutdown.shutdownDatabase();
server.shutdown();
server.stop();
running = false;
}
}
ShutdownDAO.shutdowndabase()方法执行以下操作:
public void shutdownDatabase(){
jdbcTemplate.execute("SHUTDOWN COMPACT");
}
#HSQL Database Engine 2.2.4
#Tue Nov 03 11:16:49 GMT 2015
version=2.2.4
modified=no
start()方法中引用的属性设置如下:
// NB: these first two properties only seem to work as part of the path and don't get picked up when in the properties file
internalDbProps.put("crypt_key", this.internalCryptKey);
internalDbProps.put("crypt_type", "AES");
internalDbProps.put("server.database.0", "file:C:\path\to\my\db\dbname;crypt_key=" + this.internalCryptKey + ";crypt_type=AES");
internalDbProps.put("server.dbname.0","dbname");
internalDbProps.put("server.remote_open","true");
internalDbProps.put("hsqldb.reconfig_logging","false");
internalDbProps.put("shutdown","true");
21515 [HSQLDB Connection @fbfa2] INFO hsqldb.db.HSQLDB50CD101BCF.ENGINE - Database closed
21906 [HSQLDB Connection @fbfa2] INFO hsqldb.db.HSQLDB50CD101BCF.ENGINE - Database closed
[Server@63f6ea]: Initiating shutdown sequence...
[Server@63f6ea]: Shutdown sequence completed in 0 ms.
[Server@63f6ea]: 2015-11-03 11:16:49.726 SHUTDOWN : System.exit() was not called
如果我不尝试加密数据库,一切正常
关键部分是异常未打开:org.hsqldb.HsqlException:java.io.IOException:not in GZIP格式-这表明数据库未正确关闭。我在关闭数据库时尝试了SHUTDOWN和SHUTDOWN COMPACT,以查看这是否导致了问题,但没有效果
关闭时记录的相关EXERPT如下所示:
// NB: these first two properties only seem to work as part of the path and don't get picked up when in the properties file
internalDbProps.put("crypt_key", this.internalCryptKey);
internalDbProps.put("crypt_type", "AES");
internalDbProps.put("server.database.0", "file:C:\path\to\my\db\dbname;crypt_key=" + this.internalCryptKey + ";crypt_type=AES");
internalDbProps.put("server.dbname.0","dbname");
internalDbProps.put("server.remote_open","true");
internalDbProps.put("hsqldb.reconfig_logging","false");
internalDbProps.put("shutdown","true");
21515 [HSQLDB Connection @fbfa2] INFO hsqldb.db.HSQLDB50CD101BCF.ENGINE - Database closed
21906 [HSQLDB Connection @fbfa2] INFO hsqldb.db.HSQLDB50CD101BCF.ENGINE - Database closed
[Server@63f6ea]: Initiating shutdown sequence...
[Server@63f6ea]: Shutdown sequence completed in 0 ms.
[Server@63f6ea]: 2015-11-03 11:16:49.726 SHUTDOWN : System.exit() was not called
关闭后,包含数据库的文件夹仅包含dbname.properties和dbname.script文件。.properties文件包含以下内容:
public void shutdownDatabase(){
jdbcTemplate.execute("SHUTDOWN COMPACT");
}
#HSQL Database Engine 2.2.4
#Tue Nov 03 11:16:49 GMT 2015
version=2.2.4
modified=no
.script文件似乎已加密
但是,当重新启动Tomcat时,会遇到上述错误
请注意,由于客户端的限制,我仅限于Java 5,因此我使用的是hsqldb-j5 2.2.4。如果在将来的版本中修复了这个问题,我可能无法升级
编辑-Java 5信息
正如下面fredt所指出的,Java 5版本可从Maven获得,并在pom.xml中包含以下内容
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.3.3</version>
<classifier>jdk5</classifier>
</dependency>
org.hsqldb
hsqldb
2.3.3
jdk5
不幸的是,这个错误在J5上仍然存在——但这可能对将来的其他人有用
编辑-其他信息
到数据库的连接是使用C3P0汇集的-这可能会对数据库关闭的方式产生影响吗?该问题是由于在服务器重新启动之间加密密钥被其他地方的代码损坏而导致的。因此,数据库被错误地解密,并导致遇到GZIP格式错误 Maven上提供了适用于Java5的所有最新版本的HSQLDBJAR。例如,这很有趣,谢谢。我将对此进行研究,因为我显然希望尽可能使用最新的代码。实际上,我已经用Gradle在本地构建了J5版本,以查看它是否解决了问题,但不幸的是,没有-引发了相同的异常..您也可以尝试其他加密算法。我已经解决了问题-我的加密密钥在服务器重新启动之间被损坏。谢谢你在这方面的帮助!