gpt4 book ai didi

java - Hibernate与sql server检索数据遇到意外的未知 token

转载 作者:行者123 更新时间:2023-12-03 03:12:13 26 4
gpt4 key购买 nike

使用 c3p0 0.9.2.1、hibernate 4.2.21、JBoss AS 7.1.1 并与 Microsoft SQL Server 2014 - 12.0.2000.8 (X64) 连接。

mssql-jdbc 6.2.2(我测试了 6.4.0,但我们使用的是 6.2.2)用于 JRE7 支持。

场景

从表中以多行形式检索数据,但不是一次检索。这只是一个循环,每次检索一行,最后放入 HashMap 中。

问题和输出

经过长时间的调查和调试,我发现问题范围与表中的两列有关。一种是 VARBINARY(MAX),称为签名,另一种是 VARCHAR(1024),称为状态。如果这两列在一行上都有值,则在检索行时会发生此异常。如果其中任何一个设置为 null,则一切都很好。无论其中值的长度如何。

到目前为止的尝试

  • 更改列顺序,使 VARCHAR 列成为最后一个列
  • 更改列顺序,使 VARBINARY 列成为最后一个列
  • 更改长度(VARBINARY 更改为 8000,VARCHAR 更改为 50)
  • 在其他具有相似或更大长度的列上设置值
  • 记录org.hibernate.session对象方法isConnected()isOpen(),并且在抛出异常之前,输出始终是true 对于两者

问题

这个异常是什么意思?特别是 sql server 日志记录没有显示任何相关信息!!

为什么会发生这种情况以及可能的原因是什么?

   18:53:12,294 SEVERE [com.microsoft.sqlserver.jdbc.internals.TDS.TOKEN]
(http-localhost-127.0.0.1-8080-6) ConnectionID:24 ClientConnectionId:
5bbf35a7-2a3c-4fb5-845a-bbd81d2739ef: getNextResult: Encountered
unexpected unknown token (0x1)
18:53:12,296 SEVERE [com.microsoft.sqlserver.jdbc.internals.TDS.Reader]
(http-localhost-127.0.0.1-8080-6) ConnectionID:24 ClientConnectionId:
5bbf35a7-2a3c-4fb5-845a-bbd81d2739ef got unexpected value in TDS
response at offset:1158
18:53:12,300 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) java.lang.reflect.InvocationTargetException

18:53:12,300 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

18:53:12,301 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

18:53:12,301 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

18:53:12,302 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at java.lang.reflect.Method.invoke(Unknown Source)
.
.
18:53:12,304 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)

18:53:12,305 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)

18:53:12,306 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)

18:53:12,307 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)

18:53:12,308 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)

18:53:12,309 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)

18:53:12,309 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329)

18:53:12,310 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

.
.
18:53:12,312 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)

18:53:12,312 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

.
.

18:53:12,314 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)

18:53:12,314 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

.
.
18:53:12,316 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)

18:53:12,316 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

18:53:12,317 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.catalina.filters.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:101)

18:53:12,318 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280)

18:53:12,318 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248)

18:53:12,319 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)

18:53:12,320 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)

18:53:12,320 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153)

18:53:12,321 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155)

18:53:12,322 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

18:53:12,322 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

18:53:12,323 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368)

18:53:12,324 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)

18:53:12,325 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671)

18:53:12,326 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930)

18:53:12,327 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at java.lang.Thread.run(Unknown Source)

18:53:12,328 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) Caused by: org.hibernate.exception.GenericJDBCException: could not
extract ResultSet

18:53:12,329 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)

18:53:12,330 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:124)

18:53:12,332 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)

18:53:12,333 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:88)

18:53:12,335 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.loader.Loader.getResultSet(Loader.java:2062)

18:53:12,336 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1859)

18:53:12,337 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1838)

18:53:12,338 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.loader.Loader.doQuery(Loader.java:906)

18:53:12,339 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:348)

18:53:12,340 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.loader.Loader.doList(Loader.java:2550)

18:53:12,340 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.loader.Loader.doList(Loader.java:2536)

18:53:12,341 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2366)

18:53:12,342 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.loader.Loader.list(Loader.java:2361)

18:53:12,342 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:495)

18:53:12,343 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:357)

18:53:12,343 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:198)

18:53:12,344 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1230)

18:53:12,345 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.internal.QueryImpl.list(QueryImpl.java:101)

18:53:12,345 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.internal.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:909)

.
.

18:53:12,350 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) ... 38 more

18:53:12,351 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: The
connection is closed.

18:53:12,351 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:228)

18:53:12,352 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at com.microsoft.sqlserver.jdbc.SQLServerConnection.checkClosed(SQLServerConnection.java:1007)

18:53:12,353 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at com.microsoft.sqlserver.jdbc.SQLServerStatement.checkClosed(SQLServerStatement.java:1024)

18:53:12,353 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:198)

18:53:12,354 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(SQLServerPreparedStatement.java:401)

18:53:12,355 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:116)

18:53:12,355 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:79)

18:53:12,356 ERROR [stderr] (http-localhost-127.0.0.1-8080-6) ... 59 more

最佳答案

以下解决了检索问题,并在某些提交行(插入和刷新)中显示。从数据库检索数据到驱动程序似乎太多了,因为 VARBINARY 映射到 Java 中的 LOB,并且大对象会打开流,该流不会很快关闭,直到另一个检索命中数据库和驱动程序,这会导致此问题。解决办法在这里

如果使用 cursor JDBC 属性检索数据,如何解决该问题。当 github 上的 Microsoft 团队清除了从 JDBC 驱动程序直接抛出的异常并提供了有效的示例代码时,我(直到此刻)未能复制该问题。

使用游标只需将 selectMethod=cursor 添加到 JDBC url 即可。要完全了解此选项的作用以及它如何影响数据(特别是 LOB 类型)的检索,请参阅 thisthis文档和文章。

摘自微软文档

selectMethod, String, ["direct" | "cursor"], default is direct

If this property is set to "cursor," a database cursor is created for each query created on the connection for TYPE_FORWARD_ONLY and CONCUR_READ_ONLY cursors. This property is typically required only if the application generates large result sets that cannot be fully contained in client memory. When this property is set to "cursor," only a limited number of result set rows are retained in client memory.

The default behavior is that all result set rows are retained in client memory. This behavior provides the fastest performance when the application is processing all rows.

摘自问答文章

Some questions to consider:

  1. Does the application use scrollable result sets, forward only result sets or some of both? If scrollable, do they fetch all of the data or only scroll to and fetch some of the rows?
  2. Are the result sets generated small or large? This includes number of rows and columns as well as the data contained there in(namely, are there any LOBs)?
  3. Is the application doing transactional work or only operating in auto commit mode?
  4. Does the application ever have more than one result set open at a time on a single connection?

The answers to these questions should help decide which connection parameter is best for the application. First off be aware that eliminating the option from your connection URL altogether is that same as setting the option to “direct” since “direct” is the default setting.

Next, take a look at our description of this option from the JDBC User’s Guide and Reference Manual. Here are some of the key points in this description:

A hint to the driver that determines whether the driver requests a database cursor for Select statements. Performance and behavior of the driver are affected by this property, which is defined as a hint because the driver may not always be able to satisfy the requested method.

If set to direct (the default), the database server sends the complete result set in a single response to the driver when responding to a query. A server-side database cursor is not created if the requested result set type is a forward-only result set. Typically, responses are not cached by the driver. Using this method, the driver must process the entire response to a query before another query is submitted. If another query is submitted (using a different statement on the same connection, for example), the driver caches the response to the first query before submitting the second query. Typically, the direct method performs better than the cursor method.

If set to cursor, a server-side cursor is requested. When returning forward-only result sets, the rows are retrieved from the server in blocks. The setFetchSize() method can be used to control the number of rows that are retrieved for each request when forward-only result sets are returned. Performance tests show that, when returning forward-only result sets, the value of Statement.setFetchSize() significantly impacts performance. There is no simple rule for determining the setFetchSize() value that you should use. We recommend that you experiment with different setFetchSize() values to determine which value gives the best performance for your application. The cursor method is useful for queries that produce a large amount of data, particularly if multiple open result sets are used.

Consider this excerpt from the Performance Considerations section of the User Guide and Reference Manual:

SelectMethod: In most cases, using server-side database cursors impacts performance negatively. However, if the following variables are true for the application, the best setting for this property is cursor, which means use server-side database cursors:

  • Your application contains queries that retrieve large amounts of data.
  • Your application executes a SQL statement before processing or closing a previous large result set and does this multiple times.
  • Large result sets returned by your application use forward-only cursors.

关于java - Hibernate与sql server检索数据遇到意外的未知 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51496139/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com