Skip to content

Commit d54aab2

Browse files
committed
Log connection URL for embedded databases
Prior to this commit, EmbeddedDatabaseFactory logged the name of an embedded database just before it was created; however, the name alone is not sufficient for connecting to the database via external tools such as the H2 Web Console. This commit updates EmbeddedDatabaseFactory to make it easier to connect to embedded databases via external tools by logging the complete connection URL for an embedded database as it's being created. In addition, EmbeddedDatabaseFactory now logs when an embedded database is being shut down. Issue: SPR-13370
1 parent 2025d5c commit d54aab2

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

spring-jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseFactory.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
import java.sql.SQLException;
2222
import java.util.UUID;
2323
import java.util.logging.Logger;
24-
2524
import javax.sql.DataSource;
2625

2726
import org.apache.commons.logging.Log;
2827
import org.apache.commons.logging.LogFactory;
2928

29+
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
3030
import org.springframework.jdbc.datasource.init.DatabasePopulator;
3131
import org.springframework.jdbc.datasource.init.DatabasePopulatorUtils;
3232
import org.springframework.util.Assert;
@@ -63,7 +63,7 @@
6363
public class EmbeddedDatabaseFactory {
6464

6565
/**
66-
* Default name for an embedded database: "testdb".
66+
* Default name for an embedded database: {@value}
6767
*/
6868
public static final String DEFAULT_DATABASE_NAME = "testdb";
6969

@@ -171,17 +171,25 @@ protected void initDatabase() {
171171
setDatabaseName(UUID.randomUUID().toString());
172172
}
173173

174-
// Create the embedded database source first
175-
if (logger.isInfoEnabled()) {
176-
logger.info("Creating embedded database '" + this.databaseName + "'");
177-
}
174+
// Create the embedded database first
178175
if (this.databaseConfigurer == null) {
179176
this.databaseConfigurer = EmbeddedDatabaseConfigurerFactory.getConfigurer(EmbeddedDatabaseType.HSQL);
180177
}
181178
this.databaseConfigurer.configureConnectionProperties(
182179
this.dataSourceFactory.getConnectionProperties(), this.databaseName);
183180
this.dataSource = this.dataSourceFactory.getDataSource();
184181

182+
if (logger.isInfoEnabled()) {
183+
if (this.dataSource instanceof SimpleDriverDataSource) {
184+
SimpleDriverDataSource simpleDriverDataSource = (SimpleDriverDataSource) this.dataSource;
185+
logger.info(String.format("Starting embedded database: url='%s', username='%s'",
186+
simpleDriverDataSource.getUrl(), simpleDriverDataSource.getUsername()));
187+
}
188+
else {
189+
logger.info(String.format("Starting embedded database '%s'", this.databaseName));
190+
}
191+
}
192+
185193
// Now populate the database
186194
if (this.databasePopulator != null) {
187195
try {
@@ -203,6 +211,17 @@ protected void initDatabase() {
203211
*/
204212
protected void shutdownDatabase() {
205213
if (this.dataSource != null) {
214+
215+
if (logger.isInfoEnabled()) {
216+
if (this.dataSource instanceof SimpleDriverDataSource) {
217+
logger.info(String.format("Shutting down embedded database: url='%s'",
218+
((SimpleDriverDataSource) this.dataSource).getUrl()));
219+
}
220+
else {
221+
logger.info(String.format("Shutting down embedded database '%s'", this.databaseName));
222+
}
223+
}
224+
206225
this.databaseConfigurer.shutdown(this.dataSource, this.databaseName);
207226
this.dataSource = null;
208227
}
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
log4j.appender.console=org.apache.log4j.ConsoleAppender
22
log4j.appender.console.layout=org.apache.log4j.PatternLayout
3-
log4j.appender.console.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%c] - %m%n
3+
log4j.appender.console.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%-5p] [%c] - %m%n
4+
5+
log4j.appender.file=org.apache.log4j.FileAppender
6+
log4j.appender.file.file=bin/spring-jdbc.log
7+
log4j.appender.file.layout=org.apache.log4j.PatternLayout
8+
log4j.appender.file.layout.ConversionPattern=%d{HH:mm:ss,SSS} [%c] - %m%n
9+
10+
log4j.rootCategory=ERROR, console, file
411

5-
log4j.rootCategory=WARN, console
612
log4j.logger.org.springframework.beans=WARN
713
log4j.logger.org.springframework.binding=DEBUG
814

915
#log4j.logger.org.springframework.jdbc=TRACE
10-
16+
#log4j.logger.org.springframework.jdbc.datasource.embedded=INFO

0 commit comments

Comments
 (0)