我正在使用node.js
。oracledb
我
已经创建了连接池来连接数据库。
当我从数据库获取任何数据时,它都运行正常。但当我尝试更新表列时,它却没有更新oracle sql server table
。
我有一张EmpInfo
包含列empno (primary key)
和的表格notes (varchar(200))
。我正尝试更新notes
相对于的列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
列更新对我而言不起作用。
这是数据库连接池问题还是其他问题?请给出一些建议。
执行更新后您没有明确提交,因此这些更改在事务外部不可见,并且会在连接关闭时回滚。
您可以将连接设置为自动提交,或者可能更为稳健地在执行更新后明确提交: