Table of Contents
Question content
Workaround
Home Java How to monitor Spring Data JPA streams

How to monitor Spring Data JPA streams

Feb 22, 2024 pm 02:01 PM
Log monitoring

php editor Youzi brings you java questions and answers about monitoring Spring Data JPA flow. During development, monitoring data flows in real time is critical for system performance optimization and troubleshooting. This article will introduce how to monitor Spring Data JPA flow, allowing you to better understand the data processing process, detect problems in time, and handle them accordingly. Let’s discuss how to effectively monitor Spring Data JPA flow and improve system stability and performance!

Question content

I am trying to use spring data jpa streaming as instructed on this blog. However, I can't monitor the process or progress with any logs. Should I see multiple sql queries printed in the log when the process tries to extract data in batches? If not, then how do I know that all the rows are not loaded at once?

Other blogs like this one and this one suggested that I should set mysql's hint_fetch_size to integer.min_value which I thought might be the solution, but this throws The following exception:

2024-01-29 14:40:20.843 warning 78247 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.sqlexceptionhelper: sql Error: 0, sqlstate: s1000 2024-01-29 14:40:20.843 Error 78247 --- [nio-8080-exec-1] o.h.engine.jdbc.spi.sqlexceptionhelper: Streaming result set com.mysql.cj.protocol.a.result. resultsetrowsstreaming@4ca63fa5 is still active. No statements shall be issued while any streaming result set is open and in use on a given connection. Before trying more queries, make sure you have called .close() on any active streaming result sets. End time: 48 org.springframework.orm.jpa.jpasystemexception: Unable to extract resultset; nested exception is org.hibernate.exception.genericjdbcexception: Unable to extract resultset at org.springframework.orm.jpa.vendor.hibernatejpadialect.converthibernateaccessexception(hibernatejpadialect.java:331)

This is my repository code:

@QueryHints(value = {
        @QueryHint(name = org.hibernate.jpa.QueryHints.HINT_FETCH_SIZE, value = "" + Integer.MIN_VALUE),
        @QueryHint(name = org.hibernate.jpa.QueryHints.HINT_CACHEABLE, value = "false"),
        @QueryHint(name = org.hibernate.jpa.QueryHints.HINT_READONLY, value = "true"),
})
@Query("SELECT s FROM Salary s")
Stream<Salary> findAllStream();
Copy after login

I guess I'd like to get a guarantee if the above is the correct way to use stream queries in spring data jpa since I can't reliably monitor the performance of streaming myself?

renew

The above exception occurs due to repeated calls to the findallstream method in the same calling method. Removing one of them fixed the exception.

Workaround

I can't find any log configuration to show whether the data is being fetched in batches. But I did find a way to test performance locally.

To test the streaming functionality, I need to access a database containing millions of records. I use docker image https://www.php.cn/link/7092d5eb1bbca1a22bdc69ba3f517e68 to use mysql employee data

After setting up the docker image, I'm having trouble connecting mysql workbench with the server. It looks like the docker image is not configured to accept ssl connections by default. I had to disable the use ssl flag to be able to establish a connection. This setting appears in the mysql workbench under the ssl tab.

The connection string in the application must also be configured as follows:

spring.datasource.url=jdbc:mysql://localhost:3307/employees?verifyservercertificate=false&usessl=false&requiressl=false
Copy after login

The data in the employee database consists of a table named salaries, which has approximately 2.8 million rows.

For testing, I wrote a small spring data jpa application that has the following methods in the repository class and a simple controller to call these methods:

@Override
List<Salary> findAll();

@QueryHints(value = {
        @QueryHint(name = org.hibernate.jpa.QueryHints.HINT_FETCH_SIZE, value = "" + Integer.MIN_VALUE),
        @QueryHint(name = org.hibernate.jpa.QueryHints.HINT_CACHEABLE, value = "false"),
        @QueryHint(name = org.hibernate.jpa.QueryHints.HINT_READONLY, value = "true"),
})

@Query("SELECT s FROM Salary s")
Stream<Salary> findAllStream();
Copy after login

I then wrote a small piece of code to convert the read data into a json object and then write it back to the file using multiple threads. This is to simulate processing in real-life cases.

This is what I observed.

  • When using the list method, memory usage increases significantly. The initial query took most of the time, but once all the data was loaded, the actual data processing was completed quickly.

  • When using the stream method, the impact on memory usage is almost unnoticeable. But overall, the performance of the completion processing part is similar or even worse compared to the list method.

in conclusion

My above findings lead me to conclude that the stream return type of a repository method should only be used when there is a risk of running out of memory, i.e. getting an out memory exception . Otherwise, if your application is already running on a large enough server, the overall impact on memory usage will be barely noticeable and will only be temporary if your process completes quickly.

