oracle sql server 连接池不更新表列

问题描述 投票:0回答:1

我在

node.js
oracledb
一起工作。
我已经创建了连接数据库的连接池。

当我从数据库中获取任何数据时,它工作正常。 但是当我尝试更新表列时,它没有更新

oracle sql server table

我有一个

EmpInfo
表,其中包含
empno (primary key)
notes  (varchar(200))
列。我正在尝试更新
notes
专栏 w.r.t
empno

这是我的示例代码如下:

oracleSqlDb.ts

import * as oracledb from 'oracledb';

const oracleDB_connectionConfig = {
    connectString   : "host:port/schema",
    user            : "username",
    password        : "********",
    poolIncrement   : 0,
    poolMax         : 10,
    poolMin         : 10,
    poolTimeout     : 60,
    poolAlias       : 'default'
};

export default class OracleSQL {
    private static isConnected: boolean = false;
    private static poolConn: any;

    static async connect() {
        return await oracledb.createPool(
            oracleDB_connectionConfig, 
            (err, pool) => {
                if (err) {
                    console.error('Connectivity Error: ', {code : err.errorNum, msg: err.message});
                    this.close();
                }
                console.info('Connection Pool Established ...');
                this.isConnected = true;
            });
    }

    static async query(queryStr: string) {
        if (!this.isConnected) {
            console.info('Reconnecting ....');
            await this.connect();
        }
        console.debug(`Query: ${queryStr}`);
        try {
            this.poolConn = await oracledb.getConnection('default');
            const result = await this.poolConn.execute(queryStr);
            if (result) {
                await this.poolConn.close();
                console.debug('Query result: ', result);
                return result;
            }
        } catch(err) {
            console.error('Query execution error: ', {code : err.errorNum, msg: err.message});
            this.close();
            return null;
        }
    }

    static async close() {
        console.debug("Disconnecting Connection Oracle SQL DB .... ");
        if (this.isConnected && this.poolConn && this.poolConn.isHealthy()) {
            await this.poolConn.close().then(() => {
                this.poolConn = null;
                console.info("DB disconnected ...");
            });
        }
        await oracledb.getPool().close(0);
        this.poolConn = null;
        this.isConnected = false;
    }
}

Model.ts

export interface UpdateQueryModel {
    empno : number
    notes : string | ''
};
export interface UpdateDataResponseModel {
    updateRecordId      : number | string,
    updateRecordType    : string,
    updateRecordCount   : number
};

AppRepository.ts

import { UpdateQueryModel, UpdateDataResponseModel } from "./models";
import { OracleSQL } from "./oracleSqlDb";

export class AppRepository {
    public async updateTableColumn(query: UpdateQueryModel): Promise<any> {
        console.debug(`QueryModel: ${JSON.stringify(query)}`);

        const resModel: UpdateDataResponseModel = {
            updateRecordId: query.empno,
            updateRecordType: 'Failure Message',
            updateRecordCount: 0
        };
        try {
            const dbQuery: string = `UPDATE  empinfo
                        SET     notes     =   '${query.notes}'
                        WHERE   empno     =   ${Number(query.empno)}`;

            const result = await OracleSQL.query(dbQuery);
            if (result && result.rowsAffected) {
                return {
                    ...resModel, 
                    updateRecordCount: dbData.rowsAffected,
                    updateRecordType : 'Update Record succesfully'
                };
            } else return resModel;
        } catch(err) {
            return resModel;
        }
    }
}

AppController.ts

import { Request, Response } from "express";
import { UpdateQueryModel } from "./models";
import { AppRepository } from "./AppRepository"

export class AppController {
    public async updateUserNotes(req: Request, res: Response) {
        console.debug(`Request body info  : ${JSON.stringify(req.body)}`);
        const appRepos: AppRepository = new AppRepository();

        let resultList: UpdateDataResponseModel;
        let queryModel: UpdateQueryModel = req.body;
        console.info(`API Url: '${req.originalUrl}' with config: ${JSON.stringify(req.body)}`);

        if(queryModel && Object.keys(queryModel).length && queryModel.empno) {
            resultList = await appRepos.updateTableColumn(queryModel);
            console.silly(`Output Data: ${JSON.stringify(resultList, null, 2)}`);

            return res.status(200).send({
                msg     :   "Succesful Response",
                data    :   resultList
            });
        } else {
            return res.status(200).send({
                msg: `Invalid Query: ${JSON.stringify(queryModel)}`
            });
        }
    }
}

Notes
专栏更新对我来说不起作用。

是数据库连接池问题还是其他问题?请给一些建议。

node.js typescript sql-update node-oracledb
1个回答
0
投票

执行更新后您没有显式提交,因此这些更改从事务外部不可见,并且将在连接关闭时回滚。

您可以将连接设置为自动提交,或者,可能更可靠的是,在执行更新后显式提交:

await this.poolConn.commit();
© www.soinside.com 2019 - 2024. All rights reserved.