diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index 1491716db..4d695a238 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1,2 +1,4 @@ -- [cygnus-ngsi] Removes "_" in schema name for DM -schema family (#2270, #2201 reopens for Postgis) - +- [cygnus-common][SQL] Fix expiration records tablename used by delete and select (#2265) +- [cygnus-common][SQL] Fix expiration records select with a limit to avoid java out of memory error (#2273) +- [cygnus-ngsi] Removes "_" in schema name for DM -schema family (#2270, #2201 reopens for Postgis) + diff --git a/cygnus-common/src/main/java/com/telefonica/iot/cygnus/backends/sql/SQLBackendImpl.java b/cygnus-common/src/main/java/com/telefonica/iot/cygnus/backends/sql/SQLBackendImpl.java index ad697b4da..d1aec7ae3 100644 --- a/cygnus-common/src/main/java/com/telefonica/iot/cygnus/backends/sql/SQLBackendImpl.java +++ b/cygnus-common/src/main/java/com/telefonica/iot/cygnus/backends/sql/SQLBackendImpl.java @@ -53,6 +53,7 @@ public class SQLBackendImpl implements SQLBackend{ private final int maxLatestErrors; private static final String DEFAULT_ERROR_TABLE_SUFFIX = "_error_log"; private static final int DEFAULT_MAX_LATEST_ERRORS = 100; + private static final String DEFAULT_LIMIT_SELECT_EXP_RECORDS = "4096"; private String nlsTimestampFormat; private String nlsTimestampTzFormat; @@ -308,14 +309,14 @@ private CachedRowSet select(String dataBase, String schema, String tableName, St Connection con = driver.getConnection(dataBase); String query = ""; if (sqlInstance == SQLInstance.MYSQL) { - query = "select " + selection + " from `" + tableName + "` order by recvTime"; + query = "select " + selection + " from `" + tableName + "` order by recvTime desc limit " + DEFAULT_LIMIT_SELECT_EXP_RECORDS; } else if (sqlInstance == SQLInstance.POSTGRESQL) { - if (schema != null) { + if (schema != null && (!tableName.startsWith(schema))) { tableName = schema + '.' + tableName; } - query = "select " + selection + " from " + tableName + " order by recvTime"; + query = "select " + selection + " from " + tableName + " order by recvTime desc limit " + DEFAULT_LIMIT_SELECT_EXP_RECORDS; } else { - query = "select " + selection + " from " + tableName + " order by recvTime"; + query = "select " + selection + " from " + tableName + " order by recvTime desc limit " + DEFAULT_LIMIT_SELECT_EXP_RECORDS; } try { @@ -336,6 +337,7 @@ private CachedRowSet select(String dataBase, String schema, String tableName, St CachedRowSet crs = new CachedRowSetImpl(); crs.populate(rs); // FIXME: close Resultset Objects?? closeSQLObjects(con, stmt); + rs.close(); return crs; } catch (SQLTimeoutException e) { throw new CygnusPersistenceError(sqlInstance.toString().toUpperCase() + " Data select error. Query " + query, "SQLTimeoutException", e.getMessage()); @@ -356,7 +358,7 @@ private void delete(String dataBase, String schema, String tableName, String fil if (sqlInstance == SQLInstance.MYSQL) { query = "delete from `" + tableName + "` where " + filters; } else if (sqlInstance == SQLInstance.POSTGRESQL) { - if (schema != null) { + if (schema != null && (!tableName.startsWith(schema))) { tableName = schema + '.' + tableName; } query = "delete from " + tableName + " where " + filters; @@ -376,7 +378,7 @@ private void delete(String dataBase, String schema, String tableName, String fil stmt.executeUpdate(query); } catch (SQLTimeoutException e) { throw new CygnusPersistenceError(sqlInstance.toString().toUpperCase() + " Data delete error. Query " + query, "SQLTimeoutException", e.getMessage()); - }catch (SQLException e) { + } catch (SQLException e) { closeSQLObjects(con, stmt); persistError(dataBase, schema, query, e); throw new CygnusPersistenceError(sqlInstance.toString().toUpperCase() + " Deleting error", "SQLException", e.getMessage());