connection pool hikaricp jpa Latest spring boot spring-data

Spring Start + Spring Information JPA – Mkyong.com

Spring Start Spring Information

On this tutorial, we’ll show you ways Spring Boot + Spring Knowledge JPA is used to retailer knowledge in a H2 reminiscence database and knowledge query

Technologies used:

  • Spring Start 2.1.2.RELEASE
  • Spring 5.1.4.RELEASE
  • Hibernation 5.three.7
  • HikariCP three.2.zero
  • H2 Memory Database 1.4.197
  • Maven three
  • Java eight

1. Challenge Directory

  Project Directory

2. Maven

Sets spring-boot-starter knowledge jpa, it receives spring knowledge, hibernation, HikariCP, and all database dependencies.

pom.xml

four.0.zero

spring-knowledge-jpa
jar
Spring start spring info JPA
1,zero

org.springframework.boot
spring-starting starter-mother or father
2.1.2.RELEASE

1.eight
true
true

org.springframework.boot
spring-play-begin-data-JPA

com .h2database
h2

org.springframework.boot
spring-boot-booter check
check

org.springframework.boot
spring-boot-maven plugin

org .apache.maven.plugins
Maven Mountain Protected Plugin
2.22.zero [19659039] Present Undertaking Dependencies

$ mvn dependence: tree

