捕获错误后无法执行sql语句

时间:2019-09-25 13:07:44

标签: java sql postgresql spring-boot jdbc

当我将记录导入数据库时​​,我捕获了异常(我特别在SQL语句中犯了错误以获得SQLException),并且我希望在另一个表中捕获此更新记录(通常情况下,当我们没有异常时,我也会运行)导入记录后的此语句)。但是问题是另一个表中的记录没有更新,但是在console中,我看到此更新成功。

首先,从FE记录中导入用户,在BE中,我们在控制器(It_tableController)和服务调用DAO({{1})中获得了此POST请求(It_tableService)。 }。在DAO方法中,我捕获了SQLException(在It_tableDAO中),并尝试更新另一个表中的记录(使用It_tableDAO),但是它不起作用。

那么为什么执行sql语句却不更新catch块中的记录?

it_tableController.java:

import_sessionsDAO

It_tableService.java:

    @RequestMapping(value = "/*URL*/", 
                    method = RequestMethod.POST)
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<List<EntityResponse>> addData(@PathVariable Integer originator_id,
                                                        @PathVariable Integer deployment_type_id,
                                                        @PathVariable Integer template_id,  
                                                        @PathVariable String filename,
                                                        @RequestBody String[][] data,
                                                        @CurrentUser UserPrincipal currentUser) {
        logger.info("Performing /*URL*/ POST request");                                                             
        List<EntityResponse> response = it_tableService.addData(
                deployment_type_id, 
                template_id, 
                originator_id, 
                filename, 
                currentUser.getId(),
                currentUser.getUsername(), 
                data);
        return new ResponseEntity<List<EntityResponse>>(response, HttpStatus.ACCEPTED);

    }

it_tableDAO.java:

     public List<EntityResponse> addData (
             Integer deployment_type_id, 
             Integer template_id, 
             Integer originator_id, 
             String filename,
             Long user_id,
             String username,
             String[][] data) {
        if (!import_sessionsDAO.retrieveActiveImport_sessions(deployment_type_id).isEmpty()) 
            { return null; } 
        else {
            Timestamp timestamp = import_sessionsDAO.insertImport_session(username, deployment_type_id, true, filename);
            List<EntityResponse> responseItTable = new ArrayList<EntityResponse>();
                responseItTable = 
                    it_tableDAO.addData(deployment_type_id, template_id, originator_id, filename, user_id, username, data);
            import_sessionsDAO.updateStatusOfImport_session(false, username, deployment_type_id, filename);
                return responseItTable;
        }
     }

     public Integer retrieveLastIdInTable(Integer deployment_type_id) {
        Integer responseLastId = it_tableDAO.retrieveLastIdInTable(deployment_type_id);
        return responseLastId;
    }

import_sessionsDAO.java:

    @Transactional
    public List<EntityResponse> addData(Integer deployment_type_id, 
                                        Integer template_id, 
                                        Integer originator_id, 
                                        String filename, 
                                        Long user_id,
                                        String username,
                                        String[][] data) {

        List<ColumnsNameModel> columnsName = getColumnsNameForDeploymentType(template_id);
        List<EntityResponse> newRows       = new ArrayList<EntityResponse>();
        String tableName                   = getTableNameForDeploymentType(deployment_type_id);     
        String primaryKeyItTable           = getPrimaryKeyItTableQuery(tableName);      
        // int columnsCountToItTable           = getColumnsNameToItTable(columnsName, data);

        for (int i = 1; i < data.length; i++) {
            String INSERT_IT_TABLE_QUERY_part2 = "";
            String updateFieldsOnConflict = "";
            INSERT_IT_TABLE_QUERY = "INSERT INTO kddb." + tableName + " (";  

            for (int j = 0; j < data[0].length; j++) {
                String column = "";
                for (ColumnsNameModel columnName: columnsName) {
                    if (Objects.equals(columnName.getSource_field_name(), data[0][j]) ) {
                        column = columnName.getField_name_it_table();
                        if (j == 0 ) { 
                            INSERT_IT_TABLE_QUERY += column; 
                            updateFieldsOnConflict += column + "=EXCLUDED." + column;
                        }
                        else { 
                            INSERT_IT_TABLE_QUERY += ", " + column; 
                            updateFieldsOnConflict += ", " + column + "=EXCLUDED." + column;
                        }  
                        INSERT_IT_TABLE_QUERY_part2 += ConcatinateToQuery(data[i][j], j);
                    }
                }
            }
            updateFieldsOnConflict += ", import_user_id=EXCLUDED.import_user_id"+
                    ", import_date=EXCLUDED.import_date"+
                    ", import_file=EXCLUDED.import_file";
            INSERT_IT_TABLE_QUERY += ", import_user_id, import_date, import_file)\n VALUES (";

            INSERT_IT_TABLE_QUERY += INSERT_IT_TABLE_QUERY_part2;
            INSERT_IT_TABLE_QUERY += ", " + user_id + ", \'" + new java.sql.Timestamp(new java.util.Date().getTime()) + "\', \'"
                    + filename + "\'";

            String uniqueKey = getUniqueKeyItTableQuery(tableName);
            if (!Objects.equals(uniqueKey, null)) {
                INSERT_IT_TABLE_QUERY += ") \n ON CONFLICT (" + uniqueKey + ") DO UPDATE SET \n" + updateFieldsOnConflict; 
            }
            else {
                INSERT_IT_TABLE_QUERY += ") \n ON CONFLICT (" + primaryKeyItTable + ") DO UPDATE SET \n" + updateFieldsOnConflict; 
            }
            GeneratedKeyHolder holder = new GeneratedKeyHolder();
            try {
            jdbcTemplate.update(new PreparedStatementCreator() {
                @Override
                public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                    PreparedStatement statement = con.prepareStatement(INSERT_IT_TABLE_QUERY, Statement.RETURN_GENERATED_KEYS);
                    return statement;
                }
            }, holder);

            GeneratedKeyHolder holder = new GeneratedKeyHolder();
            try {
            jdbcTemplate.update(new PreparedStatementCreator() {
                @Override
                public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
                    PreparedStatement statement = con.prepareStatement(INSERT_IT_TABLE_QUERY, Statement.RETURN_GENERATED_KEYS);
                    return statement;
                }
            }, holder);
            } catch (Exception e) {
/* ============ THIS STATEMENT NOT UPDATE THE TABLE */
                import_sessionsDAO.updateStatusOfImport_session(false, username, deployment_type_id, filename); 
                logger.info("++++++++++++++++++++++++++++++++++++++++");
            }

            /* some another actions */

        return newRows;
    }

0 个答案:

没有答案