BAEL-3075 - Initial commit
This commit is contained in:
parent
ae8fb70e40
commit
6d60aad5f9
|
@ -1,3 +1,4 @@
|
||||||
### Relevant Articles:
|
### Relevant Articles:
|
||||||
|
|
||||||
- [Using JDBI with Spring Boot](https://www.baeldung.com/spring-boot-jdbi)
|
- [Using JDBI with Spring Boot](https://www.baeldung.com/spring-boot-jdbi)
|
||||||
|
- [Oracle Connection Pooling With Spring](https://www.baeldung.com/oracle-connection-pooling-with-spring)
|
|
@ -43,7 +43,17 @@
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-jdbc</artifactId>
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>net.bytebuddy</groupId>
|
||||||
|
<artifactId>byte-buddy</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -82,6 +92,47 @@
|
||||||
<artifactId>spring-boot-starter-test</artifactId>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.oracle</groupId>
|
||||||
|
<artifactId>ojdbc8</artifactId>
|
||||||
|
<version>12.2.0.1</version>
|
||||||
|
<scope>system</scope>
|
||||||
|
<systemPath>${basedir}/lib/ojdbc8.jar</systemPath>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.oracle</groupId>
|
||||||
|
<artifactId>ucp</artifactId>
|
||||||
|
<version>12.2.0.1</version>
|
||||||
|
<scope>system</scope>
|
||||||
|
<systemPath>${basedir}/lib/ucp.jar</systemPath>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.oracle</groupId>
|
||||||
|
<artifactId>ons</artifactId>
|
||||||
|
<version>12.2.0.1</version>
|
||||||
|
<scope>system</scope>
|
||||||
|
<systemPath>${basedir}/lib/ons.jar</systemPath>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.mchange</groupId>
|
||||||
|
<artifactId>c3p0</artifactId>
|
||||||
|
<version>${c3p0.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-dbcp2</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.tomcat</groupId>
|
||||||
|
<artifactId>tomcat-jdbc</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
|
@ -96,6 +147,7 @@
|
||||||
<properties>
|
<properties>
|
||||||
<jdbi.version>3.9.1</jdbi.version>
|
<jdbi.version>3.9.1</jdbi.version>
|
||||||
<spring.boot.dependencies>2.1.8.RELEASE</spring.boot.dependencies>
|
<spring.boot.dependencies>2.1.8.RELEASE</spring.boot.dependencies>
|
||||||
|
<c3p0.version>0.9.5.2</c3p0.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|
|
@ -0,0 +1,29 @@
|
||||||
|
package com.baeldung.spring.oracle.pooling;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.CommandLineRunner;
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@Slf4j
|
||||||
|
public class SpringOraclePoolingApplication implements CommandLineRunner{
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DataSource dataSource;
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringOraclePoolingApplication.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(String... args) throws Exception {
|
||||||
|
log.info("Connection Polling datasource : "+ dataSource);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.baeldung.spring.oracle.pooling.configuration;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
|
||||||
|
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@Profile("c3p0")
|
||||||
|
public class C3P0Configuration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DataSource dataSource() throws SQLException {
|
||||||
|
ComboPooledDataSource dataSource = new ComboPooledDataSource();
|
||||||
|
dataSource.setUser("books");
|
||||||
|
dataSource.setPassword("books");
|
||||||
|
dataSource.setJdbcUrl("jdbc:oracle:thin:@//localhost:11521/ORCLPDB1");
|
||||||
|
dataSource.setMinPoolSize(5);
|
||||||
|
dataSource.setMaxPoolSize(10);
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.baeldung.spring.oracle.pooling.configuration;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
|
||||||
|
import oracle.jdbc.pool.OracleDataSource;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@Profile("oracle")
|
||||||
|
public class OracleConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DataSource dataSource() throws SQLException {
|
||||||
|
OracleDataSource dataSource = new OracleDataSource();
|
||||||
|
dataSource.setUser("books");
|
||||||
|
dataSource.setPassword("books");
|
||||||
|
dataSource.setURL("jdbc:oracle:thin:@//localhost:11521/ORCLPDB1");
|
||||||
|
dataSource.setFastConnectionFailoverEnabled(true);
|
||||||
|
dataSource.setImplicitCachingEnabled(true);
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.baeldung.spring.oracle.pooling.configuration;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Profile;
|
||||||
|
|
||||||
|
import oracle.ucp.jdbc.PoolDataSource;
|
||||||
|
import oracle.ucp.jdbc.PoolDataSourceFactory;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@Profile("oracle-ucp")
|
||||||
|
public class OracleUCPConfiguration {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public DataSource dataSource() throws SQLException {
|
||||||
|
PoolDataSource dataSource = PoolDataSourceFactory.getPoolDataSource();
|
||||||
|
dataSource.setUser("books");
|
||||||
|
dataSource.setPassword("books");
|
||||||
|
dataSource.setConnectionFactoryClassName("oracle.jdbc.pool.OracleDataSource");
|
||||||
|
dataSource.setURL("jdbc:oracle:thin:@//localhost:11521/ORCLPDB1");
|
||||||
|
dataSource.setFastConnectionFailoverEnabled(true);
|
||||||
|
dataSource.setInitialPoolSize(5);
|
||||||
|
dataSource.setMaxPoolSize(10);
|
||||||
|
return dataSource;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
package com.baeldung.spring.oracle.pooling.controller;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import com.baeldung.spring.oracle.pooling.entity.Book;
|
||||||
|
import com.baeldung.spring.oracle.pooling.exception.BookstoreException;
|
||||||
|
import com.baeldung.spring.oracle.pooling.repository.BookRepository;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/books")
|
||||||
|
public class BookstoreController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private BookRepository repository;
|
||||||
|
|
||||||
|
@GetMapping(value = "/hello")
|
||||||
|
public String sayHello() {
|
||||||
|
return "Hello";
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/all")
|
||||||
|
public List<Book> findAll() {
|
||||||
|
return repository.findAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/create")
|
||||||
|
public Book newBook(@RequestBody Book newBook) {
|
||||||
|
return repository.save(newBook);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/get/{id}")
|
||||||
|
public Book findOne(@PathVariable Long id) throws BookstoreException {
|
||||||
|
return repository.findById(id)
|
||||||
|
.orElseThrow(BookstoreException::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,45 @@
|
||||||
|
package com.baeldung.spring.oracle.pooling.entity;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.GenerationType;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Book {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Book() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Book(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Book [id=" + id + ", name=" + name + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,10 @@
|
||||||
|
package com.baeldung.spring.oracle.pooling.exception;
|
||||||
|
|
||||||
|
public class BookstoreException extends Exception{
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
package com.baeldung.spring.oracle.pooling.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
import com.baeldung.spring.oracle.pooling.entity.Book;
|
||||||
|
|
||||||
|
public interface BookRepository extends JpaRepository<Book, Long>{
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
logging.level.root=INFO
|
||||||
|
|
||||||
|
# OracleDB connection settings
|
||||||
|
spring.datasource.url=jdbc:oracle:thin:@//localhost:11521/ORCLPDB1
|
||||||
|
spring.datasource.username=books
|
||||||
|
spring.datasource.password=books
|
||||||
|
spring.datasource.driver-class-name=oracle.jdbc.OracleDriver
|
||||||
|
|
||||||
|
# Comment this line for standard Spring Boot default choosing algorithm
|
||||||
|
#spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource
|
||||||
|
|
||||||
|
# HikariCP settings
|
||||||
|
spring.datasource.hikari.minimumIdle=5
|
||||||
|
spring.datasource.hikari.maximumPoolSize=20
|
||||||
|
spring.datasource.hikari.idleTimeout=30000
|
||||||
|
spring.datasource.hikari.maxLifetime=2000000
|
||||||
|
spring.datasource.hikari.connectionTimeout=30000
|
||||||
|
spring.datasource.hikari.poolName=HikariPoolBooks
|
||||||
|
|
||||||
|
# Tomcat settings
|
||||||
|
spring.datasource.tomcat.maxActive=15
|
||||||
|
spring.datasource.tomcat.minIdle=5
|
||||||
|
|
||||||
|
# JPA settings
|
||||||
|
spring.jpa.database-platform=org.hibernate.dialect.Oracle12cDialect
|
||||||
|
spring.jpa.hibernate.use-new-id-generator-mappings=false
|
||||||
|
spring.jpa.hibernate.ddl-auto=create
|
|
@ -1 +1,9 @@
|
||||||
|
# Available profiles
|
||||||
|
# - none for no profile
|
||||||
|
# - oracle-pooling-basic - Oracle database with HikariCP. Loads configuration from application-oracle-pooling-basic.properties
|
||||||
|
# - oracle - Uses OracleDataSource. This profile also needs "oracle-pooling-basic"
|
||||||
|
# - oracle-ucp - Uses PoolDataSource. This profile also needs "oracle-pooling-basic"
|
||||||
|
# - c3p0 - Uses ComboPooledDataSource. This profile also needs "oracle-pooling-basic"
|
||||||
|
spring:
|
||||||
|
profiles:
|
||||||
|
active: none
|
|
@ -5,10 +5,8 @@ import static org.junit.Assert.assertNotNull;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.jdbi.v3.core.Jdbi;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
@ -24,7 +22,7 @@ import com.baeldung.boot.jdbi.service.CarMakerService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest(classes = {SpringBootJdbiApplication.class, JdbiConfiguration.class})
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class SpringBootJdbiApplicationUnitTest {
|
public class SpringBootJdbiApplicationUnitTest {
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.baeldung.spring.oracle.pooling;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = {SpringOraclePoolingApplication.class})
|
||||||
|
@ActiveProfiles({"oracle-pooling-basic", "c3p0"})
|
||||||
|
public class SpringOraclePoolingApplicationC3P0LiveTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DataSource dataSource;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenC3p0Configuration_thenBuildsComboPooledDataSource() {
|
||||||
|
assertTrue(dataSource instanceof com.mchange.v2.c3p0.ComboPooledDataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.baeldung.spring.oracle.pooling;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = {SpringOraclePoolingApplication.class})
|
||||||
|
@ActiveProfiles("oracle-pooling-basic")
|
||||||
|
public class SpringOraclePoolingApplicationHikariCPLiveTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DataSource dataSource;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenHikariCPConfiguration_thenBuildsHikariCP() {
|
||||||
|
assertTrue(dataSource instanceof com.zaxxer.hikari.HikariDataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.baeldung.spring.oracle.pooling;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = {SpringOraclePoolingApplication.class})
|
||||||
|
@ActiveProfiles({"oracle-pooling-basic", "oracle"})
|
||||||
|
public class SpringOraclePoolingApplicationOracleLiveTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DataSource dataSource;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenOracleConfiguration_thenBuildsOracleDataSource() {
|
||||||
|
assertTrue(dataSource instanceof oracle.jdbc.pool.OracleDataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.baeldung.spring.oracle.pooling;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest(classes = {SpringOraclePoolingApplication.class})
|
||||||
|
@ActiveProfiles({"oracle-pooling-basic", "oracle-ucp"})
|
||||||
|
public class SpringOraclePoolingApplicationOracleUCPLiveTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DataSource dataSource;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenOracleUCPConfiguration_thenBuildsOraclePoolDataSource() {
|
||||||
|
assertTrue(dataSource instanceof oracle.ucp.jdbc.PoolDataSource);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue