SQL Server retrieving data Encountered unexpected unknown token Hibernate 4.2.21

Note this question is posted on stackoverflow. I think it’s a bug since a whole team has been investigating and testing scenarios without any clue where or what might the issue be. The problem orbits around basic column with type of varbinary. Here are the details:

Using c3p0 0.9.2.1, hibernate 4.2.21, JBoss AS 7.1.1 and connecting with Microsoft SQL Server 2014 - 12.0.2000.8 (X64).

mssql-jdbc 6.2.2 (I tested 6.4.0 for doubt but we are using 6.2.2) is used for JRE7 support.

Scenario

Retrieving data from a table as multiple rows but NOT at once. It’s simply a loop and each time a row retrieved and finally they put in HashMap.

Problem & Output

After long investigation and debugging I found out that the problem scope related with two columns in the table. One is VARBINARY(MAX) called signature and the other is VARCHAR(1024) called status. If both of these columns have a value on a row, this exception occurs on retrieving the next row. If any of them is set to null, all is good. No matter the length of the value in them.

Tries so far

  • Changing the columns order making the VARCHARcolumn is the last cloumn
  • Changing the columns order making the VARBINARY column is the last cloumn
  • Changing the length (VARBINARY to 8000 and VARCHAR to 50)
  • Setting values on other columns with similar or larger length
  • Logging org.hibernate.session object methods isConnected() and isOpen() and until the exception is thrown, the output was always true for both

Questions

What does that exception means ? Specially that sql server logging doesn’t show anything about it !!

Why it’s happening and what might be the cause of this ?

   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

Please write a replicating test case and send a Pull Request so we can investigate it.