亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Spring Boot 中啟用 H2 數據庫服務器模式

如何在 Spring Boot 中啟用 H2 數據庫服務器模式

浮云間 2022-10-20 17:29:16
我正在使用帶有使用 Spring Boot 的文件的 H2 數據庫。在我的 application.properties 中,我有這個條目:spring.datasource.url=jdbc:h2:file:c:/Testprojekte/spring-boot-h2-db但是現在我希望能夠在運行應用程序時查看數據庫,目前這是不可能的,因為我需要讓數據庫在服務器模式下運行才能這樣做。在文檔中,我發現我必須將 AUTO_SERVER=TRUE 添加到 URL 但這并不能解決問題。那么,我需要改變什么才能同時從不同的進程連接到該數據庫?謝謝你的幫助!托爾斯滕
查看完整描述

2 回答

?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

您可以將 H2 TCP 服務器作為 bean 啟動:


<dependency>

    <groupId>com.h2database</groupId>

    <artifactId>h2</artifactId>

    <!-- <scope>runtime</scope> -->

</dependency>

@SpringBootApplication

public class Application {


    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);

    }


    @Bean(initMethod = "start", destroyMethod = "stop")

    public Server h2Server() throws SQLException {

        return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9092");

    }

}

然后使用以下參數(密碼 - 空)從您的 IDE 連接到它:


url: jdbc:h2:tcp://localhost:9092/mem:testdb

user: sa

更多信息在這里這里。



查看完整回答
反對 回復 2022-10-20
?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

您可以使用瀏覽器中的 Web 界面啟用 h2 Web 控制臺以訪問內存或文件數據庫中的 h2。

因此添加 application.properties 行:

spring.h2.console.enabled=true
spring.h2.console.path=/h2-console

之后重新啟動您的 Spring Boot 應用程序并檢查http://localhost:8080/h2-console您的瀏覽器。


查看完整回答
反對 回復 2022-10-20
  • 2 回答
  • 0 關注
  • 293 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號