BAEL-2102 - Configure a Tomcat Connection Pool in Spring Boot (#5095)
* Initial Commit * Initial Commit * Initial Commit * Update CommandLineCrudRunner.java * Update pom.xml * Update Customer.java * Update application.properties * Update pom.xml * Update SpringBootConsoleApplication.java * Update SpringBootConsoleApplication.java * Update SpringBootConsoleApplication.java * Update CommandLineCrudRunner.java * Update pom.xml
This commit is contained in:
parent
3824590375
commit
d7edc9a7c2
@ -1,57 +1,75 @@
|
|||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
<modelVersion>4.0.0</modelVersion>
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>spring-boot-persistence</artifactId>
|
||||||
|
<version>0.1.0</version>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<artifactId>parent-boot-2</artifactId>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>spring-boot-persistence</artifactId>
|
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<relativePath>../parent-boot-2</relativePath>
|
||||||
<name>spring-boot-persistence</name>
|
</parent>
|
||||||
<description>This is a simple Spring Data Repositories test</description>
|
|
||||||
|
<dependencies>
|
||||||
<parent>
|
<dependency>
|
||||||
<artifactId>parent-boot-2</artifactId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<groupId>com.baeldung</groupId>
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<exclusions>
|
||||||
<relativePath>../parent-boot-2</relativePath>
|
<exclusion>
|
||||||
</parent>
|
<groupId>com.zaxxer</groupId>
|
||||||
|
<artifactId>HikariCP</artifactId>
|
||||||
<dependencies>
|
</exclusion>
|
||||||
<dependency>
|
</exclusions>
|
||||||
<groupId>org.springframework.boot</groupId>
|
</dependency>
|
||||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
<dependency>
|
||||||
</dependency>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<dependency>
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
<groupId>com.h2database</groupId>
|
<scope>test</scope>
|
||||||
<artifactId>h2</artifactId>
|
</dependency>
|
||||||
</dependency>
|
<dependency>
|
||||||
<dependency>
|
<groupId>org.apache.tomcat</groupId>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<artifactId>tomcat-jdbc</artifactId>
|
||||||
<artifactId>spring-boot-starter</artifactId>
|
<version>${tomcat-jdbc.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
<build>
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
<finalName>spring-boot-persistence</finalName>
|
<version>${mysql-connector-java.version}</version>
|
||||||
<resources>
|
</dependency>
|
||||||
<resource>
|
<dependency>
|
||||||
<directory>src/main/resources</directory>
|
<groupId>com.h2database</groupId>
|
||||||
<filtering>true</filtering>
|
<artifactId>h2</artifactId>
|
||||||
</resource>
|
<version>${h2database.version}</version>
|
||||||
</resources>
|
<scope>runtime</scope>
|
||||||
<plugins>
|
</dependency>
|
||||||
<plugin>
|
</dependencies>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
|
||||||
<artifactId>maven-war-plugin</artifactId>
|
<properties>
|
||||||
</plugin>
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
<plugin>
|
<java.version>1.8</java.version>
|
||||||
<groupId>pl.project13.maven</groupId>
|
<mysql-connector-java.version>8.0.12</mysql-connector-java.version>
|
||||||
<artifactId>git-commit-id-plugin</artifactId>
|
<tomcat-jdbc.version>9.0.10</tomcat-jdbc.version>
|
||||||
<version>${git-commit-id-plugin.version}</version>
|
<h2database.version>1.4.197</h2database.version>
|
||||||
</plugin>
|
</properties>
|
||||||
</plugins>
|
|
||||||
</build>
|
<build>
|
||||||
|
<finalName>spring-boot-persistence</finalName>
|
||||||
<properties>
|
<resources>
|
||||||
<git-commit-id-plugin.version>2.2.4</git-commit-id-plugin.version>
|
<resource>
|
||||||
</properties>
|
<directory>src/main/resources</directory>
|
||||||
</project>
|
<filtering>true</filtering>
|
||||||
|
</resource>
|
||||||
|
</resources>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
||||||
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.tomcatconnectionpool.application;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||||
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
@ComponentScan(basePackages={"com.baeldung.tomcatconnectionpool.application"})
|
||||||
|
@EnableJpaRepositories(basePackages="com.baeldung.tomcatconnectionpool.application.repositories")
|
||||||
|
@EnableTransactionManagement
|
||||||
|
@EntityScan(basePackages="com.baeldung.tomcatconnectionpool.application.entities")
|
||||||
|
public class SpringBootConsoleApplication {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(SpringBootConsoleApplication.class);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
package com.baeldung.tomcatconnectionpool.application.entities;
|
||||||
|
|
||||||
|
import javax.persistence.Column;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.GenerationType;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.Table;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "customers")
|
||||||
|
public class Customer {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||||
|
private long id;
|
||||||
|
@Column(name = "first_name")
|
||||||
|
private String firstName;
|
||||||
|
@Column(name = "last_name")
|
||||||
|
private String lastName;
|
||||||
|
|
||||||
|
public Customer() {}
|
||||||
|
|
||||||
|
public Customer(String firstName, String lastName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLastName(String lastName) {
|
||||||
|
this.lastName = lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getFirstName() {
|
||||||
|
return firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setFirstName(String firstName) {
|
||||||
|
this.firstName = firstName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Customer{" + "id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + '}';
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.tomcatconnectionpool.application.repositories;
|
||||||
|
|
||||||
|
import com.baeldung.tomcatconnectionpool.application.entities.Customer;
|
||||||
|
import java.util.List;
|
||||||
|
import org.springframework.data.repository.CrudRepository;
|
||||||
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
@Repository
|
||||||
|
public interface CustomerRepository extends CrudRepository<Customer, Long> {
|
||||||
|
|
||||||
|
List<Customer> findByLastName(String lastName);
|
||||||
|
}
|
@ -0,0 +1,37 @@
|
|||||||
|
package com.baeldung.tomcatconnectionpool.application.runners;
|
||||||
|
|
||||||
|
import com.baeldung.tomcatconnectionpool.application.entities.Customer;
|
||||||
|
import com.baeldung.tomcatconnectionpool.application.repositories.CustomerRepository;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.CommandLineRunner;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class CommandLineCrudRunner implements CommandLineRunner {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(CommandLineCrudRunner.class);
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private CustomerRepository customerRepository;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run(String... args) throws Exception {
|
||||||
|
customerRepository.save(new Customer("John", "Doe"));
|
||||||
|
customerRepository.save(new Customer("Jennifer", "Wilson"));
|
||||||
|
|
||||||
|
logger.info("Customers found with findAll():");
|
||||||
|
customerRepository.findAll().forEach(c -> logger.info(c.toString()));
|
||||||
|
|
||||||
|
logger.info("Customer found with findById(1L):");
|
||||||
|
Customer customer = customerRepository.findById(1L)
|
||||||
|
.orElseGet(() -> new Customer("Non-existing customer", ""));
|
||||||
|
logger.info(customer.toString());
|
||||||
|
|
||||||
|
logger.info("Customer found with findByLastName('Wilson'):");
|
||||||
|
customerRepository.findByLastName("Wilson").forEach(c -> {
|
||||||
|
logger.info(c.toString());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -1,2 +1,16 @@
|
|||||||
spring.jpa.show-sql=true
|
spring.datasource.tomcat.initial-size=15
|
||||||
spring.jpa.hibernate.ddl-auto=none
|
spring.datasource.tomcat.max-wait=20000
|
||||||
|
spring.datasource.tomcat.max-active=50
|
||||||
|
spring.datasource.tomcat.max-idle=15
|
||||||
|
spring.datasource.tomcat.min-idle=8
|
||||||
|
spring.datasource.tomcat.default-auto-commit=true
|
||||||
|
spring.datasource.url=jdbc:h2:mem:test
|
||||||
|
spring.datasource.username=root
|
||||||
|
spring.datasource.password=
|
||||||
|
spring.datasource.driver-class-name=org.h2.Driver
|
||||||
|
|
||||||
|
spring.jpa.show-sql=false
|
||||||
|
spring.jpa.hibernate.ddl-auto=update
|
||||||
|
spring.jpa.hibernate.naming-strategy=org.hibernate.cfg.ImprovedNamingStrategy
|
||||||
|
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||||
|
spring.jpa.properties.hibernate.id.new_generator_mappings=false
|
||||||
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.tomcatconnectionpool.test.application;
|
||||||
|
|
||||||
|
import javax.sql.DataSource;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
import static org.assertj.core.api.Assertions.*;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
|
||||||
|
@RunWith(SpringRunner.class)
|
||||||
|
@SpringBootTest
|
||||||
|
public class SpringBootTomcatConnectionPoolIntegrationTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DataSource dataSource;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenTomcatConnectionPoolInstance_whenCheckedPoolClassName_thenCorrect() {
|
||||||
|
assertThat(dataSource.getClass().getName()).isEqualTo("org.apache.tomcat.jdbc.pool.DataSource");
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user