[INFO] org.springframework.boot: spring-boot-knowledge-jpa: jar: 1.zero
[INFO] + – org.springframework.boot: spring-boot-starter-knowledge-jpa: jar: 2.1.2.RELEASE: compile
[INFO] | + – org.springframework.boot: spring-boot-starter-aop: jar: 2.1.2.RELEASE: compile
[INFO] | | + – org.springframework: spring-aop: jar: 5.1.4.RELEASE: compile
[INFO] | | – org.aspectj: aspectjweaver: jar: 1.9.2: compile
[INFO] | + – org.springframework.boot: spring-boot-starter-jdbc: jar: 2.1.2.RELEASE: Translate
[INFO] | | + – com.zaxxer: HikariCP: jar: three.2.zero: translate
[INFO] | | – org.springframework: spring-jdbc: jar: 5.1.4.RELEASE: compile
[INFO] | + – javax.transaction: javax.transaction-api: jar: 1.3: translate
[INFO] | + – javax.xml.bind: jaxb-api: jar: 2.three.1: compile
[INFO] | | – javax.activation: javax.activation-api: jar: 1.2.0: translate
[INFO] | + – org.hibernate: Hibernate-core: jar: 5.three.7 Conclusion: assemble
[INFO] | | + – org.jboss.logging: jboss-logging: jar: 3.3.2 Conclusion: assemble
[INFO] | | + – javax.persistence: javax.persistence-api: jar: 2.2: translate
[INFO] | | + – org.javassist: javassist: jar: 3.23.1-GA: put together
[INFO] | | + – internet.bytebuddy: byte-buddy: jar: 1.9.7: compile
[INFO] | | + – antlr: antlr: jar: 2.7.7: compile
[INFO] | | + – org.jboss: jandex: jar: 2.0.5. Conclusion: put together
[INFO] | | + – com.fasterxml: Classmate: jar: 1.4.zero: Translate
[INFO] | | + – org.dom4j: dom4j: jar: 2.1.1: compile
[INFO] | | – org.hibernate.widespread: Hibernate-Commons-annotations: jar: 5.zero.four.
[INFO] | + – org.springframework.knowledge:spring-knowledge-jpa:jar:2.1.4.RELEASE:compile
[INFO] | | + – org.springframework.knowledge:spring-knowledge-commons:jar:2.1.four.RELEASE:compile
[INFO] | | + – org.springframework: spring-orm: jar: 5.1.four.RELEASE: compile
[INFO] | | + – org.springframework: spring-context: jar: 5.1.4.RELEASE: compile
[INFO] | | | – org.springframework: spring-expression: jar: 5.1.four.RELEASE: compile
[INFO] | | + – org.springframework: spring-tx: jar: 5.1.four.RELEASE: put together
[INFO] | | + – org.springframework: spring-beans: jar: 5.1.4.RELEASE: put collectively
[INFO] | | – org.slf4j: slf4j-api: jar: 1.7.25: compile
[INFO] | – org.springframework: spring elements: jar: 5.1.4.RELEASE: put together
[INFO] + – com.h2database: h2: jar: 1.4.197: put together
[INFO] org.springframework.boot: spring-boot-starter check: jar: 2.1.2.RELEASE: check
[INFO] + – org.springframework.boot: spring-boot-starter: jar: 2.1.2.RELEASE: assemble
[INFO] | + – org.springframework.boot: spring-boot: jar: 2.1.2.RELEASE: compile
[INFO] | + – org.springframework.boot: spring-boot-autoconfigure: jar: 2.1.2.RELEASE: compile
[INFO] | + – org.springframework.boot: spring-boot-starter-logging: jar: 2.1.2.RELEASE: compile
[INFO] | | + – ch.qos.logback: logback-basic: jar: 1.2.3: translate
[INFO] | | | – ch.qos.logback: logback-core: jar: 1.2.3: compile
[INFO] | | + – org.apache.logging.log4j: log4j-to-slf4j: jar: 2.11.1: compile
[INFO] | | | – org.apache.logging.log4j: log4j-api: jar: 2.11.1: compile
[INFO] | | – org.slf4j: jul-to-slf4j: jar: 1.7.25: translate
[INFO] | + – javax.annotation: javax.annotation-api: jar: 1.3.2: compile
[INFO] | – org.yaml: snakeyaml: jar: 1.23: runtime
[INFO] + – org.springframework.boot: spring-boot-check: jar: 2.1.2.RELEASE: check
[INFO] + – org.springframework.boot: spring-boot-check-autoconfigure: jar: 2.1.2.RELEASE: check
[INFO] + – com.jayway.jsonpath: json-path: jar: 2.4.zero: check
[INFO] | – internet.minidev: json-sensible: jar: 2.three: check
[INFO] | – internet.minidev: accessors-sensible: jar: 1.2: check
[INFO] | – org.ow2.asm: asm: jar: 5.0.4: check
[INFO] + – trains: trains: jar: 4.12: check
[INFO] + – org.assertj: assertj-core: jar: 3.11.1: check
[INFO] + – org.mockito: mockito-core: jar: 2.23.four: check
[INFO] | + – internet.bytebuddy: byte-man-agent: jar: 1.9.7: check
[INFO] | – org.objenesis: objenesis: jar: 2.6: check
[INFO] + – org.hamcrest: hamcrest-core: jar: 1.three: check
[INFO] + – org.hamcrest: hamcrest-library: jar: 1.3: check
[INFO] + – org.skyscreamer: jsonassert: jar: 1.5.zero: check
[INFO] | com.vaadin.exterior.google:android-json:jar:zero.0.20131108.vaadin1:check
[INFO] + – org.springframework: spring-core: jar: 5.1.4.RELEASE: compile
[INFO] | – org.springframework: spring-jcl: jar: 5.1.four.RELEASE: put collectively
[INFO] + – org.springframework: spring check: jar: 5.1.four.RELEASE: check
[INFO] – org.xmlunit: xmlunit-core: jar: 2.6.2: check

3. Spring Information JPA

Ebook.java

package deal com.mkyong;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class ebook

@Id
@GeneratedValue (strategy = GenerationType.AUTO)
personal Lengthy ID;
personal language identify;

public guide ()

public ebook (string identify)
this.identify = identify;

// getters, Setters, toString …

BookRepository.java

package deal com.mkyong;

import org.springframework.knowledge.repository.CrudRepository;

import java.util.Listing;

public interface BookRepository extends CrudRepository

Record findByName (string identify);

four. @SpringBootApplication

4.1 Start the Spring Boot, add 3 books to the H2 database and check the search () technique.

StartApplication.java

package deal com.mkyong;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.manufacturing unit.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StartApplication carried out by CommandLineRunner

personal static remaining Logger log = LoggerFactory.getLogger (StartApplication.class);

@Autowired
a personal BookRepository archive;

public Static void fundamental (String [] args)
SpringApplication.run (StartApplication.class, args);

@Bypass
public void run (String … args)

log.information ("StartApplication …");

repository.save (new guide ("Java"));
repository.save (new e-book ("node"));
repository.save (new guide ("Python"));

System.out.println ("nfindAll ()");
repository.findAll () .EE (x -> System.out.println (x));

