简体   繁体   English

使用JDBC驱动程序在SQL Server中设置默认行预取

[英]Set a default row prefetch in SQL Server using JDBC driver

I have an application where I want to define the default number of rows to prefetch for a connection, using for both Oracle and SQL Server drivers. 我有一个应用程序,我想定义预取连接的默认行数,用于Oracle和SQL Server驱动程序。 The Oracle driver has a the OracleConnection interface, which provides the setDefaultRowPrefetch method to do it, but I didn't find anything equivalent for the SQL Server driver. Oracle驱动程序有一个OracleConnection接口,它提供了setDefaultRowPrefetch方法来执行此操作,但我没有找到与SQL Server驱动程序等效的任何内容。

There is a method to define the default row prefetch for a connection using the SQL Server JDBC driver? 有一种方法可以使用SQL Server JDBC驱动程序为连接定义默认行预取?

The usual ways to set row fetch size are: 设置行提取大小的常用方法是:

  1. Via java.sql.Connection vendor implementation class custom method (eg OracleConnection.setDefaultRowPrefetch ) 通过java.sql.Connection供应商实现类自定义方法 (例如OracleConnection.setDefaultRowPrefetch
  2. Via java.sql.Statement.setFetchSize(int) : gives a hint to the driver as to the row fetch size for all ResultSets obtained from this Statement . 通过java.sql.Statement.setFetchSize(int) :向驱动程序提供有关从此Statement获取的所有ResultSets的行提取大小的提示。 This method is inherited by PreparedStatement and CallableStatement . 此方法由PreparedStatementCallableStatement继承。 Most JDBC drivers support it. 大多数JDBC驱动程序都支持它
  3. Via java.sql.ResultSet.setFetchSize(int) : gives a hint to the driver as to the row fetch size for all this ResultSet . 通过java.sql.ResultSet.setFetchSize(int) :向驱动程序提供有关所有此ResultSet的行提取大小的提示。

MS SQL Server JDBC driver does not support any of these ways: MS SQL Server JDBC驱动程序不支持以下任何方式:

  1. MSSQL driver has no such a method. MSSQL驱动程序没有这样的方法。
  2. Unfortunately, while most drivers respect the hint, the MSSQL driver does not. 不幸的是,虽然大多数驱动程序都尊重提示,但MSSQL驱动程序却没有。 So not useful for you. 所以对你没用。 See What does Statement.setFetchSize(nSize) method really do in SQL Server JDBC driver? 请参阅SQL Server JDBC驱动程序中Statement.setFetchSize(nSize)方法的作用是什么?
  3. Same problem as Statement . Statement相同的问题。

By default it retrieves all the rows from database unless you specify cursor type in the JDBC driver. 默认情况下,除非在JDBC驱动程序中指定游标类型,否则它将从数据库中检索所有行。 MSSQL driver can't directly control the fetch size using the usual methods. MSSQL驱动程序无法使用常用方法直接控制提取大小。

Solutions: 解决方案:

  • Cast your Statement to SQLServerStatement and use the method setMaxRows(int) . 将您的StatementSQLServerStatement并使用方法setMaxRows(int) Why they didn't implement this within the standard method Steve Ballmer only knows ;^) 为什么他们没有在标准方法中实现这一点史蒂夫鲍尔默只知道; ^)
  • Create your driver with a cursor type. 使用游标类型创建驱动程序。 The default fetch size for a cursor is 1. Set the Connection string property selectMethod=cursor . 游标的默认提取大小为1.设置Connection字符串属性selectMethod=cursor Alternatively, you can create the Statement with com.microsoft.sqlserver.jdbc.SQLServerResultSet.TYPE_SS_SERVER_CURSOR_FORWARD_ONLY scrollability for forward-only, read-only access, and then use the setFetchSize method to tune performance. 或者,您可以使用com.microsoft.sqlserver.jdbc.SQLServerResultSet.TYPE_SS_SERVER_CURSOR_FORWARD_ONLY可滚动性创建Statement ,以进行只进,只读访问,然后使用setFetchSize方法调整性能。 http://technet.microsoft.com/en-us/library/aa342344%28SQL.90%29.aspx http://technet.microsoft.com/en-us/library/aa342344%28SQL.90%29.aspx
  • Use (proprietary) SQL to limit the number of rows returned ( not the same thing as setting the fetch size ): SET ROWCOUNT or SELECT TOP N 使用(专有)SQL来限制返回的行数( 与设置提取大小不同 ): SET ROWCOUNTSELECT TOP N
  • Switch to open source jTDS driver, specially made to overcome the problems of the SQL Server driver. 切换到开源jTDS驱动程序,专门用于克服SQL Server驱动程序的问题。 It's a superior driver. 这是一个优秀的驱动力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM