Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
719 views
in Technique[技术] by (71.8m points)

java - How to enable H2 Database Server Mode in Spring Boot

I'm using a H2 database with a file using Spring Boot.

In my application.properties, I have this entry:

spring.datasource.url=jdbc:h2:file:c:/Testprojekte/spring-boot-h2-db

But now I would like to be able to look at the database while running the application, which currently isn't possible because I need to have the database running in server mode in order to do so. In the documentation I found that I have to add AUTO_SERVER=TRUE to the URL but this doesn't solve the problem.

So, what do I have to change to be able to connect to that database from different processes at the same time ?

thanks for any help! Thorsten

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can start the H2 TCP server as a 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");
    }
}

Then connect to it from your IDE with the following params (password - empty):

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

More info is here and here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...