Skip to content

Commit 6f87093

Browse files
committed
refactor: oracle.js
1 parent da3a1bb commit 6f87093

File tree

3 files changed

+21
-30
lines changed

3 files changed

+21
-30
lines changed

lib/loader.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ function createOneClient(config, app) {
1515
app.coreLogger.info('[egg-oracle] connecting %s', config.connectString);
1616
let client = new OracleDB(config, app.coreLogger);
1717
app.beforeStart(async () => {
18-
// connection init
19-
await client.init();
2018
const { rows } = await client.execute("select to_char(sysdate,'yyyy-MM-dd HH24:mi:ss') from dual", []);
2119
const index = count++;
2220
app.coreLogger.info(`[egg-oracle] instance[${index}] status OK, currentTime: ${rows[0][0]}`);

lib/oracle.js

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class OracleDB {
1010
this.log = log;
1111
}
1212

13-
async init() {
14-
return (this.pool = await oracledb.createPool(this.config));
13+
async ensure() {
14+
return this.pool || (this.pool = await oracledb.createPool(this.config));
1515
}
1616

1717
/**
@@ -22,33 +22,25 @@ class OracleDB {
2222
* @see {@link https://github.com/OraOpenSource/orawrap}
2323
*/
2424
async _getConnection() {
25+
const pool = this.pool || (await this.ensure());
2526
try {
26-
return await this.pool.getConnection();
27+
return await pool.getConnection();
2728
} catch (err) {
2829
this.log.error(err.message);
29-
try {
30-
await this.pool.close();
31-
} catch (closeError) {
32-
this.log.error(closeError.message);
33-
this.pool = await oracledb.createPool(this.config);
34-
throw closeError;
35-
}
30+
await pool.close();
31+
this.pool = await oracledb.createPool(this.config);
3632
throw err;
3733
}
3834
}
3935
async getConnection() {
36+
const pool = this.pool || (await this.ensure());
4037
try {
41-
const connection = await this.pool.getConnection();
38+
const connection = await pool.getConnection();
4239
return new Connection(connection, this.log);
4340
} catch (err) {
4441
this.log.error(err.message);
45-
try {
46-
await this.pool.close();
47-
} catch (closeError) {
48-
this.log.error(closeError.message);
49-
this.pool = await oracledb.createPool(this.config);
50-
throw closeError;
51-
}
42+
await pool.close();
43+
this.pool = await oracledb.createPool(this.config);
5244
throw err;
5345
}
5446
}
@@ -75,20 +67,21 @@ class OracleDB {
7567
}
7668

7769
async close() {
78-
const error = await this.connection.close();
79-
if (error) {
80-
throw error;
70+
if (this.connection && this.connection.close) {
71+
await this.connection.close();
72+
this.connection = null;
8173
}
82-
this.connection = null;
8374
}
8475

8576
async destroy() {
86-
const error = await this.pool.close();
87-
if (error) {
88-
throw error;
77+
if (this.connection && this.connection.close) {
78+
await this.connection.close();
79+
this.connection = null;
80+
}
81+
if (this.pool && this.pool.close) {
82+
await this.pool.close();
83+
this.pool = null;
8984
}
90-
this.connection = null;
91-
this.pool = null;
9285
}
9386
}
9487

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "egg-oracle",
3-
"version": "1.0.11",
3+
"version": "1.0.12",
44
"description": "Oracle database plugin for egg",
55
"eggPlugin": {
66
"name": "oracle"

0 commit comments

Comments
 (0)