Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
800eb010
Commit
800eb010
authored
Apr 27, 2017
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Change the default JDBC connection pool to Hikari
Closes gh-6013
parent
2e491dfa
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
39 additions
and
37 deletions
+39
-37
PublicMetricsAutoConfigurationTests.java
...te/autoconfigure/PublicMetricsAutoConfigurationTests.java
+2
-1
DataSourceAutoConfiguration.java
.../boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java
+1
-1
DataSourceAutoConfigurationTests.java
.../autoconfigure/jdbc/DataSourceAutoConfigurationTests.java
+22
-23
DataSourceInitializerTests.java
...k/boot/autoconfigure/jdbc/DataSourceInitializerTests.java
+7
-6
CustomHibernateJpaAutoConfigurationTests.java
...ure/orm/jpa/CustomHibernateJpaAutoConfigurationTests.java
+1
-0
spring-boot-features.adoc
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
+6
-6
No files found.
spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/PublicMetricsAutoConfigurationTests.java
View file @
800eb010
...
...
@@ -126,9 +126,10 @@ public class PublicMetricsAutoConfigurationTests {
}
@Test
public
void
autoDataSource
()
{
public
void
autoDataSource
()
throws
SQLException
{
load
(
DataSourceAutoConfiguration
.
class
);
PublicMetrics
bean
=
this
.
context
.
getBean
(
DataSourcePublicMetrics
.
class
);
this
.
context
.
getBean
(
DataSource
.
class
).
getConnection
().
close
();
Collection
<
Metric
<?>>
metrics
=
bean
.
metrics
();
assertMetrics
(
metrics
,
"datasource.primary.active"
,
"datasource.primary.usage"
);
}
...
...
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration.java
View file @
800eb010
...
...
@@ -104,7 +104,7 @@ public class DataSourceAutoConfiguration {
@Configuration
@Conditional
(
PooledDataSourceCondition
.
class
)
@ConditionalOnMissingBean
({
DataSource
.
class
,
XADataSource
.
class
})
@Import
({
DataSourceConfiguration
.
Tomcat
.
class
,
DataSourceConfiguration
.
Hikari
.
class
,
@Import
({
DataSourceConfiguration
.
Hikari
.
class
,
DataSourceConfiguration
.
Tomcat
.
class
,
DataSourceConfiguration
.
Dbcp2
.
class
,
DataSourceConfiguration
.
Generic
.
class
})
protected
static
class
PooledDataSourceConfiguration
{
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java
View file @
800eb010
...
...
@@ -84,9 +84,8 @@ public class DataSourceAutoConfigurationTests {
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
dataSource
=
this
.
context
.
getBean
(
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
.
class
);
assertThat
(
dataSource
.
getUrl
()).
isNotNull
();
HikariDataSource
dataSource
=
this
.
context
.
getBean
(
HikariDataSource
.
class
);
assertThat
(
dataSource
.
getJdbcUrl
()).
isNotNull
();
assertThat
(
dataSource
.
getDriverClassName
()).
isNotNull
();
}
...
...
@@ -114,41 +113,41 @@ public class DataSourceAutoConfigurationTests {
}
@Test
public
void
tomcatValidatesConnectionByDefault
()
{
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
dataSource
=
autoConfigureDataSource
(
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
.
class
);
assertThat
(
dataSource
.
isTestOnBorrow
()).
isTrue
();
assertThat
(
dataSource
.
getValidationQuery
())
.
isEqualTo
(
DatabaseDriver
.
HSQLDB
.
getValidationQuery
());
public
void
hikariValidatesConnectionByDefault
()
throws
Exception
{
HikariDataSource
dataSource
=
autoConfigureDataSource
(
HikariDataSource
.
class
,
"org.apache.tomcat"
);
assertThat
(
dataSource
.
getConnectionTestQuery
()).
isNull
();
// Use Connection#isValid()
}
@Test
public
void
hikari
IsFallback
()
throws
Exception
{
HikariDataSource
dataSource
=
autoConfigureDataSource
(
HikariDataSource
.
class
,
"org.apache.tomcat
"
);
assertThat
(
dataSource
.
get
Jdbc
Url
()).
isEqualTo
(
"jdbc:hsqldb:mem:testdb"
);
public
void
tomcat
IsFallback
()
throws
Exception
{
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
dataSource
=
autoConfigureDataSource
(
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
.
class
,
"com.zaxxer.hikari
"
);
assertThat
(
dataSource
.
getUrl
()).
isEqualTo
(
"jdbc:hsqldb:mem:testdb"
);
}
@Test
public
void
hikariValidatesConnectionByDefault
()
throws
Exception
{
HikariDataSource
dataSource
=
autoConfigureDataSource
(
HikariDataSource
.
class
,
"org.apache.tomcat"
);
assertThat
(
dataSource
.
getConnectionTestQuery
()).
isNull
();
// Use Connection#isValid()
public
void
tomcatValidatesConnectionByDefault
()
{
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
dataSource
=
autoConfigureDataSource
(
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
.
class
,
"com.zaxxer.hikari"
);
assertThat
(
dataSource
.
isTestOnBorrow
()).
isTrue
();
assertThat
(
dataSource
.
getValidationQuery
())
.
isEqualTo
(
DatabaseDriver
.
HSQLDB
.
getValidationQuery
());
}
@Test
public
void
commonsDbcp2IsFallback
()
throws
Exception
{
BasicDataSource
dataSource
=
autoConfigureDataSource
(
BasicDataSource
.
class
,
"
org.apache.tomcat"
,
"com.zaxxer.hikari
"
);
"
com.zaxxer.hikari"
,
"org.apache.tomcat
"
);
assertThat
(
dataSource
.
getUrl
()).
isEqualTo
(
"jdbc:hsqldb:mem:testdb"
);
}
@Test
public
void
commonsDbcp2ValidatesConnectionByDefault
()
throws
Exception
{
org
.
apache
.
commons
.
dbcp2
.
BasicDataSource
dataSource
=
autoConfigureDataSource
(
org
.
apache
.
commons
.
dbcp2
.
BasicDataSource
.
class
,
"
org.apache.tomcat
"
,
"
com.zaxxer.hikari
"
);
org
.
apache
.
commons
.
dbcp2
.
BasicDataSource
.
class
,
"
com.zaxxer.hikari
"
,
"
org.apache.tomcat
"
);
assertThat
(
dataSource
.
getTestOnBorrow
()).
isEqualTo
(
true
);
assertThat
(
dataSource
.
getValidationQuery
()).
isNull
();
// Use Connection#isValid()
}
...
...
@@ -163,7 +162,7 @@ public class DataSourceAutoConfigurationTests {
this
.
context
.
refresh
();
DataSource
bean
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertThat
(
bean
).
isNotNull
();
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
pool
=
(
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
)
bean
;
HikariDataSource
pool
=
(
Hikari
DataSource
)
bean
;
assertThat
(
pool
.
getDriverClassName
()).
isEqualTo
(
"org.hsqldb.jdbcDriver"
);
assertThat
(
pool
.
getUsername
()).
isEqualTo
(
"sa"
);
}
...
...
@@ -215,7 +214,7 @@ public class DataSourceAutoConfigurationTests {
this
.
context
.
refresh
();
DataSource
bean
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertThat
(
bean
).
isNotNull
();
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
pool
=
(
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
)
bean
;
HikariDataSource
pool
=
(
Hikari
DataSource
)
bean
;
assertThat
(
pool
.
getDriverClassName
()).
isEqualTo
(
"org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfigurationTests$DatabaseTestDriver"
);
assertThat
(
pool
.
getUsername
()).
isNull
();
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializerTests.java
View file @
800eb010
...
...
@@ -24,6 +24,7 @@ import java.util.Random;
import
javax.sql.DataSource
;
import
com.zaxxer.hikari.HikariDataSource
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Rule
;
...
...
@@ -112,7 +113,7 @@ public class DataSourceInitializerTests {
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertThat
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
).
isTrue
(
);
assertThat
(
dataSource
).
isInstanceOf
(
HikariDataSource
.
class
);
assertThat
(
dataSource
).
isNotNull
();
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertThat
(
template
.
queryForObject
(
"SELECT COUNT(*) from BAR"
,
Integer
.
class
))
...
...
@@ -131,7 +132,7 @@ public class DataSourceInitializerTests {
.
addResourcePathToPackagePath
(
getClass
(),
"data.sql"
));
this
.
context
.
refresh
();
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertThat
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
).
isTrue
(
);
assertThat
(
dataSource
).
isInstanceOf
(
HikariDataSource
.
class
);
assertThat
(
dataSource
).
isNotNull
();
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertThat
(
template
.
queryForObject
(
"SELECT COUNT(*) from FOO"
,
Integer
.
class
))
...
...
@@ -154,7 +155,7 @@ public class DataSourceInitializerTests {
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertThat
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
).
isTrue
(
);
assertThat
(
dataSource
).
isInstanceOf
(
HikariDataSource
.
class
);
assertThat
(
dataSource
).
isNotNull
();
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertThat
(
template
.
queryForObject
(
"SELECT COUNT(*) from FOO"
,
Integer
.
class
))
...
...
@@ -177,7 +178,7 @@ public class DataSourceInitializerTests {
.
addResourcePathToPackagePath
(
getClass
(),
"encoding-data.sql"
));
this
.
context
.
refresh
();
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertThat
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
).
isTrue
(
);
assertThat
(
dataSource
).
isInstanceOf
(
HikariDataSource
.
class
);
assertThat
(
dataSource
).
isNotNull
();
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertThat
(
template
.
queryForObject
(
"SELECT COUNT(*) from BAR"
,
Integer
.
class
))
...
...
@@ -197,7 +198,7 @@ public class DataSourceInitializerTests {
this
.
context
.
refresh
();
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
this
.
context
.
publishEvent
(
new
DataSourceInitializedEvent
(
dataSource
));
assertThat
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
).
isTrue
(
);
assertThat
(
dataSource
).
isInstanceOf
(
HikariDataSource
.
class
);
assertThat
(
dataSource
).
isNotNull
();
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
try
{
...
...
@@ -270,7 +271,7 @@ public class DataSourceInitializerTests {
this
.
context
.
setResourceLoader
(
resourceLoader
);
this
.
context
.
refresh
();
DataSource
dataSource
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertThat
(
dataSource
instanceof
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
).
isTrue
(
);
assertThat
(
dataSource
).
isInstanceOf
(
HikariDataSource
.
class
);
assertThat
(
dataSource
).
isNotNull
();
JdbcOperations
template
=
new
JdbcTemplate
(
dataSource
);
assertThat
(
template
.
queryForObject
(
"SELECT COUNT(*) from FOO"
,
Integer
.
class
))
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/CustomHibernateJpaAutoConfigurationTests.java
View file @
800eb010
...
...
@@ -64,6 +64,7 @@ public class CustomHibernateJpaAutoConfigurationTests {
// Set up environment so we get a MySQL database but don't require server to be
// running...
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.datasource.type:"
+
org
.
apache
.
tomcat
.
jdbc
.
pool
.
DataSource
.
class
.
getName
(),
"spring.datasource.database:mysql"
,
"spring.datasource.url:jdbc:mysql://localhost/nonexistent"
,
"spring.datasource.initialize:false"
,
"spring.jpa.database:MYSQL"
);
...
...
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
View file @
800eb010
...
...
@@ -2821,10 +2821,10 @@ ensuring that it happens once access to the database is no longer needed.
Production database connections can also be auto-configured using a pooling `DataSource`.
Here's the algorithm for choosing a specific implementation:
* We prefer
the Tomcat pooling `DataSource` for its performance and concurrency, so if
that is available we
always choose it.
* Otherwise, if
HikariCP
is available we will use it.
* If neither
the Tomcat pooling datasource nor HikariCP
are available and if Commons
* We prefer
HikariCP for its performance and concurrency, so if that is available we
always choose it.
* Otherwise, if
the Tomcat pooling `DataSource`
is available we will use it.
* If neither
HikariCP nor the Tomcat pooling datasource
are available and if Commons
DBCP2 is available we will use it.
If you use the `spring-boot-starter-jdbc` or `spring-boot-starter-data-jpa`
...
...
@@ -2863,8 +2863,8 @@ loadable.
See {sc-spring-boot-autoconfigure}/jdbc/DataSourceProperties.{sc-ext}[`DataSourceProperties`]
for more of the supported options. These are the standard options that work regardless of
the actual implementation. It is also possible to fine-tune implementation-specific
settings using their respective prefix (`+spring.datasource.
tomcat
.*+`,
`+spring.datasource.
hikari
.*+`, and `+spring.datasource.dbcp2.*+`). Refer to the
settings using their respective prefix (`+spring.datasource.
hikari
.*+`,
`+spring.datasource.
tomcat
.*+`, and `+spring.datasource.dbcp2.*+`). Refer to the
documentation of the connection pool implementation you are using for more details.
For instance, if you are using the
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment