简体   繁体   English

HSQLDB-无法重新启动加密的数据库

[英]HSQLDB - cannot restart encrypted database

I have a Java web application running in Tomcat that embeds an HSQLDB server database. 我有一个在Tomcat中运行的Java Web应用程序,该应用程序嵌入了HSQLDB服务器数据库。 I am attempting to encrypt this database. 我正在尝试加密此数据库。

The first time that I start Tomcat the database is created fine, the files appear encrypted and the application functions as expected. 第一次启动Tomcat时,数据库创建良好,文件显示为已加密,并且应用程序按预期运行。 However when I shutdown Tomcat and restart, I get the following error: 但是,当我关闭Tomcat并重新启动时,出现以下错误:

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

The relevant portions of my startup and shutdown code are as follows: 我的启动和关闭代码的相关部分如下:

/*
 * (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;
    }
}

The ShutdownDAO.shutdownDatabase() method performs the following: ShutdownDAO.shutdownDatabase()方法执行以下操作:

public void shutdownDatabase(){     
    jdbcTemplate.execute("SHUTDOWN COMPACT");
}

The properties referred to in the start() method are set up as follows: 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");

If I do not attempt to encrypt the database, all works fine. 如果我不尝试加密数据库,则一切正常。

The critical portion is the exception did not open: org.hsqldb.HsqlException: java.io.IOException: Not in GZIP format - this suggests to me that the database has not been shut down correctly. 关键部分是未打开的异常:org.hsqldb.HsqlException:java.io.IOException:不是GZIP格式 -这向我暗示数据库未正确关闭。 I have tried both SHUTDOWN and SHUTDOWN COMPACT when shutting down the database to see if this was causing the issue but to no avail. 我在关闭数据库时尝试了SHUTDOWN和SHUTDOWN COMPACT,以查看这是否是导致问题的原因,但无济于事。

Relevant exerpts from logs when shut down are as follows: 关闭时来自日志的相关摘录如下:

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

The folder which contains the database contains only the dbname.properties and dbname.script files after shutdown. 关闭后,包含数据库的文件夹仅包含dbname.properties和dbname.script文件。 The .properties file contains the following: .properties文件包含以下内容:

#HSQL Database Engine 2.2.4
#Tue Nov 03 11:16:49 GMT 2015
version=2.2.4
modified=no

The .script file appears to be encrypted. .script文件似乎已加密。

When Tomcat is restarted however, the above error is encountered. 但是,当Tomcat重新启动时,会遇到上述错误。

Please note that due to client constraints I am limited to Java 5 - so am using hsqldb-j5 2.2.4. 请注意,由于客户端的限制,我仅限于Java 5-使用的是hsqldb-j5 2.2.4。 If this is fixed in a future release I may not be in a position to upgrade. 如果在将来的版本中已解决此问题,则我可能无法升级。

EDIT - Java 5 information 编辑-Java 5信息

As pointed out by fredt below, the Java 5 version is available from Maven with the following in pom.xml 如下面的fredt所指出的,Maven提供了Java 5版本,其中pom.xml中包含以下内容:

  <dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <version>2.3.3</version>
    <classifier>jdk5</classifier>
  </dependency>

Unfortunately the error still persists at J5 - but this may be of use to others in future. 不幸的是,该错误在J5上仍然存在-以后可能会对其他人有用。

EDIT - Additional information 编辑-其他信息

Connections to the database are pooled using C3P0 - could this be making a difference to how the database shuts down perhaps? 使用C3P0池化到数据库的连接-可能对关闭数据库的方式有所不同吗?

The problem was caused by the encryption key being corrupted between restarts of the server by code elsewhere. 该问题是由于在其他服务器之间的代码重新启动之间,加密密钥被破坏了。 The database was therefore being decrypted incorrectly and causing the GZIP format error encountered. 因此,该数据库被不正确地解密,并导致遇到GZIP格式错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM