DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Working With Multi-Level JSON in CockroachDB
  • Building a Reverse Image Search System Based on Milvus and VGG
  • Introduction to Spring Data JPA Part 8: Many-to-Many Bidirectional
  • Working With Data in Microservices

Trending

  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • AI-Based Threat Detection in Cloud Security
  • How Trustworthy Is Big Data?
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  1. DZone
  2. Coding
  3. Java
  4. Efficiently Transforming JDBC Query Results to JSON

Efficiently Transforming JDBC Query Results to JSON

In this article, learn how to avoid common pitfalls when returning large amounts of data from a REST service.

By 
Greg Brown user avatar
Greg Brown
·
Jan. 13, 24 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
4.5K Views

Join the DZone community and get the full member experience.

Join For Free

A lot of enterprise data is stored in relational databases and accessed via SQL queries. Many web services are little more than HTTP-based wrappers around such queries.

For example, the following service method (built using the open-source Kilo framework) retrieves all rows from an employees table and returns the results to the caller as JSON:

Java
 
@RequestMethod("GET")
public List<Employee> getEmployees() throws SQLException {
    var queryBuilder = QueryBuilder.select(
        EMPLOYEE_NUMBER.as("employeeNumber"),
        FIRST_NAME.as("firstName"),
        LAST_NAME.as("lastName"),
        GENDER,
        BIRTH_DATE.as("birthDate"),
        HIRE_DATE.as("hireDate")
    ).from(Employee.Schema.class);

    try (var connection = getConnection();
        var statement = queryBuilder.prepare(connection);
        var results = new ResultSetAdapter(queryBuilder.executeQuery(statement))) {
        return results.stream().map(result -> BeanAdapter.coerce(result, Employee.class)).toList();
    }
}


Unfortunately, this solution, while straightforward, is not efficient. The entire data set must be traversed twice – once to read it from the database and again to write it to the output stream. Additionally, the caller will not begin receiving a response until all of the rows have been read, and none of the elements will become eligible for garbage collection until all of the results have been written.

For small result sets, the latency and memory implications associated with this approach might be acceptable. However, for larger data volumes, the following alternative may be preferable. The query is executed on a background thread, and the transformed results are streamed back to the caller via a pipe:

Java
 
@RequestMethod("GET")
@ResourcePath("stream")
public List<Employee> getEmployeesStream() {
    var queryBuilder = QueryBuilder.select(
        EMPLOYEE_NUMBER.as("employeeNumber"),
        FIRST_NAME.as("firstName"),
        LAST_NAME.as("lastName"),
        GENDER,
        BIRTH_DATE.as("birthDate"),
        HIRE_DATE.as("hireDate")
    ).from(Employee.Schema.class);

    var pipe = new Pipe<Employee>(4096, 15000);

    executorService.submit(() -> {
        try (var connection = getConnection();
            var statement = queryBuilder.prepare(connection);
            var results = new ResultSetAdapter(queryBuilder.executeQuery(statement))) {
            pipe.accept(results.stream().map(result -> BeanAdapter.coerce(result, Employee.class)));
        } catch (SQLException exception) {
            throw new RuntimeException(exception);
        }
    });

    return pipe;
}


From the Kilo documentation:

The Pipe class provides a vehicle by which a producer thread can submit a sequence of elements for retrieval by a consumer thread. It implements the List interface and produces an iterator that returns values as they become available, blocking if necessary.

The pipe in this example is configured with a capacity of 4K elements and a timeout of 15s. Limiting the capacity ensures that the producer does not do more work than necessary if the consumer fails to retrieve all of the data. Similarly, specifying a timeout ensures that the consumer does not wait indefinitely if the producer stops submitting data.

This implementation of the method is slightly more verbose than the first one. However, because no intermediate buffering is required, results are available to the caller sooner, and CPU and memory load is reduced.

Note that while the examples above use Kilo’s QueryBuilder and ResultSet adapter classes to process results, the same approach can be used with an ORM framework such as Hibernate:

Java
 
@RequestMethod("GET")
@ResourcePath("hibernate")
public List<Employee> getEmployeesHibernate() throws SQLException {
    var configuration = new Configuration();

    configuration.addAnnotatedClass(HibernateEmployee.class);

    try (var connection = getConnection();
        var sessionFactory = configuration.configure().buildSessionFactory();
        var session = sessionFactory.withOptions().connection(connection).openSession()) {
        var criteriaQuery = session.getCriteriaBuilder().createQuery(Employee.class);
        var query = session.createQuery(criteriaQuery.select(criteriaQuery.from(HibernateEmployee.class)));

        return query.list();
    }
}


Java
 
@RequestMethod("GET")
@ResourcePath("hibernate-stream")
public List<Employee> getEmployeesHibernateStream() {
    var pipe = new Pipe<Employee>(4096, 15000);

    executorService.submit(() -> {
        var configuration = new Configuration();

        configuration.addAnnotatedClass(HibernateEmployee.class);

        try (var connection = getConnection();
            var sessionFactory = configuration.configure().buildSessionFactory();
            var session = sessionFactory.withOptions().connection(connection).openSession()) {
            var criteriaQuery = session.getCriteriaBuilder().createQuery(Employee.class);
            var query = session.createQuery(criteriaQuery.select(criteriaQuery.from(HibernateEmployee.class)));

            try (var stream = query.stream()) {
                pipe.accept(stream);
            }
        } catch (SQLException exception) {
            throw new RuntimeException(exception);
        }
    });

    return pipe;
}


The performance of the Hibernate versions is comparable to the preceding examples, although they do result in slightly higher memory consumption and GC activity.

The complete source code for this example can be found here.

Database JSON Java (programming language) Data set

Published at DZone with permission of Greg Brown, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Working With Multi-Level JSON in CockroachDB
  • Building a Reverse Image Search System Based on Milvus and VGG
  • Introduction to Spring Data JPA Part 8: Many-to-Many Bidirectional
  • Working With Data in Microservices

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!