System.out.println ("nfindById (1L)");
repository.findById (1l) .ifPresent (x -> System.out.println (x));

System.out.println ("nfindByName (Node & # 39;)");
repository.findByName ("Node") prematurely (x -> System.out.println (x));

5. HikariCP Connection Pool

By default, Spring Boot 2 makes use of HikariCP as the default interface.

5.1 Allow com.zaxxer for login debug, it prints HikariCP default settings.

software.properties

logging.degree.org.springframework = INFO
logging.degree.com.mkyong = INFO
logging.degree.com.zaxxer = DEBUG
logging.degree.root = ERROR

5.2 Run the Spring Boot program, examine the submit:

DEBUG com.zaxxer.hikari.HikariConfig – Driver class org.h2.Driver may be found in the Thread context class downloader jdk.inner.loader.ClassLoaders$AppClassLoader@28c97a5
DEBUG com.zaxxer.hikari.HikariConfig – HikariPool-1 configuration:
DEBUG com.zaxxer.hikari.HikariConfig – allowPoolSuspension …………. false
DEBUG com.zaxxer.hikari.HikariConfig – autoCommit …………………. true
DEBUG com.zaxxer.hikari.HikariConfig – record ……………………. no
DEBUG com.zaxxer.hikari.HikariConfig – connectionInitSql …………… nothing
DEBUG com.zaxxer.hikari.HikariConfig – connectionTestQuery …………. no
DEBUG com.zaxxer.hikari.HikariConfig – connectionTimeout …………… 30000
DEBUG com.zaxxer.hikari.HikariConfig – dataSource …………………. no
DEBUG com.zaxxer.hikari.HikariConfig – dataSourceClassName …………. no
DEBUG com.zaxxer.hikari.HikariConfig – dataSourceJNDI ……………… nothing
DEBUG com.zaxxer.hikari.HikariConfig – dataSourceProperties ………… password =
DEBUG com.zaxxer.hikari.HikariConfig – driverClassName …………….. "org.h2.Driver"
DEBUG com.zaxxer.hikari.HikariConfig – healthCheckProperties ………..
DEBUG com.zaxxer.hikari.HikariConfig – healthCheckRegistry …………. no
DEBUG com.zaxxer.hikari.HikariConfig – idleTimeout ………………… 600000
DEBUG com.zaxxer.hikari.HikariConfig – initializationFailTimeout ……. 1
DEBUG com.zaxxer.hikari.HikariConfig – isolateInternalQueries ………. false
DEBUG com.zaxxer.hikari.HikariConfig – jdbcUrl ……………………. jdbc: h2: mem: testdb, DB_CLOSE_DELAY = -1, DB_CLOSE_ON_EXIT = WRONG
DEBUG com.zaxxer.hikari.HikariConfig – leakDetectionThreshold ………. zero
DEBUG com.zaxxer.hikari.HikariConfig – maxLifetime ………………… 1800000
DEBUG com.zaxxer.hikari.HikariConfig – maximumPoolSize …………….. 10
DEBUG com.zaxxer.hikari.HikariConfig – metricRegistry ……………… nothing
DEBUG com.zaxxer.hikari.HikariConfig – metricsTrackerFactory ……….. nothing
DEBUG com.zaxxer.hikari.HikariConfig – minimumSend ……………
DEBUG com.zaxxer.hikari.HikariConfig Password …………………..
DEBUG com.zaxxer.hikari.HikariConfig – poolName …………………… "HikariPool-1"
DEBUG com.zaxxer.hikari.HikariConfig – readOnly …………………… false
DEBUG com.zaxxer.hikari.HikariConfig – registerMbeans ……………… false
DEBUG com.zaxxer.hikari.HikariConfig – ScheduleExecutor …………… nothing
DEBUG com.zaxxer.hikari.HikariConfig – chart …………………….. no
DEBUG com.zaxxer.hikari.HikariConfig – threadFactory ………………. inner
DEBUG com.zaxxer.hikari.HikariConfig Transaction Transmission ………… default
DEBUG com.zaxxer.hikari.HikariConfig – username …………………… "sa"
DEBUG com.zaxxer.hikari.HikariConfig – validationTimeout …………… 5000
INFO com.zaxxer.hikari.HikariDataSource – HikariPool-1 – Getting Began …
DEBUG com.zaxxer.hikari.pool.HikariPool – HikariPool-1 – Added connection conn0: url = jdbc: h2: mem: testdb consumer = SA
INFO com.zaxxer.hikari.HikariDataSource – HikariPool-1 – Get began.
DEBUG com.zaxxer.hikari.pool.
DEBUG com.zaxxer.hikari.pool.HikariPool – HikariPool-1 – Added connection conn1: url = jdbc: h2: mem: testdb consumer = SA
DEBUG com.zaxxer.hikari.pool.HikariPool – HikariPool-1 – Added connection conn2: url = jdbc: h2: mem: testdb consumer = SA
DEBUG com.zaxxer.hikari.pool.HikariPool – HikariPool-1 – Added connection conn3: url = jdbc: h2: mem: testdb consumer = SA
DEBUG com.zaxxer.hikari.pool.HikariPool – HikariPool-1 – Added connection conn4: url = jdbc: h2: mem: testdb consumer = SA
DEBUG com.zaxxer.hikari.pool.HikariPool – HikariPool-1 – Added connection5: url = jdbc: h2: mem: testdb consumer = SA
DEBUG com.zaxxer.hikari.pool.HikariPool – HikariPool-1 – Added connection conn6: url = jdbc: h2: mem: testdb consumer = SA
DEBUG com.zaxxer.hikari.pool.HikariPool – HikariPool-1 – Added interface7: url = jdbc: h2: mem: testdb consumer = SA
DEBUG com.zaxxer.hikari.pool.HikariPool – HikariPool-1 – Added connection conn8: url = jdbc: h2: mem: testdb consumer = SA
DEBUG com.zaxxer.hikari.pool.HikariPool – HikariPool-1 – Added connection conn9: url = jdbc: h2: mem: testdb consumer = SA
DEBUG com.zaxxer.hikari.pool.HikariPool – HikariPool-1 – After adding statistics (complete = 10, lively = 0, idle = 10, waiting = zero)

