-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathH2ServerConfiguration.java
56 lines (49 loc) · 1.87 KB
/
H2ServerConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.lambdaschool.usermodel.config;
import org.h2.tools.Server;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.sql.SQLException;
@Configuration
// taken from https://techdev.io/en/developer-blog/querying-the-embedded-h2-database-of-a-spring-boot-application
// necessary for using the database tool built into intellij
public class H2ServerConfiguration
{
// TCP port for remote connections, default 9092
@Value("${h2.tcp.port:9092}")
private String h2TcpPort;
// Web port, default 8082
@Value("${h2.web.port:8082}")
private String h2WebPort;
/**
* TCP connection to connect with SQL clients to the embedded h2 database.
* <p>
* Connect to "jdbc:h2:tcp://localhost:9092/mem:testdb", username "sa", password empty.
*/
@Bean
@ConditionalOnExpression("${h2.tcp.enabled:true}")
public Server h2TcpServer() throws SQLException
{
return Server.createTcpServer("-tcp",
"-tcpAllowOthers",
"-tcpPort",
h2TcpPort)
.start();
}
/**
* Web console for the embedded h2 database.
* <p>
* Go to http://localhost:8082 and connect to the database "jdbc:h2:mem:testdb", username "sa", password empty.
*/
@Bean
@ConditionalOnExpression("${h2.web.enabled:true}")
public Server h2WebServer() throws SQLException
{
return Server.createWebServer("-web",
"-webAllowOthers",
"-webPort",
h2WebPort)
.start();
}
}