由于hibernate不支持子查询,在hibernate的WHERE条件语句中使用IN查询时,如果超过1000条数据,并不是MySQL的限制,会报如下错误:
15:08:40.134 logback [http-nio-8096-exec-3] ERROR o.a.c.c.C.[.[.[.[dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.StackOverflowError] with root cause
java.lang.StackOverflowError: null
at antlr.BaseAST.toString(BaseAST.java:333)
at antlr.BaseAST.toStringList(BaseAST.java:341)
at antlr.BaseAST.toStringList(BaseAST.java:347)
at antlr.BaseAST.toStringList(BaseAST.java:347)
at antlr.BaseAST.toStringList(BaseAST.java:347)
解决办法:控制IN内数据小于1000条,或分成多个IN,用OR连接,或者将IN中的数据放到一个字符串中,mysql可以解析处理。
有文章说hibernate已经解决了这个问题,待验证。
后来也遇到过同样的报错,当时觉得是由于日志打印级别为debug级别,导出输出日志信息过多,占用内存过大,导致堆栈溢出。其实这两者有着必然的联系,当时没有深入跟踪代码,导致忽略了报错真正的原因。
跟踪代码,打印堆栈信息就会一目了然了
综上,其实是在日志级别设置debug时,会导致堆栈溢出。