5.2 We will use spring.datasource.hikari. *: Ta to bypass the default settings.

software.properties

logging.degree.org.springframework = INFO
logging.degree.com.mkyong = INFO
logging.degree.com.zaxxer = DEBUG
logging.degree.root = ERROR

spring.datasource.hikari.connectionTimeout = 20000
spring.datasource.hikari.maximumPoolSize = 5
spring.datasource.hikari.poolName = HikariPoolZZZ

6. Unit Check

6.1 @DataJpaTest and TestEntityManager to check the spring knowledge JPA archive.

BookRepositoryTest.java

package deal com.mkyong;

import org.junit.Check;
import org.junit.runner.RunWith;
import org.springframework.beans.manufacturing unit.annotation.Autowired;
import org.springframework.boot.check.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.check.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.check.context.junit4.SpringRunner;

import java.util.Listing;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;

@RunWith (SpringRunner.class)
@DataJpaTest
Public Class BookRepositoryTest

@Autowired
personal TestEntityManager entityManager;

@Autowired
a personal BookRepository archive;

@Check
public void testFindByName ()

unitManager.persist (new e-book ("C ++"));

Listing books = repository.findByName ("C ++");
assertEquals (1, books.measurement ());

assertThat (books) .extracting (Ebook :: getName) .containsOnly ("C ++");

7. Demo

$ mvn spring-boot: run

INFO com.mkyong.StartApplication – Begins the StartApplication software in 2.79 seconds (JVM operating 10.883)
INFO com.mkyong.StartApplication – StartApplication …

findAll ()
DEBUG com.zaxxer.hikari.pool.PoolBase – HikariPoolZZZ – Restore (readOnly) connection0: url = jdbc: h2: mem: testdb consumer = SA
Ebook id = 1, identify = & # 39; Java & # 39;
E-book id = 2, identify = & # 39; Node & # 39;
Guide id = 3, identify = & # 39; Python & # 39;

findById (1 I)
DEBUG com.zaxxer.hikari.pool.PoolBase – HikariPoolZZZ – Restore (readOnly) connection0: url = jdbc: h2: mem: testdb consumer = SA
Guide id = 1, identify = & # 39; Java & # 39;

findByName ("Node")
Ebook id = 2, identify = & # 39; Node & # 39;

Obtain Supply

References