{"record":{"id":"bf92440ec3da26d2","repo":"zaproxy/zaproxy","slug":"sqlexception-wrapped-in-databaseexception","errorCode":null,"errorMessage":"SQLException wrapped in DatabaseException","messagePattern":"SQLException wrapped in DatabaseException","errorType":"exception","errorClass":"DatabaseException","httpStatus":null,"severity":"error","filePath":"zap/src/main/java/org/zaproxy/zap/db/sql/SqlTableParam.java","lineNumber":130,"sourceCode":"        SqlPreparedStatementWrapper psInsert = null;\n        try {\n            psInsert = DbSQL.getSingleton().getPreparedStatement(\"param.ps.insert\");\n            psInsert.getPs().setString(1, site);\n            psInsert.getPs().setString(2, type);\n            psInsert.getPs().setString(3, name);\n            psInsert.getPs().setInt(4, used);\n            psInsert.getPs().setString(5, flags);\n            psInsert.getPs().setString(6, values);\n            psInsert.getPs().executeUpdate();\n\n            long id;\n            try (ResultSet rs = psInsert.getLastInsertedId()) {\n                rs.next();\n                id = rs.getLong(1);\n            }\n            return read(id);\n        } catch (SQLException e) {\n            throw new DatabaseException(e);\n        } finally {\n            DbSQL.getSingleton().releasePreparedStatement(psInsert);\n        }\n    }\n\n    @Override\n    public synchronized void update(long paramId, int used, String flags, String values)\n            throws DatabaseException {\n        SqlPreparedStatementWrapper psUpdate = null;\n        try {\n            psUpdate = DbSQL.getSingleton().getPreparedStatement(\"param.ps.update\");\n            psUpdate.getPs().setInt(1, used);\n            psUpdate.getPs().setString(2, flags);\n            psUpdate.getPs().setString(3, values);\n            psUpdate.getPs().setLong(4, paramId);\n            psUpdate.getPs().executeUpdate();\n        } catch (SQLException e) {\n            throw new DatabaseException(e);","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/zaproxy/zaproxy/blob/9d1970a436b1b189bfb588fc88864c80d9baf6a5/zap/src/main/java/org/zaproxy/zap/db/sql/SqlTableParam.java#L112-L148","documentation":"SqlTableParam.insert executes 'param.ps.insert', retrieves the generated key via psInsert.getLastInsertedId(), and then calls read(id) to return the new RecordParam. Any SQLException in the INSERT, the key retrieval, or the follow-up read is wrapped in DatabaseException. Because insert also calls read, failures from the read path (error 242) can be reported here too.","triggerScenarios":"Calling insert(scanId, name, type, used, flags, values) when the connection is broken, a NOT NULL/constraint is violated (e.g., null name/values), getLastInsertedId() returns an empty ResultSet, or the subsequent read(id) throws.","commonSituations":"Inserting params with null/oversized values exceeding column size; concurrent inserts with DB lock contention on HSQLDB; session DB closed during active scan.","solutions":["Check e.getCause() for constraint violation vs connection failure","Ensure no bound value is null or exceeds the column's declared size","Confirm getLastInsertedId() support for the configured DB (HSQLDB identity columns)","Retry the operation if the DB was temporarily locked; otherwise check session health"],"exampleFix":"// before\npsInsert.getPs().setString(3, values); // values may be null\n// after\npsInsert.getPs().setString(3, values != null ? values : \"\"); // guard nulls before insert","handlingStrategy":"try-catch","validationCode":"// Sanitize inputs before insert\nString safeName = name == null ? \"\" : name;\nString safeValues = values == null ? \"\" : (values.length() > MAX ? values.substring(0, MAX) : values);","typeGuard":"static SQLException asSqlCause(DatabaseException e) {\n    return (e.getCause() instanceof SQLException) ? (SQLException) e.getCause() : null;\n}","tryCatchPattern":"try {\n    RecordParam rp = tableParam.insert(scanId, name, type, used, flags, values);\n} catch (DatabaseException e) {\n    SQLException sql = asSqlCause(e);\n    if (sql != null && sql.getMessage().contains(\"integrity\")) {\n        logger.warn(\"Constraint violation inserting param: \" + sql.getMessage());\n    } else {\n        logger.error(\"insert failed\", e);\n    }\n}","preventionTips":["Null-guard and length-limit name/flags/values before insert","Don't insert concurrently from many threads on the same session","Verify HSQLDB identity-column support for getLastInsertedId()","Check e.getCause() for constraint vs connection errors"],"tags":["java","jdbc","database","insert","zap"],"backgroundTag":"sql-exception-wrapped-database-exception","analyzedSha":"9d1970a436b1b189bfb588fc88864c80d9baf6a5","analyzedAt":"2026-09-05T19:26:59.356Z","contentChangedAt":"2026-09-05T19:26:59.356Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}