title: Java Spring refcursor queries slows down
body: <p>I'm trying to call postgresql functions working with refcursor parameter. For the first time it works great, but over time, the query speed drops to about 10 seconds per request. 
  Does anyone have any ideas? Thanks in advance for your help. Here is the way I'm calling the funcion:</p>
  
  <pre><code>@Transactional
      public List&lt;Table&gt; getTable(TableFilter entity) {
          List&lt;Table&gt; tableList = new ArrayList&lt;&gt;();
          String cursor = "cursor_" + UUID.randomUUID().toString();
          String function = "{? = call mySchema.gettable('" + cursor + "',?)}";
  
          try (Connection conn = jdbcTemplate.getDataSource().getConnection();
               CallableStatement cs = conn.prepareCall(function);
          ) {
              ObjectMapper objectMapper = new ObjectMapper();
              PGobject jsonbObj = new PGobject();
              jsonbObj.setType("json");
              jsonbObj.setValue(objectMapper.writeValueAsString(entity));
  
              conn.setAutoCommit(false);
              cs.registerOutParameter(1, Types.REF_CURSOR);
              cs.setObject(2, jsonbObj);
              cs.execute();
              ResultSet resultSet = (ResultSet) cs.getObject(1);
              while (resultSet.next()) {
                  tableList.add(new Table(
                          resultSet.getInt("id"),
                          resultSet.getString("number")
                  ));
              }
              conn.commit();
          } catch (SQLException e) {
              log.error("SQL State: {}", e.getSQLState(), e);
          } catch (Exception e) {
              log.error("Error while fetching getTable data.", e);
          }
          return tableList;
      }
  </code></pre>
  
  <p>And the function declared:</p>
  
  <pre><code>mySchema.gettable(INOUT refcur refcursor, inJson json)
  </code></pre>