WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Spring Framework · Guru · question 85 of 100

How can you implement advanced performance optimizations in Spring JDBC, such as batching, fetch size, and pagination?

📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

Spring provides multiple ways to implement advanced performance optimizations in Spring JDBC. Let’s discuss each of them below -

1. Batching - Batching is the process of sending multiple SQL statements as a single request to the database server. This reduces network round trips and improves performance. Spring JDBC provides support for batch updates using JdbcTemplate class. You can batch updates as below -

jdbcTemplate.batchUpdate("INSERT INTO Employee(name, age) VALUES (?, ?)", new BatchPreparedStatementSetter() {
 
    @Override
    public void setValues(PreparedStatement ps, int i) throws SQLException {
        ps.setString(1, employees.get(i).getName());
        ps.setInt(2, employees.get(i).getAge());
    }
 
    @Override
    public int getBatchSize() {
        return employees.size();
    }
});

2. Fetch Size - Fetch Size is the number of rows fetched from the database at a time. By default, JDBC fetches all the rows at once, but this can cause performance issues if the table has a large number of rows. Spring JDBC provides a way to set the fetch size using the StatementCreatorUtils class as below -

jdbcTemplate.query("SELECT * FROM Employee", new RowMapper<Employee>() {
    @Override
    public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {
        // ...
    }
}, stmt -> {
    stmt.setFetchSize(50);
});

3. Pagination - Pagination is the process of retrieving a subset of results from a large dataset. Spring JDBC provides pagination support using the JdbcTemplate class. You can use ’LIMIT’ or ’OFFSET’ clauses to retrieve a specific subset of results. The ’LIMIT’ and ’OFFSET’ clauses can be added to your SQL query as below -

int pageSize = 10;
int pageNumber = 2;
int offset = (pageNumber - 1) * pageSize;

String sql = "SELECT * FROM Employee LIMIT ? OFFSET ?";
List<Employee> employees = jdbcTemplate.query(sql, new EmployeeMapper(), pageSize, offset);

In the above code, the ’pageSize’ determines how many records should be returned per page, while ’pageNumber’ determines which page to return.

In conclusion, Spring JDBC provides several ways to optimize performance, and batching, fetch size, and pagination are some of the techniques you can use to improve performance while working with large datasets. By optimizing the performance, you can reduce the application’s response time and improve the user experience.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Spring Framework interview — then scores it.
📞 Practice Spring Framework — free 15 min
📕 Buy this interview preparation book: 100 Spring Framework questions & answers — PDF + EPUB for $5

All 100 Spring Framework questions · All topics