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
8e9e502b
Commit
8e9e502b
authored
Sep 01, 2014
by
David Liu
Committed by
Andy Wilkinson
Sep 07, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for auto-configuration of Commons DBCP2
Closes gh-1292 Closes gh-1477
parent
8ed46194
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
65 additions
and
23 deletions
+65
-23
pom.xml
spring-boot-autoconfigure/pom.xml
+5
-0
DataSourceBuilder.java
...gframework/boot/autoconfigure/jdbc/DataSourceBuilder.java
+2
-1
DataSourceAutoConfigurationTests.java
.../autoconfigure/jdbc/DataSourceAutoConfigurationTests.java
+49
-21
pom.xml
spring-boot-dependencies/pom.xml
+6
-0
spring-boot-features.adoc
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
+3
-1
No files found.
spring-boot-autoconfigure/pom.xml
View file @
8e9e502b
...
@@ -75,6 +75,11 @@
...
@@ -75,6 +75,11 @@
<artifactId>
activemq-pool
</artifactId>
<artifactId>
activemq-pool
</artifactId>
<optional>
true
</optional>
<optional>
true
</optional>
</dependency>
</dependency>
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-dbcp2
</artifactId>
<optional>
true
</optional>
</dependency>
<dependency>
<dependency>
<groupId>
org.apache.solr
</groupId>
<groupId>
org.apache.solr
</groupId>
<artifactId>
solr-solrj
</artifactId>
<artifactId>
solr-solrj
</artifactId>
...
...
spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceBuilder.java
View file @
8e9e502b
...
@@ -43,7 +43,8 @@ public class DataSourceBuilder {
...
@@ -43,7 +43,8 @@ public class DataSourceBuilder {
private
static
final
String
[]
DATA_SOURCE_TYPE_NAMES
=
new
String
[]
{
private
static
final
String
[]
DATA_SOURCE_TYPE_NAMES
=
new
String
[]
{
"org.apache.tomcat.jdbc.pool.DataSource"
,
"org.apache.tomcat.jdbc.pool.DataSource"
,
"com.zaxxer.hikari.HikariDataSource"
,
"com.zaxxer.hikari.HikariDataSource"
,
"org.apache.commons.dbcp.BasicDataSource"
};
"org.apache.commons.dbcp.BasicDataSource"
,
"org.apache.commons.dbcp2.BasicDataSource"
};
private
Class
<?
extends
DataSource
>
type
;
private
Class
<?
extends
DataSource
>
type
;
...
...
spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfigurationTests.java
View file @
8e9e502b
...
@@ -45,9 +45,11 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
...
@@ -45,9 +45,11 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
import
com.zaxxer.hikari.HikariDataSource
;
import
com.zaxxer.hikari.HikariDataSource
;
import
static
org
.
hamcrest
.
Matchers
.
instanceOf
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
/**
/**
...
@@ -70,8 +72,8 @@ public class DataSourceAutoConfigurationTests {
...
@@ -70,8 +72,8 @@ public class DataSourceAutoConfigurationTests {
@After
@After
public
void
restore
()
{
public
void
restore
()
{
EmbeddedDatabaseConnection
.
override
=
null
;
EmbeddedDatabaseConnection
.
override
=
null
;
if
(
context
!=
null
)
{
if
(
this
.
context
!=
null
)
{
context
.
close
();
this
.
context
.
close
();
}
}
}
}
...
@@ -119,28 +121,26 @@ public class DataSourceAutoConfigurationTests {
...
@@ -119,28 +121,26 @@ public class DataSourceAutoConfigurationTests {
@Test
@Test
public
void
testHikariIsFallback
()
throws
Exception
{
public
void
testHikariIsFallback
()
throws
Exception
{
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
HikariDataSource
pool
=
testDataSourceFallback
(
HikariDataSource
.
class
,
"spring.datasource.driverClassName:org.hsqldb.jdbcDriver"
,
"org.apache.tomcat"
);
"spring.datasource.url:jdbc:hsqldb:mem:testdb"
);
this
.
context
.
setClassLoader
(
new
URLClassLoader
(
new
URL
[
0
],
getClass
()
.
getClassLoader
())
{
@Override
protected
Class
<?>
loadClass
(
String
name
,
boolean
resolve
)
throws
ClassNotFoundException
{
if
(
name
.
startsWith
(
"org.apache.tomcat"
))
{
throw
new
ClassNotFoundException
();
}
return
super
.
loadClass
(
name
,
resolve
);
}
});
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
DataSource
bean
=
this
.
context
.
getBean
(
DataSource
.
class
);
HikariDataSource
pool
=
(
HikariDataSource
)
bean
;
assertEquals
(
"jdbc:hsqldb:mem:testdb"
,
pool
.
getJdbcUrl
());
assertEquals
(
"jdbc:hsqldb:mem:testdb"
,
pool
.
getJdbcUrl
());
}
}
@Test
public
void
commonsDbcpIsFallback
()
throws
Exception
{
BasicDataSource
dataSource
=
testDataSourceFallback
(
BasicDataSource
.
class
,
"org.apache.tomcat"
,
"com.zaxxer.hikari"
);
assertEquals
(
"jdbc:hsqldb:mem:testdb"
,
dataSource
.
getUrl
());
}
@Test
public
void
commonsDbcp2IsFallback
()
throws
Exception
{
org
.
apache
.
commons
.
dbcp2
.
BasicDataSource
dataSource
=
testDataSourceFallback
(
org
.
apache
.
commons
.
dbcp2
.
BasicDataSource
.
class
,
"org.apache.tomcat"
,
"com.zaxxer.hikari"
,
"org.apache.commons.dbcp."
);
assertEquals
(
"jdbc:hsqldb:mem:testdb"
,
dataSource
.
getUrl
());
}
@Test
@Test
public
void
testEmbeddedTypeDefaultsUsername
()
throws
Exception
{
public
void
testEmbeddedTypeDefaultsUsername
()
throws
Exception
{
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
...
@@ -215,6 +215,34 @@ public class DataSourceAutoConfigurationTests {
...
@@ -215,6 +215,34 @@ public class DataSourceAutoConfigurationTests {
assertNotNull
(
this
.
context
.
getBean
(
NamedParameterJdbcOperations
.
class
));
assertNotNull
(
this
.
context
.
getBean
(
NamedParameterJdbcOperations
.
class
));
}
}
@SuppressWarnings
(
"unchecked"
)
private
<
T
extends
DataSource
>
T
testDataSourceFallback
(
Class
<
T
>
expectedType
,
final
String
...
hiddenPackages
)
{
EnvironmentTestUtils
.
addEnvironment
(
this
.
context
,
"spring.datasource.driverClassName:org.hsqldb.jdbcDriver"
,
"spring.datasource.url:jdbc:hsqldb:mem:testdb"
);
this
.
context
.
setClassLoader
(
new
URLClassLoader
(
new
URL
[
0
],
getClass
()
.
getClassLoader
())
{
@Override
protected
Class
<?>
loadClass
(
String
name
,
boolean
resolve
)
throws
ClassNotFoundException
{
for
(
String
hiddenPackage
:
hiddenPackages
)
{
if
(
name
.
startsWith
(
hiddenPackage
))
{
throw
new
ClassNotFoundException
();
}
}
return
super
.
loadClass
(
name
,
resolve
);
}
});
this
.
context
.
register
(
DataSourceAutoConfiguration
.
class
,
PropertyPlaceholderAutoConfiguration
.
class
);
this
.
context
.
refresh
();
DataSource
bean
=
this
.
context
.
getBean
(
DataSource
.
class
);
assertThat
(
bean
,
instanceOf
(
expectedType
));
return
(
T
)
bean
;
}
@Configuration
@Configuration
static
class
TestDataSourceConfiguration
{
static
class
TestDataSourceConfiguration
{
...
...
spring-boot-dependencies/pom.xml
View file @
8e9e502b
...
@@ -54,6 +54,7 @@
...
@@ -54,6 +54,7 @@
<commons-beanutils.version>
1.9.2
</commons-beanutils.version>
<commons-beanutils.version>
1.9.2
</commons-beanutils.version>
<commons-collections.version>
3.2.1
</commons-collections.version>
<commons-collections.version>
3.2.1
</commons-collections.version>
<commons-dbcp.version>
1.4
</commons-dbcp.version>
<commons-dbcp.version>
1.4
</commons-dbcp.version>
<commons-dbcp2.version>
2.0.1
</commons-dbcp2.version>
<commons-digester.version>
2.1
</commons-digester.version>
<commons-digester.version>
2.1
</commons-digester.version>
<commons-pool.version>
1.6
</commons-pool.version>
<commons-pool.version>
1.6
</commons-pool.version>
<commons-pool2.version>
2.2
</commons-pool2.version>
<commons-pool2.version>
2.2
</commons-pool2.version>
...
@@ -484,6 +485,11 @@
...
@@ -484,6 +485,11 @@
<artifactId>
commons-collections
</artifactId>
<artifactId>
commons-collections
</artifactId>
<version>
${commons-collections.version}
</version>
<version>
${commons-collections.version}
</version>
</dependency>
</dependency>
<dependency>
<groupId>
org.apache.commons
</groupId>
<artifactId>
commons-dbcp2
</artifactId>
<version>
${commons-dbcp2.version}
</version>
</dependency>
<dependency>
<dependency>
<groupId>
commons-dbcp
</groupId>
<groupId>
commons-dbcp
</groupId>
<artifactId>
commons-dbcp
</artifactId>
<artifactId>
commons-dbcp
</artifactId>
...
...
spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc
View file @
8e9e502b
...
@@ -1249,7 +1249,9 @@ Production database connections can also be auto-configured using a pooling
...
@@ -1249,7 +1249,9 @@ Production database connections can also be auto-configured using a pooling
* We prefer the Tomcat pooling `DataSource` for its performance and concurrency, so if
* We prefer the Tomcat pooling `DataSource` for its performance and concurrency, so if
that is available we always choose it.
that is available we always choose it.
* If commons-dbcp is available we will use that, but we don't recommend it in production.
* If HikariCP is available we will use it
* If Commons DBCP is available we will use it, but we don't recommend it in production.
* Lastly, if Commons DBCP2 is available we will use it
If you use the `spring-boot-starter-jdbc` or `spring-boot-starter-data-jpa`
If you use the `spring-boot-starter-jdbc` or `spring-boot-starter-data-jpa`
``starter POMs'' you will automcatically get a dependency to `tomcat-jdbc`.
``starter POMs'' you will automcatically get a dependency to `tomcat-jdbc`.
...
...
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