Skip to content

Commit cb9e1f2

Browse files
thombergspedja4
authored andcommitted
added code example using HikariCP in Boot 1.x and 2.x (eugenp#5007)
* added code example using HikariCP in Boot 1.x and 2.x * code formatting
1 parent 110a74e commit cb9e1f2

5 files changed

Lines changed: 100 additions & 0 deletions

File tree

‎spring-4/pom.xml‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,27 @@
1919
<groupId>org.springframework.boot</groupId>
2020
<artifactId>spring-boot-starter-web</artifactId>
2121
</dependency>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-data-jpa</artifactId>
25+
<exclusions>
26+
<exclusion>
27+
<groupId>org.apache.tomcat</groupId>
28+
<artifactId>tomcat-jdbc</artifactId>
29+
</exclusion>
30+
</exclusions>
31+
</dependency>
32+
<dependency>
33+
<groupId>com.h2database</groupId>
34+
<artifactId>h2</artifactId>
35+
<version>${h2database.version}</version>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>com.zaxxer</groupId>
40+
<artifactId>HikariCP</artifactId>
41+
<version>${hikaricp.version}</version>
42+
</dependency>
2243
<dependency>
2344
<groupId>org.springframework.boot</groupId>
2445
<artifactId>spring-boot-starter-test</artifactId>
@@ -58,6 +79,7 @@
5879
<flips-web.version>1.0.1</flips-web.version>
5980
<lombok.version>1.16.18</lombok.version>
6081
<java.version>1.8</java.version>
82+
<h2database.version>1.4.197</h2database.version>
6183
</properties>
6284

6385
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.connectionpool;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ApplicationWithHikariConnectionPool {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(ApplicationWithHikariConnectionPool.class, args);
11+
}
12+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.connectionpool;
2+
3+
import static org.junit.Assert.*;
4+
5+
import javax.sql.DataSource;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
13+
@RunWith(SpringRunner.class)
14+
@SpringBootTest(
15+
// instead of setting this property, we can exclude the dependency to org.apache.tomcat:tomcat-jdbc in pom.xml
16+
properties = "spring.datasource.type=com.zaxxer.hikari.HikariDataSource")
17+
public class HikariIntegrationTest {
18+
19+
@Autowired
20+
private DataSource dataSource;
21+
22+
@Test
23+
public void hikariConnectionPoolIsConfigured() {
24+
assertEquals("com.zaxxer.hikari.HikariDataSource", dataSource.getClass()
25+
.getName());
26+
}
27+
28+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.connectionpool;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class ApplicationWithHikariConnectionPool {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(ApplicationWithHikariConnectionPool.class, args);
11+
}
12+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.connectionpool;
2+
3+
import static org.junit.Assert.*;
4+
5+
import javax.sql.DataSource;
6+
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.test.context.SpringBootTest;
11+
import org.springframework.test.context.junit4.SpringRunner;
12+
13+
@RunWith(SpringRunner.class)
14+
@SpringBootTest
15+
public class HikariIntegrationTest {
16+
17+
@Autowired
18+
private DataSource dataSource;
19+
20+
@Test
21+
public void hikariConnectionPoolIsConfigured() {
22+
assertEquals("com.zaxxer.hikari.HikariDataSource", dataSource.getClass()
23+
.getName());
24+
}
25+
26+
}

0 commit comments

Comments
 (0)