电脑配置信息:
今天早晨在一个服务器上导数据的时候出现了下面的问题,提示内存不足。
Exception in thread "main" org.hibernate.exception.GenericJDBCException: could not execute query
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.loader.Loader.doList(Loader.java:2235)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
at org.hibernate.loader.Loader.list(Loader.java:2124)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
at com.alpha.test.DataBaseManagerTest.testDatabase(DataBaseManagerTest.java:172)
at com.alpha.test.DataBaseManagerTest.main(DataBaseManagerTest.java:30)
Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: 系统内存不足。请对大型 ResultSet 使用服务器端游标: Java heap space。ResultSet 大小:-1,564,181,511。JVM 总内存大小:259,522,560。
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
at com.microsoft.sqlserver.jdbc.DBComms.receive(Unknown Source)
at com.microsoft.sqlserver.jdbc.IOBuffer.sendCommand(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.sendExecute(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteQuery(Unknown Source)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeQuery(Unknown Source)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1812)
at org.hibernate.loader.Loader.doQuery(Loader.java:697)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.doList(Loader.java:2232)
解决方法:
在hibernate.cfg.xml配置文件中connection.url后面添加selectMethod=cursor。
如果利用jdbc连接数据库也是在在url上加上这句话。
例如:
<property name="connection.url">
jdbc:sqlserver://192.168.0.118:1433;databaseName=Test;selectMethod=cursor
</property>
作用:利用服务器端的游标加快速度
使用:1.执行多个Statements的操作时候用
2.需要手动使用事务的时候使用