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();
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
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();
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!

如何在Linux系统中利用PHP脚本进行日志监控随着Linux系统的广泛应用,对于系统的监控和日志分析变得越来越重要。而其中,使用PHP脚本进行日志监控是一种常见的方式。本文将介绍如何使用PHP脚本实现简单的日志监控,并提供具体的代码示例。一、创建PHP脚本文件首先,在Linux系统上创建一个名为“log_monitor.php”的文件,该文件将用于监控指定

如何通过Linux工具实现日志监控和警报?在日常的服务器管理和运维过程中,实时监控和分析日志是非常重要的。Linux系统提供了一些强大的工具,可以帮助我们实现日志监控和警报的功能。本文将介绍如何使用Linux工具来监控和警报日志,并提供一些代码示例。使用tail命令实时查看日志tail命令可以实时查看日志文件的更新内容。通过使用tail命令,我们可以在终端窗

如何解决Linux服务器上的系统日志丢失问题摘要:在Linux服务器上,系统日志对于监控和故障排除非常重要。然而,有时候系统日志可能会丢失或未能正常记录,给故障排查造成困扰。本文将介绍一些解决方案,帮助解决Linux服务器上的系统日志丢失问题。引言:在Linux服务器上,系统日志是一种非常重要的资源,它用于记录系统和应用程序的运行状态、错误信息、警告信息以及

源码编译安装PHPPDOMySQL:实用技巧与注意事项PHP是一种广泛应用的服务器端脚本语言,而MySQL是一个流行的开源关系型数据库管理系统,两者结合使用能够为网站开发提供强大的支持。在实际开发中,经常需要使用PHPPDO扩展来连接MySQL数据库,以实现数据的存储与操作。本文将介绍如何通过源码编译安装PHP,并配置PDO连接MySQL的方法,同时提

Golang作为一种高效、简洁的编程语言,在文件处理方面有着出色的表现。其中,文件监控是一个非常常见且有用的功能,可以帮助我们实时监测文件系统中的变化,从而及时做出相应的处理。本文将深入探究Golang如何实现文件监控功能,并提供具体的代码示例,帮助读者更好地理解和应用这一功能。为何需要文件监控功能?在现代软件开发过程中,文件操作是一个非常重要的环节。特别是

随着互联网和大数据时代的到来,MySQL数据库作为一种常用的开源数据库管理系统,被越来越多的公司和组织所采用。但是,在实际的应用过程中,MySQL数据库也会出现各种各样的错误和异常,如系统崩溃、查询超时、死锁等。这些异常对系统的稳定性和数据的完整性会造成严重的影响,因此,快速检测和分析MySQL的错误和异常是一项非常重要的工作。日志监控是MySQL的重要功能

解决Oracle服务丢失的问题Oracle数据库是众多企业和组织首选的关系型数据库管理系统,但在实际使用过程中,有时会遇到数据库服务丢失的情况,影响系统正常运行。本文将介绍如何解决Oracle服务丢失的问题,并给出具体的代码示例,帮助读者更好地处理这一常见的数据库故障。一、检查Oracle服务状态在解决Oracle服务丢失问题前,首先需要确认服务的当前状态。

如何进行PHP秒杀系统的日志监控和故障排查引言:随着电商行业的快速发展,秒杀活动成为吸引消费者的一种重要方式。而在秒杀活动中,系统的稳定性和高并发处理能力是至关重要的。为了保证秒杀系统的正常运行,需要进行日志监控和故障排查。本文将介绍如何使用PHP进行秒杀系统的日志监控和故障排查,并提供一些代码示例。一、日志监控设置日志级别在秒杀系统的配置文件中,我们可以设

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
