Skip to content

Commit 19d550c

Browse files
author
tom
committed
First shot
1 parent 7cb4c8f commit 19d550c

File tree

12 files changed

+488
-1
lines changed

12 files changed

+488
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# spring-boot-multi-hsql
22
A Spring-Boot-Example with an unlimted an easy to configure HSQL file based _permanent_ database
33

4-
### My intro to GitHub ... comming soon
4+
### My intro to GitHub
55
### Description
66

77
From time to time I need more than one _independet_ und _permanent_ databases to

pom.xml

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.springjunky</groupId>
7+
<artifactId>multihsql</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>multi-hsql</name>
12+
<description>Spring Boot with HSQL-Server</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.2.RELEASE</version>
18+
<relativePath /> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-data-jpa</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-web</artifactId>
39+
</dependency>
40+
41+
<dependency>
42+
<groupId>org.hsqldb</groupId>
43+
<artifactId>hsqldb</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.springframework.boot</groupId>
47+
<artifactId>spring-boot-starter-test</artifactId>
48+
<scope>test</scope>
49+
</dependency>
50+
<dependency>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-configuration-processor</artifactId>
53+
<optional>true</optional>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>commons-dbcp</groupId>
58+
<artifactId>commons-dbcp</artifactId>
59+
60+
</dependency>
61+
62+
</dependencies>
63+
64+
<build>
65+
<plugins>
66+
<plugin>
67+
<groupId>org.springframework.boot</groupId>
68+
<artifactId>spring-boot-maven-plugin</artifactId>
69+
</plugin>
70+
</plugins>
71+
</build>
72+
73+
<repositories>
74+
<repository>
75+
<id>spring-milestones</id>
76+
<name>Spring Milestones</name>
77+
<url>https://repo.spring.io/milestone</url>
78+
<snapshots>
79+
<enabled>false</enabled>
80+
</snapshots>
81+
</repository>
82+
</repositories>
83+
84+
85+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.springjunky;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class DemoApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(DemoApplication.class, args);
11+
}
12+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.springjunky.configuration;
2+
3+
import javax.sql.DataSource;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.beans.factory.annotation.Qualifier;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
import org.springframework.context.annotation.Primary;
12+
13+
import com.springjunky.hsqlfileserver.HSQLFileDatabaseServer;
14+
15+
@Configuration
16+
public class DataSourceConfig {
17+
18+
static Logger logger = LoggerFactory.getLogger(DataSourceConfig.class);
19+
20+
@Autowired
21+
@Qualifier("SourceDB")
22+
HSQLFileDatabaseServer sourceDatabase;
23+
24+
@Autowired
25+
@Qualifier("DestDB")
26+
HSQLFileDatabaseServer destDatabase;
27+
28+
@Bean("SourceDB-DataSource")
29+
@Primary // Spring's JPA Autoconfig needs a hint :-)
30+
public DataSource sourceDatabaseDataSource() {
31+
logger.info("Creating Datasource of SourceDB");
32+
return sourceDatabase.getBasicDataSourceForFileDatabaseServer();
33+
}
34+
35+
@Bean("DestDB-DataSource")
36+
public DataSource destDatabaseDataSource() {
37+
logger.info("Creating Datasource of DestDB ");
38+
return destDatabase.getBasicDataSourceForFileDatabaseServer();
39+
}
40+
41+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* @(#) $HeadURL$
2+
* Hier folgt eine kurze Beschreibung der Datei
3+
*
4+
* Copyright © DEVK 2009. All rights reserved.
5+
*
6+
* $Id$
7+
*/
8+
9+
package com.springjunky.configuration;
10+
11+
import org.springframework.boot.context.properties.ConfigurationProperties;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.Configuration;
14+
15+
import com.springjunky.hsqlfileserver.HSQLFileDatabaseServer;
16+
import com.springjunky.hsqlfileserver.HSQLFileDatabaseServerImpl;
17+
18+
/**
19+
*/
20+
@Configuration
21+
public class FileDatabaseConfig {
22+
23+
@ConfigurationProperties(prefix = "source.database")
24+
@Bean("SourceDB")
25+
public HSQLFileDatabaseServer sourceDataBase() {
26+
// Just return the Bean the @PostConstruct starts it for you
27+
return new HSQLFileDatabaseServerImpl();
28+
}
29+
30+
@ConfigurationProperties(prefix = "dest.database")
31+
@Bean("DestDB")
32+
public HSQLFileDatabaseServer destDataBase() {
33+
return new HSQLFileDatabaseServerImpl();
34+
}
35+
36+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
package com.springjunky.hsqlfileserver;
3+
4+
import javax.sql.DataSource;
5+
6+
/**
7+
*/
8+
public interface HSQLFileDatabaseServer {
9+
10+
public String getJdbcConnectionString();
11+
12+
public DataSource getBasicDataSourceForFileDatabaseServer();
13+
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package com.springjunky.hsqlfileserver;
2+
3+
import java.io.File;
4+
import java.net.Inet4Address;
5+
import java.net.UnknownHostException;
6+
import java.util.Objects;
7+
import java.util.Optional;
8+
9+
import javax.annotation.PostConstruct;
10+
import javax.annotation.PreDestroy;
11+
import javax.sql.DataSource;
12+
import javax.validation.constraints.NotNull;
13+
14+
import org.apache.commons.dbcp.BasicDataSource;
15+
import org.hsqldb.Server;
16+
import org.hsqldb.server.ServerConstants;
17+
import org.slf4j.Logger;
18+
import org.slf4j.LoggerFactory;
19+
import org.springframework.core.io.ClassPathResource;
20+
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
21+
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
22+
23+
/**
24+
* Implementaion of the simple Database.
25+
*
26+
* @author springjunky
27+
*/
28+
public class HSQLFileDatabaseServerImpl implements HSQLFileDatabaseServer {
29+
30+
private static final Logger logger = LoggerFactory.getLogger(HSQLFileDatabaseServerImpl.class);
31+
32+
private static final String ORG_HSQLDB_JDBC_DRIVER = "org.hsqldb.jdbcDriver";
33+
34+
private static final String EMPTY = "";
35+
36+
@NotNull
37+
private Integer serverPort;
38+
39+
@NotNull
40+
private Integer dbNumber;
41+
42+
@NotNull
43+
private String dbName;
44+
45+
@NotNull
46+
private Boolean dropAndCreate;
47+
48+
private String createScript;
49+
50+
private String fillScript;
51+
52+
private Server server;
53+
54+
@PostConstruct
55+
public void startServer() {
56+
if (server == null) {
57+
logger.info("Starting HSQL server with file Database see target{}HSQLDatabase{}{} ...", File.separator,
58+
File.separator, dbName);
59+
server = new Server();
60+
try {
61+
server.setPort(serverPort);
62+
server.setDatabaseName(dbNumber, dbName);
63+
server.setDatabasePath(dbNumber,
64+
"file:target" + File.separator + "HSQLDatabase" + File.separator + dbName);
65+
server.setSilent(false);
66+
server.start();
67+
logger.info("Server is online, executing scipt with {} ", getJdbcConnectionString());
68+
if (dropAndCreate) {
69+
logger.info("Populating database {}", dbName);
70+
databasePopulate();
71+
}
72+
} catch (Exception ex) {
73+
logger.warn("Exception during startup ", ex);
74+
}
75+
}
76+
}
77+
78+
@PreDestroy
79+
public void shutdownServer() {
80+
if (server.getState() == ServerConstants.SERVER_STATE_ONLINE) {
81+
server.shutdown();
82+
logger.info("HSQL server at target{}HSQLDatabase{}{} stopped", File.separator, File.separator, dbName);
83+
}
84+
}
85+
86+
/*
87+
* Get your jdbc-Connection-String
88+
*/
89+
@Override
90+
public DataSource getBasicDataSourceForFileDatabaseServer() {
91+
BasicDataSource dataSource = new BasicDataSource();
92+
dataSource.setDriverClassName(ORG_HSQLDB_JDBC_DRIVER);
93+
String jdbcConnectionString = this.getJdbcConnectionString();
94+
dataSource.setUrl(jdbcConnectionString);
95+
logger.info("JDBC-Connection-String:{}", jdbcConnectionString);
96+
return dataSource;
97+
}
98+
99+
@Override
100+
public String getJdbcConnectionString() {
101+
String hostName;
102+
try {
103+
hostName = Inet4Address.getLocalHost().getHostName();
104+
} catch (UnknownHostException ex) {
105+
hostName = "unknown";
106+
logger.warn("Your hostname freaks out ", ex);
107+
}
108+
return "jdbc:hsqldb:hsql://" + hostName + ":" + serverPort + "/" + dbName;
109+
}
110+
111+
private void databasePopulate() {
112+
DataSource dataSource = getBasicDataSourceForFileDatabaseServer();
113+
ClassPathResource createScriptResource = new ClassPathResource(
114+
Objects.requireNonNull(createScript, "must provide a createScript"));
115+
Optional<String> optionalFillScript = Optional.ofNullable(fillScript);
116+
ClassPathResource fillScriptResource = new ClassPathResource(optionalFillScript.orElse(EMPTY));
117+
ResourceDatabasePopulator populator = new ResourceDatabasePopulator(createScriptResource, fillScriptResource);
118+
populator.setContinueOnError(true);
119+
populator.setIgnoreFailedDrops(true);
120+
DatabasePopulatorUtils.execute(populator, dataSource);
121+
}
122+
123+
// All the boring setter / getter need by @ConfigurationProperties for a
124+
// type-safe binding
125+
public Integer getServerPort() {
126+
return serverPort;
127+
}
128+
129+
public void setServerPort(Integer serverPort) {
130+
this.serverPort = serverPort;
131+
}
132+
133+
public Integer getDbNumber() {
134+
return dbNumber;
135+
}
136+
137+
public void setDbNumber(Integer dbNumber) {
138+
this.dbNumber = dbNumber;
139+
}
140+
141+
public String getDbName() {
142+
return dbName;
143+
}
144+
145+
public void setDbName(String dbName) {
146+
this.dbName = dbName;
147+
}
148+
149+
public String getCreateScript() {
150+
return createScript;
151+
}
152+
153+
public void setCreateScript(String createScript) {
154+
this.createScript = createScript;
155+
}
156+
157+
public String getFillScript() {
158+
return fillScript;
159+
}
160+
161+
public void setFillScript(String fillScript) {
162+
this.fillScript = fillScript;
163+
}
164+
165+
public Boolean getDropAndCreate() {
166+
return dropAndCreate;
167+
}
168+
169+
public void setDropAndCreate(Boolean dropAndCreate) {
170+
this.dropAndCreate = dropAndCreate;
171+
}
172+
173+
}

0 commit comments

Comments
 (0)