Memory usage statistics from intellij profiler

  • left -> When list method runs
  • Right -> When the stream method is running

The above is the detailed content of How to monitor Spring Data JPA streams. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use PHP scripts for log monitoring in Linux systems How to use PHP scripts for log monitoring in Linux systems Oct 05, 2023 am 11:36 AM

How to use PHP scripts for log monitoring in Linux systems. With the widespread application of Linux systems, system monitoring and log analysis have become more and more important. Among them, using PHP scripts for log monitoring is a common way. This article will introduce how to use PHP scripts to implement simple log monitoring and provide specific code examples. 1. Create a PHP script file. First, create a file named "log_monitor.php" on the Linux system. This file will be used to monitor the specified

How to implement log monitoring and alerting through Linux tools? How to implement log monitoring and alerting through Linux tools? Jul 28, 2023 pm 08:41 PM

How to implement log monitoring and alerting through Linux tools? In the daily server management and operation and maintenance process, real-time monitoring and analysis of logs is very important. The Linux system provides some powerful tools that can help us implement log monitoring and alarm functions. This article explains how to use Linux tools to monitor and alert logs, and provides some code examples. Use the tail command to view logs in real time. The tail command can view the updated content of log files in real time. By using the tail command, we can

How to solve the problem of Linux server log loss How to solve the problem of Linux server log loss Jun 30, 2023 pm 04:37 PM

How to solve the problem of missing system logs on Linux servers Summary: On Linux servers, system logs are very important for monitoring and troubleshooting. However, sometimes system logs may be lost or fail to record properly, causing troubleshooting. This article will introduce some solutions to help solve the problem of system log loss on Linux servers. Introduction: On a Linux server, the system log is a very important resource. It is used to record the running status, error information, warning information and

In-depth exploration: How Golang implements file monitoring function In-depth exploration: How Golang implements file monitoring function Feb 23, 2024 am 09:27 AM

As an efficient and concise programming language, Golang has excellent performance in file processing. Among them, file monitoring is a very common and useful function, which can help us monitor changes in the file system in real time, so as to make corresponding processing in a timely manner. This article will delve into how Golang implements the file monitoring function and provide specific code examples to help readers better understand and apply this function. Why do you need file monitoring capabilities? In the modern software development process, file operation is a very important link. in particular

Source code compilation and installation of PHP PDO MySQL: practical tips and precautions Source code compilation and installation of PHP PDO MySQL: practical tips and precautions Mar 07, 2024 pm 09:27 PM

Source code compilation and installation of PHPP DOMySQL: Practical tips and precautions. PHP is a widely used server-side scripting language, and MySQL is a popular open source relational database management system. The combination of the two can provide powerful support for website development. In actual development, it is often necessary to use the PHPPDO extension to connect to the MySQL database to achieve data storage and operation. This article will introduce how to install PHP through source code compilation, configure PDO to connect to MySQL, and provide

How to solve the problem of Oracle service loss? How to solve the problem of Oracle service loss? Mar 08, 2024 pm 04:27 PM

Solving the problem of Oracle service loss Oracle database is the preferred relational database management system for many enterprises and organizations. However, in actual use, database service loss sometimes occurs, affecting the normal operation of the system. This article will introduce how to solve the problem of Oracle service loss, and give specific code examples to help readers better handle this common database failure. 1. Check the Oracle service status. Before solving the problem of Oracle service loss, you first need to confirm the current status of the service.

MySql log monitoring: How to quickly detect and analyze MySQL errors and exceptions MySql log monitoring: How to quickly detect and analyze MySQL errors and exceptions Jun 15, 2023 pm 09:42 PM

With the advent of the Internet and big data era, MySQL database, as a commonly used open source database management system, is adopted by more and more companies and organizations. However, in the actual application process, various errors and exceptions may occur in the MySQL database, such as system crashes, query timeouts, deadlocks, etc. These anomalies will have a serious impact on system stability and data integrity. Therefore, quickly detecting and analyzing MySQL errors and anomalies is a very important task. Log monitoring is an important function of MySQL

How to monitor and troubleshoot logs of PHP flash sale system How to monitor and troubleshoot logs of PHP flash sale system Sep 19, 2023 am 08:24 AM

How to carry out log monitoring and troubleshooting of PHP flash sale system Introduction: With the rapid development of the e-commerce industry, flash sale activities have become an important way to attract consumers. In flash sale activities, system stability and high concurrency processing capabilities are crucial. In order to ensure the normal operation of the flash sale system, log monitoring and troubleshooting are required. This article will introduce how to use PHP for log monitoring and troubleshooting of the flash sale system, and provide some code examples. 1. Log monitoring setting log level In the configuration file of the flash sale system, we can set