Skip to content

Commit 1dea2b8

Browse files
micropatelKevinGilmore
authored andcommitted
BAEL-1980 Access an in-memory h2 db from 2 Spring Boot applications (eugenp#5230)
* Added Spring-Boot-H2 project * Optimized imports and formatted code * Code Formatting changes * Renamed the client applicaiton * Removed Name from pom.xml * formatting changes - applied formatting style * added souts * Update SpringBootApp.java * Update ClientSpringBootApp.java * Fixed Formatting issues * Fixed formatting and spelling mistakes * added H2 shared db example * a * patch * test * Revert "Fixed formatting and spelling mistakes" This reverts commit ed56ede. * Revert "test" This reverts commit 4a7133d. * Revert "patch" This reverts commit 404b9eb. * Revert "a" This reverts commit af77f84. * Revert "added H2 shared db example" This reverts commit e16918c. * Updated to have client and server in same directory * Deleted old files * removed unwanted directory * formating * removed unwanted module * commentted removed module * Revert "commentted removed module" This reverts commit 211ff14. * Revert "removed unwanted module" This reverts commit 657f9ee. * fixed pom.xml issue * typo fixed * fixed multiple main class issue
1 parent 80aabf2 commit 1dea2b8

File tree

7 files changed

+15
-93
lines changed

7 files changed

+15
-93
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@
675675
<!-- <module>rule-engines/rulebook</module> --><!-- Wrong Parent -->
676676
<module>spring-boot-custom-starter/greeter</module>
677677
<module>spring-boot-h2/spring-boot-h2-database</module>
678-
<module>spring-boot-h2/spring-boot-h2-remote-app</module>
678+
<!--module>spring-boot-h2/spring-boot-h2-remote-app</module-->
679679
<!-- <module>guest\webservices\rest-client</module> --><!-- guest post on different site -->
680680
<!-- <module>guest\webservices\rest-server</module> --><!-- PMD voilation -->
681681
<!-- <module>guest\webservices\spring-rest-service</module> --><!-- guest post on different site -->
@@ -992,7 +992,7 @@
992992
<!-- <module>rule-engines/rulebook</module> --><!-- Wrong Parent -->
993993
<module>spring-boot-custom-starter/greeter</module>
994994
<module>spring-boot-h2/spring-boot-h2-database</module>
995-
<module>spring-boot-h2/spring-boot-h2-remote-app</module>
995+
<!--module>spring-boot-h2/spring-boot-h2-remote-app</module-->
996996
<!-- <module>guest\webservices\rest-client</module> --><!-- guest post on different site -->
997997
<!-- <module>guest\webservices\rest-server</module> --><!-- PMD voilation -->
998998
<!-- <module>guest\webservices\spring-rest-service</module> --><!-- guest post on different site -->

spring-boot-h2/spring-boot-h2-database/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2323
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
2424
<java.version>1.8</java.version>
25+
<!-- The main class to start by executing java -jar -->
26+
<start-class>com.mycorp.starter.HelloWorldApplication</start-class>
2527
</properties>
2628

2729
<dependencies>

spring-boot-h2/spring-boot-h2-remote-app/src/main/java/com/baeldung/h2db/demo/ClientSpringBootApp.java renamed to spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/client/ClientSpringBootApp.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.h2db.demo;
1+
package com.baeldung.h2db.demo.client;
22

33
import java.sql.ResultSet;
44
import java.sql.SQLException;
@@ -7,16 +7,20 @@
77
import org.springframework.beans.factory.annotation.Autowired;
88
import org.springframework.boot.SpringApplication;
99
import org.springframework.boot.autoconfigure.SpringBootApplication;
10+
import org.springframework.context.annotation.ComponentScan;
11+
import org.springframework.context.annotation.PropertySource;
1012
import org.springframework.jdbc.core.JdbcTemplate;
1113
import org.springframework.jdbc.core.RowMapper;
1214

1315
@SpringBootApplication
16+
@ComponentScan("com.baeldung.h2db.demo.client")
1417
public class ClientSpringBootApp {
1518

1619
@Autowired
1720
private JdbcTemplate jdbcTemplate;
1821

1922
public static void main(String[] args) {
23+
System.setProperty("spring.datasource.url","jdbc:h2:tcp://localhost:9091/mem:mydb");
2024
SpringApplication.run(ClientSpringBootApp.class, args);
2125
}
2226

@@ -46,4 +50,4 @@ public Object mapRow(ResultSet rs, int i) throws SQLException {
4650
}
4751
});
4852
}
49-
}
53+
}

spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/SpringBootApp.java renamed to spring-boot-h2/spring-boot-h2-database/src/main/java/com/baeldung/h2db/demo/server/SpringBootApp.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.baeldung.h2db.demo;
1+
package com.baeldung.h2db.demo.server;
22

33
import java.sql.ResultSet;
44
import java.sql.SQLException;
@@ -9,10 +9,12 @@
99
import org.springframework.boot.SpringApplication;
1010
import org.springframework.boot.autoconfigure.SpringBootApplication;
1111
import org.springframework.context.annotation.Bean;
12+
import org.springframework.context.annotation.ComponentScan;
1213
import org.springframework.jdbc.core.JdbcTemplate;
1314
import org.springframework.jdbc.core.RowMapper;
1415

1516
@SpringBootApplication
17+
@ComponentScan("com.baeldung.h2db.demo.server")
1618
public class SpringBootApp {
1719

1820
@Autowired
@@ -24,8 +26,7 @@ public static void main(String[] args) {
2426

2527
@PostConstruct
2628
private void initDb() {
27-
System.out.println(String.format(
28-
"****** Creating table: %s, and Inserting test data ******", "Employees"));
29+
System.out.println(String.format("****** Creating table: %s, and Inserting test data ******", "Employees"));
2930

3031
String sqlStatements[] = {
3132
"drop table employees if exists",
@@ -57,4 +58,4 @@ public Object mapRow(ResultSet rs, int i) throws SQLException {
5758
public Server inMemoryH2DatabaseServer() throws SQLException {
5859
return Server.createTcpServer("-tcp", "-tcpAllowOthers", "-tcpPort", "9091");
5960
}
60-
}
61+
}

spring-boot-h2/spring-boot-h2-remote-app/.gitignore

Lines changed: 0 additions & 25 deletions
This file was deleted.

spring-boot-h2/spring-boot-h2-remote-app/pom.xml

Lines changed: 0 additions & 55 deletions
This file was deleted.

spring-boot-h2/spring-boot-h2-remote-app/src/main/resources/application.properties

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)