1

我正在使用Spring MVC 3.2 Embedded database (H2)支持存儲任務的實時進度,排隊通知和一些臨時日誌。唯一的問題就是我的數據消失了;如果應用程序重新部署或服務器重新啓動。這種情況在生產環境中可能非常少見,但我仍然想知道在生產環境中使用嵌入式數據庫是不錯的選擇還是不行?或者有什麼方法可以將嵌入式數據庫狀態硬盤,以便下次啓動服務器時,我們可以將數據庫狀態恢復到存儲的檢查點?在生產環境中使用嵌入式數據庫?

謝謝。

回答

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

+0

我使用嵌入式數據庫只跟蹤當前進度和任務完成後,所有的變化都堅持到真正的數據庫服務器是這種做法對嗎?..謝謝你的提示在過程模式我會試試看。 –

+0

是否有任何MySQL獨立版本,您可以將它們放在spring引導JAR文件的同一目錄中......然後創建一個bash或一個用於初始化MySQL服務器的windows.bat腳本......然後運行Spring引導JAR文件?....如果這種情況下工作,我將有一個春季引導站桌面Web應用程序 –