Merge pull request #9037 from aitorcuesta/BAEL-3075
BAEL-3075 - Oracle Connection Pooling With Spring
This commit is contained in:
commit
97ba63d8b4
|
@ -43,7 +43,12 @@
|
|||
<dependencies>
|
||||
<dependency>
|
||||
<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>
|
||||
|
@ -82,6 +87,23 @@
|
|||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</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>
|
||||
|
||||
<build>
|
||||
|
@ -90,12 +112,30 @@
|
|||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<!-- Please note that ojdbc8.jar, ucp.jar and ons.jar dependencies
|
||||
are needed for OracleConfiguration.java, OracleUCPConfiguration.java, SpringOraclePoolingApplicationOracleLiveTest.java
|
||||
and SpringOraclePoolingApplicationOracleUCPLiveTest.java -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>com/baeldung/spring/oracle/pooling/configuration/OracleConfiguration.java</exclude>
|
||||
<exclude>com/baeldung/spring/oracle/pooling/configuration/OracleUCPConfiguration.java</exclude>
|
||||
</excludes>
|
||||
<testExcludes>
|
||||
<testExclude>com/baeldung/spring/oracle/pooling/SpringOraclePoolingApplicationOracleLiveTest.java</testExclude>
|
||||
<testExclude>com/baeldung/spring/oracle/pooling/SpringOraclePoolingApplicationOracleUCPLiveTest.java</testExclude>
|
||||
</testExcludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<jdbi.version>3.9.1</jdbi.version>
|
||||
<spring.boot.dependencies>2.1.8.RELEASE</spring.boot.dependencies>
|
||||
<c3p0.version>0.9.5.2</c3p0.version>
|
||||
</properties>
|
||||
|
||||
</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,29 @@
|
|||
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);
|
||||
// Only with clients prior to v11.2
|
||||
// dataSource.setConnectionCachingEnabled(true);
|
||||
return dataSource;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
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.setMinPoolSize(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 java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.jdbi.v3.core.Jdbi;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -24,7 +22,7 @@ import com.baeldung.boot.jdbi.service.CarMakerService;
|
|||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@SpringBootTest(classes = {SpringBootJdbiApplication.class, JdbiConfiguration.class})
|
||||
@Slf4j
|
||||
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