Skip to main content

Configuring Tomcat Connection pool and Hikari Connection pool in Spring Boot

One key component of spring boot starter dependencies is spring-boot-starter-data-JPA. This allows us to use JPA and work with production databases by using some popular JDBC connection pooling implementations, such as HikariCP and Tomcat JDBC Connection Pool.
Tomcat Connection Pooling
Spring Boot will look for HikariCP on the classpath and use it by default when present
To configure a Tomcat JDBC connection pool instead of the default HikariCP, we'll exclude HikariCP from the spring-boot-starter-data-jpa dependency and add the tomcat-jdbc Maven dependency to our pom.xml:
    <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
   <exclusions>
    <exclusion>
     <groupId>com.zaxxer</groupId>
     <artifactId>HikariCP</artifactId>
    </exclusion>
   </exclusions>
  </dependency>
  <dependency>
   <groupId>org.apache.tomcat</groupId>
   <artifactId>tomcat-jdbc</artifactId>
  </dependency>
For optimizing its performance we can add additional properties 
#connection pooling 
spring.datasource.tomcat.initial-size=10
spring.datasource.tomcat.max-wait=15000
spring.datasource.tomcat.max-active=40
spring.datasource.tomcat.max-idle=15
spring.datasource.tomcat.min-idle=8
spring.datasource.tomcat.default-auto-commit=true 
Please notice that we've configured a few additional connection pooling properties, such as the pool's initial size, and the maximum and minimum number of idle connections.
How to know which connection pool is used?
@EnableJpaRepositories(basePackages = "com.knowledgefactory.Repository")
@SpringBootApplication
@ComponentScan({ "com" })
@EntityScan("com.knowledgefactory.Entity")
public class KnowledgefactorydemoApplication implements CommandLineRunner {
  @Autowired
 private UserRepository repository;
 @Autowired
 private DataSource dataSource;
  public static void main(String[] args) {
  SpringApplication.run(KnowledgefactorydemoApplication.class, args);
 }
  @Override
 public void run(String... args) throws Exception {
  System.out.println("DATASOURCE = " + dataSource);
  repository.save(new User("Knf"));
  repository.save(new User("sibin"));
 }
Start  Spring boot application 
In console System will print,
         "DATASOURCE = class org.apache.tomcat.jdbc.pool.DataSource"
Hikari Connection Pooling
Spring Boot will look for HikariCP on the classpath and use it by default when present
    <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-jpa</artifactId>
   </dependency>
One of Hikari's advantages over other DataSource implementations is the fact that it offers a lot of configuration parameters.
We can specify the values for these parameters by using the prefix spring.datasource.hikari and appending the name of the Hikari parameter:
For optimizing its performance we can add additional properties 
spring.datasource.hikari.connection-timeout=60000
spring.datasource.hikari.maxLifetime=1700000
spring.datasource.hikari.maximum-pool-size=5
How to know which connection pool is used?
@EnableJpaRepositories(basePackages = "com.knowledgefactory.Repository")
@SpringBootApplication
@ComponentScan({ "com" })
@EntityScan("com.knowledgefactory.Entity")
public class KnowledgefactorydemoApplication implements CommandLineRunner {
 @Autowired
 private UserRepository repository;
 @Autowired
 private DataSource dataSource;
 public static void main(String[] args) {
  SpringApplication.run(KnowledgefactorydemoApplication.class, args);
 }
 @Override
 public void run(String... args) throws Exception {
  System.out.println("DATASOURCE = " + dataSource);
  repository.save(new User("Knf"));
  repository.save(new User("sibin"));
  }
Start  Spring boot application 
In console System will print,
"DATASOURCE = class com.zaxxer.hikari.HikariDataSource"

Comments

Popular posts from this blog

Spring boot video streaming example-HTML5

This article shows you how to stream MP4 video in Spring Boot web application 
User Interface

Project Structure

Home Controller
@Controller
public class HomeController {
 @Autowired
 private MyResourceHttpRequestHandler handler;
 private final static File MP4_FILE = new File("D:\\videofiles\\video1.mp4");
 // supports byte-range requests
 @GetMapping("/index")
 public String home() {
  return "index";
 }
 // supports byte-range requests
 @GetMapping("/byterange")
 public void byterange( HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  request.setAttribute(MyResourceHttpRequestHandler.ATTR_FILE, MP4_FILE);
  handler.handleRequest(request, response);
 }}
Spring Boot 
@SpringBootApplication
@ComponentScan({ "com" })
public class KnowledgefactorydemoApplication {
 public static void main(String[] args) {
  SpringApplication.run(KnowledgefactorydemoApplication.class, args);
 }}
MyResource HttpRequestHandler
@Componen…

DataTable-Pagination example with Spring boot,jQuery and ajax free download

This article shows you DataTable-Pagination example with Spring boot, jQuery and ajax. 

Following technologies stack being used:
Spring Boot 2.1.1.RELEASESpring 5.1.3.RELEASE Maven 3DatatablejQueryFree Markerjavascriptajax Project Structure
View(index.ftl) <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>Knf|Pagination-Datatables</title> <!-- Tell the browser to be responsive to screen width --> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="../../css/jquery.dataTables.min.css"> <body>  <section class="content">   <div class="container-fluid">    <table id="example" class="display" style="width: 100%">     <thead>      <tr>       <th>First name</th>       <th>La…

9 Best Job Posting Sites for Employee 2020-2022

One of the most common ways today’s job seekers uncover employment opportunities are by using online sources. There are hundreds of job boards, both generic and niche, as well as aggregators, social media channels, networking groups, and staffing company websites to choose from. The good news is they provide plenty of listings. The bad news is you can be easily overwhelmed by so many options. Here, knowledge factory offers 9 of what is generally considered the best job search websites around. There are many others, of course, but this list of best job search websites should give you a good starting point for your next job hunt: 1. LinkedIn
This top networking site allows you to find jobs through your extended network. User profiles serve as resumes. You can easily share career-related content and solicit or supply recommendations. Additionally, you can join groups, participate in conversations, and follow companies you find interesting and relevant to your job search. Premium pai…