当前位置:网站首页>JDBC streaming query and cursor query
JDBC streaming query and cursor query
2022-07-26 05:45:00 【CaptHua】
The source code is based on mysql-connector-5.1
1. obtain statement
PreparedStatement statement = conn.prepareStatement(sql,ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
ResultSet resultSet = statement.executeQuery();
prepareStatement() In the method resultSetType The default value is ResultSet.TYPE_FORWARD_ONLY,resultSetConcurrency The default value is ResultSet.CONCUR_READ_ONLY

2. Get the result set
com.mysql.jdbc.MysqlIO
protected ResultSetImpl getResultSet(StatementImpl callingStatement, long columnCount, int maxRows, int resultSetType, int resultSetConcurrency,
boolean streamResults, String catalog, boolean isBinaryEncoded, Field[] metadataFromCache) throws SQLException {
if (this.connection.versionMeetsMinimum(5, 0, 2) && this.connection.getUseCursorFetch() && isBinaryEncoded && callingStatement != null
&& callingStatement.getFetchSize() != 0 && callingStatement.getResultSetType() == ResultSet.TYPE_FORWARD_ONLY) {
if (usingCursor) {
RowData rows = new RowDataCursor(this, prepStmt, fields);
ResultSetImpl rs = buildResultSetWithRows(callingStatement, catalog, fields, rows, resultSetType, resultSetConcurrency, isBinaryEncoded);
if (usingCursor) {
rs.setFetchSize(callingStatement.getFetchSize());
}
return rs;
}
}
if (!streamResults) {
rowData = readSingleRowSet(columnCount, maxRows, resultSetConcurrency, isBinaryEncoded, (metadataFromCache == null) ? fields : metadataFromCache);
} else {
rowData = new RowDataDynamic(this, (int) columnCount, (metadataFromCache == null) ? fields : metadataFromCache, isBinaryEncoded);
this.streamingData = rowData;
}
ResultSetImpl rs = buildResultSetWithRows(callingStatement, catalog, (metadataFromCache == null) ? fields : metadataFromCache, rowData, resultSetType,
resultSetConcurrency, isBinaryEncoded);
return rs;
}
From the above code , Common query , The result set returned by the churn query is different from that of the cursor query .
3. Common query
The default is normal query
stay readSingleRowSet() In the method , Add all data to rows(ArrayList) in , Return static row data (RowDataStatic)
4. Streaming query
Do not load all data into memory at once , Calling next() When the method is used ,MySQL The driver only gets from the network data stream 1 Data , Then return to the application
Opening mode
- Set up statement.setFetchSize(Integer.MIN_VALUE);
- call createStreamingResultSet()
void enableStreamingResults() throws SQLException;
mysql-connector-8
((com.mysql.cj.jdbc.JdbcStatement)statement).enableStreamingResults()
mysql-connector-5.1
((com.mysql.jdbc.Statement)statement).enableStreamingResults()
StatementImpl.java
/** * We only stream result sets when they are forward-only, read-only, and the * fetch size has been set to Integer.MIN_VALUE * * @return true if this result set should be streamed row at-a-time, rather * than read all at once. */
protected boolean createStreamingResultSet() {
return ((this.query.getResultType() == Type.FORWARD_ONLY) && (this.resultSetConcurrency == java.sql.ResultSet.CONCUR_READ_ONLY)
&& (this.query.getResultFetchSize() == Integer.MIN_VALUE));
}
The first two parameters are the default values , So just set it up fetchSize by Integer.MIN_VALUE that will do , This is mysql-connector Special values customized in , stay JDBC There is no specification in the interface .
enableStreamingResults() You can also enable streaming queries , But it's not jdbc Interface .
The process of obtaining data
- Return dynamic row data

- Every time you call resultSet.next() when , Will get a piece of data

5. Cursor query
Every time you retrieve from the server fetchSize Row data , call next() After traversing , It will fetch data from the server again .
Opening mode
url=jdbc:mysql://192.168.9.19:3307/test?useCursorFetch=true
statement.setFetchSize(20);
The process of obtaining data
- Return cursor row data

- Every time you call resultSet.next() when , Judge the present fetch The location of , If not less than (size-1) Words , It will fetch data again

- stay fetchMoreRows() I will give you mysql Send a message with fetchSize Of fetch command , call nextRow Take the data

6. summary
- These three methods will call MysqlIO#ResultSetRow nextRow() Method to get data .
- Cursor query is also a kind of streaming query , Only the amount of data read from the server each time can be set .
- Streaming query ( Include cursor ) Will use blocking methods from mysql Take the data , So you have to read all the rows in the result set ( Or shut down ) Then you can initiate other queries on this connection , Otherwise, an exception will be thrown .
There are some caveats with this approach. You must read all of the rows in the result set (or close it) before you can issue any other queries on the connection, or an exception will be thrown.
边栏推荐
猜你喜欢

MongoDB 常用命令

Redis事务

Lemon class automatic learning after all

Day110.尚医通:Gateway集成、医院排班管理:科室列表、根据日期统计数据、排班详情

Redis publish subscription

解决Vagrant报错b:48:in `join‘: incompatible character encodings: GBK and UTF-8 (Encoding::Compatib

I also found excellent software and hardware projects, all open source

Introduction to Chinese text error correction task

Ros2 preliminary: basic communication with topic

柠檬班自动化学习毕竟
随机推荐
[personal summary] end of July 24, 2022
又一开源神器,值得收藏学习!
Autumn move - Preparation Plan
Mongodb tutorial Chapter 08 comparison operators
Jupiter notebook shortcut key
leetcode-aboutString
DOM操作--操作节点
Ros2 knowledge: DDS basic knowledge
【STM32系列汇总】博主的STM32实战快速进阶之路(持续更新)
ERROR: Could not open requirements file: [Errno 2] No such file or directory: ‘requirments.txt’
C language explanation series - understanding of functions (4) declaration and definition of functions, simple exercises
How to handle aggregate collection code
Application and value of IVR in VoIP telephone system
MBA-day29 算术-绝对值初步认识
Hack the box -sql injection fundamentals module detailed Chinese tutorial
Jdbc流式查询与游标查询
如何从内存解析的角度理解“数组名实质是一个地址”?
Kingbasees SQL language reference manual of Jincang database (9. Common DDL clauses)
金仓数据库 KingbaseES SQL 语言参考手册 (8. 函数(十一))
Mongodb common commands