Partner – Microsoft – NPI (cat= Spring Boot)
announcement - icon

Azure Spring Apps is a fully managed service from Microsoft (built in collaboration with VMware), focused on building and deploying Spring Boot applications on Azure Cloud without worrying about Kubernetes.

And, the Enterprise plan comes with some interesting features, such as commercial Spring runtime support, a 99.95% SLA and some deep discounts (up to 47%) when you are ready for production.

>> Learn more and deploy your first Spring Boot app to Azure.

You can also ask questions and leave feedback on the Azure Spring Apps GitHub page.

1. Overview

Spring Boot is an opinionated – yet powerful – layer of abstraction placed on top of a plain Spring platform, which makes developing stand-alone and web applications a no-brainer. Spring Boot provides a few handy “starter” dependencies, aimed at running and testing Java applications with a minimal footprint.

One key component of these 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.

In this tutorial, we’ll learn how to configure a Tomcat connection pool in Spring Boot.

2. The Maven Dependencies

Spring Boot uses HikariCP as the default connection pool, due to its remarkable performance and enterprise-ready features.

Here’s how Spring Boot automatically configures a connection pool datasource:

  1. Spring Boot will look for HikariCP on the classpath and use it by default when present
  2. If HikariCP is not found on the classpath, then Spring Boot will pick up the Tomcat JDBC Connection Pool, if it’s available
  3. If neither of these options is available, Spring Boot will choose Apache Commons DBCP2, if that is available

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:


This simple approach allows us to get Spring Boot using a Tomcat connection pool without having to write a @Configuration class and programmatically define a DataSource bean.

It’s also worth noting that in this case, we’re using the H2 in-memory database. Spring Boot will autoconfigure H2 for us, without having to specify a database URL, user, and password.

We just need to include the corresponding dependency in the “pom.xml” file and Spring Boot will do the rest for us.

Alternatively, it’s possible to skip the connection pool scanning algorithm that Spring Boot uses and explicitly specify a connection pooling datasource in the “” file, using the “spring.datasource.type” property:

// other spring datasource properties

3. Tweaking the Connection Pool With the “” File

Once we’ve successfully configured a Tomcat connection pool in Spring Boot, it’s very likely that we’ll want to set up some additional properties, for optimizing its performance and suiting some specific requirements.

We can do so in the “” file:


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.

We can also specify some Hibernate-specific properties:

# Hibernate specific properties

4. Testing the Connection Pool

Let’s write a simple integration test to check that Spring Boot has correctly configured the connection pool:

public class SpringBootTomcatConnectionPoolIntegrationTest {
    private DataSource dataSource;
    public void givenTomcatConnectionPoolInstance_whenCheckedPoolClassName_thenCorrect() {

5. A Sample Command Line Application

With all the connection pooling plumbing already set, let’s build a simple command line application.

In doing so, we can see how to perform some CRUD operations on an H2 database using the powerful DAO layer that Spring Data JPA (and transitively, Spring Boot) provides out of the box.

For a detailed guide on how to get started using Spring Data JPA, please check this article.

5.1. The Customer Entity Class

Let’s first define a naive Customer entity class:

@Table(name = "customers")
public class Customer {

    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    @Column(name = "first_name")
    private String firstName;
    // standard constructors / getters / setters / toString

5.2. The CustomerRepository Interface

In this case, we just want to perform CRUD operations on a few Customer entities. Additionally, we need to fetch all the customers that match a given last name.

So, all that we have to do is to extend Spring Data JPA’s CrudRepository interface and define a tailored method:

public interface CustomerRepository extends CrudRepository<Customer, Long> {
    List<Customer> findByLastName(String lastName);

Now we can easily fetch a Customer entity by its last name.

5.3. The CommandLineRunner Implementation

Finally, we need at least to persist a few Customer entities in the database and verify that our Tomcat connection pool is actually working.

Let’s create an implementation of Spring Boot’s CommandLineRunner interface. Spring Boot will bootstrap the implementation before launching the application:

public class CommandLineCrudRunner implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(CommandLineCrudRunner.class);
    private final CustomerRepository repository;
    public void run(String... args) throws Exception { Customer("John", "Doe")); Customer("Jennifer", "Wilson"));
       "Customers found with findAll():");
        repository.findAll().forEach(c ->;
       "Customer found with findById(1L):");
        Customer customer = repository.findById(1L)
          .orElseGet(() -> new Customer("Non-existing customer", ""));;
       "Customer found with findByLastName('Wilson'):");
        repository.findByLastName("Wilson").forEach(c -> {

In a nutshell, the CommandLineCrudRunner class first saves a couple of Customer entities in the database. Next, it fetches the first one using the findById() method. Finally, it retrieves a customer with the findByLastName() method.

5.4. Running the Spring Boot Application

Of course, the last thing that we need to do is just run the sample application. Then we can see the Spring Boot/Tomcat connection pool tandem in action:

public class SpringBootConsoleApplication {
    public static void main(String[] args) {;

6. Conclusion

In this tutorial, we learned how to configure and use a Tomcat connection pool in Spring Boot. In addition, we developed a basic command line application to show how easy is to work with Spring Boot, a Tomcat connection pool, and the H2 database.

As usual, all the code samples shown in this tutorial are available over on GitHub.

Course – LSD (cat=Persistence)

Get started with Spring Data JPA through the reference Learn Spring Data JPA course:

res – Persistence (eBook) (cat=Persistence)
Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.