在生产环境中使用嵌入式数据库

时间:2013-12-09 12:25:11

标签: spring-mvc architecture production-environment embedded-database

我正在使用Spring MVC 3.2 Embedded database (H2)支持存储任务的实时进度,排队通知和一些临时日志。这个approch的唯一问题是我的数据消失了;如果应用程序重新部署或服务器重新启动。这种情况在生产环境中可能非常罕见,但我仍然想知道在生产环境中使用嵌入式数据库是不是一个好的选择?或者有没有办法将嵌入式数据库状态保持到硬盘以便下次服务器启动时我们可以将数据库状态恢复到存储的检查点吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

嵌入式数据库不适用于生产环境。它们用于更快的开发选项,因为您不需要运行外部数据库的依赖项。使用嵌入式数据库,您可以以编程方式启动它,并根据您的需要进行初始化。

重新部署期间丢失更改的原因是因为您使用的是内存版本的HsQL而不是进程内(独立文件)模式。您可以使用独立模式来保持更改持久。

In-Process (Standalone) Mode

This mode runs the database engine as part of your application program in the same Java Virtual Machine. For most applications this mode can be faster, as the data is not converted and sent over the network. The main drawback is that it is not possible by default to connect to the database from outside your application. As a result you cannot check the contents of the database with external tools such as Database Manager while your application is running. In 1.8.0, you can run a server instance in a thread from the same virtual machine as your application and provide external access to your in-process database.

The recommended way of using the in-process mode in an application is to use an HSQLDB Server instance for the database while developing the application and then switch to In-Process mode for deployment.

An In-Process Mode database is started from JDBC, with the database file path specified in the connection URL. For example, if the database name is testdb and its files are located in the same directory as where the command to run your application was issued, the following code is used for the connection:

    Connection c = DriverManager.getConnection("jdbc:hsqldb:file:testdb", "sa", "");
The database file path format can be specified using forward slashes in Windows hosts as well as Linux hosts. So relative paths or paths that refer to the same directory on the same drive can be identical. For example if your database path in Linux is /opt/db/testdb and you create an identical directory structure on the C: drive of a Windows host, you can use the same URL in both Windows and Linux:

    Connection c = DriverManager.getConnection("jdbc:hsqldb:file:/opt/db/testdb", "sa", "");
When using relative paths, these paths will be taken relative to the directory in which the shell command to start the Java Virtual Machine was executed. Refer to Javadoc for jdbcConnection for more details.

HSQL documentation