在选择语句的情况下解决“临时文件写入失败”的方法

问题描述

我们面临一个奇怪的问题。在执行SQL查询(select语句)时,在我们的一项服务(应用程序中大约有12到13个服务)中,该查询返回大约30 40行,执行时间不到一秒钟。

我们间歇性地收到“临时文件写入失败”异常

下面是一段代码

 Query qry = null;

  //selectquery is of type String whose value is read from properties file using @Value("${})
  qry = entityManager.createNativeQuery(selectquery);
              //set few parameters here   
             
            
 List result = qry.getResultList();
           
           

我们正处于例外之下

<Log_Created_Date: 2020/08/11 17:59:21> <Log_Level: ERROR> <application_name: abc> <Class_Name: com.pqr> <Method_Name: someMethod> <Exception: java.sql.sqlException: Temporary file write failure.

完整的错误日志如下

<Log_Created_Date: 2020/08/11 17:59:21> <Log_Level: ERROR> <application_name: abc> <Class_Name: com.pqr> <Method_Name: someMethod> <Exception: java.sql.sqlException: Temporary file write failure.
        at com.MysqL.cj.jdbc.exceptions.sqlError.createsqlException(sqlError.java:129)
        at com.MysqL.cj.jdbc.exceptions.sqlError.createsqlException(sqlError.java:97)
        at com.MysqL.cj.jdbc.exceptions.sqlExceptionsMapping.translateException(sqlExceptionsMapping.java:122)
        at com.MysqL.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
        at com.MysqL.cj.jdbc.ClientPreparedStatement.executeQuery(ClientPreparedStatement.java:1003)
        at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:82)
        at org.apache.tomcat.dbcp.dbcp2.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:82)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:60)
        ... 158 common frames omitted
Wrapped by: org.hibernate.exception.GenericJDBCException: Could not extract ResultSet
        at org.hibernate.exception.internal.StandardsqlExceptionConverter.convert(StandardsqlExceptionConverter.java:47)
        at org.hibernate.engine.jdbc.spi.sqlExceptionHelper.convert(sqlExceptionHelper.java:111)
        at org.hibernate.engine.jdbc.spi.sqlExceptionHelper.convert(sqlExceptionHelper.java:97)
        at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:69)
        at org.hibernate.loader.Loader.getResultSet(Loader.java:2167)
        at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1930)
        at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1892)
        at org.hibernate.loader.Loader.doQuery(Loader.java:937)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:340)
        at org.hibernate.loader.Loader.doList(Loader.java:2689)
        at org.hibernate.loader.Loader.doList(Loader.java:2672)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2506)
        at org.hibernate.loader.Loader.list(Loader.java:2501)
        at org.hibernate.loader.custom.Customloader.list(Customloader.java:338)
        at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:2223)
        at org.hibernate.internal.AbstractSharedSessionContract.list(AbstractSharedSessionContract.java:1053)
        at org.hibernate.query.internal.NativeQueryImpl.doList(NativeQueryImpl.java:168)
        at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1506)
        ... 144 common frames omitted
Wrapped by: javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: Could not extract ResultSet
        at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:149)
        at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
        at org.hibernate.query.internal.AbstractProducedQuery.list(AbstractProducedQuery.java:1515)
        at org.hibernate.query.Query.getResultList(Query.java:146)
        at com.pqr.someMethod(pqr.java:259)

在所有服务中,此异常仅出现在一项服务中。我们正在从所有服务连接到同一数据库其他服务与这一服务的区别在于,它的使用频率更高。

我们不会更改任何内容,也不会尝试创建任何内容。这是简单的选择语句。

我们检查了临时空间(数据库服务器上的/ tmp目录),并且每次发生此异常时都有足够的可用空间。

感谢您的帮助。谢谢!

解决方法

这恰好是数据库/服务器相关的问题。

在部署/运行数据库的服务器上增加ulimit参数的值可以解决此问题。

MySQL使用临时表来解决复杂的查询。