@@ -10,8 +10,8 @@ class OracleDB {
10
10
this . log = log ;
11
11
}
12
12
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 ) ) ;
15
15
}
16
16
17
17
/**
@@ -22,33 +22,25 @@ class OracleDB {
22
22
* @see {@link https://github.com/OraOpenSource/orawrap }
23
23
*/
24
24
async _getConnection ( ) {
25
+ const pool = this . pool || ( await this . ensure ( ) ) ;
25
26
try {
26
- return await this . pool . getConnection ( ) ;
27
+ return await pool . getConnection ( ) ;
27
28
} catch ( err ) {
28
29
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 ) ;
36
32
throw err ;
37
33
}
38
34
}
39
35
async getConnection ( ) {
36
+ const pool = this . pool || ( await this . ensure ( ) ) ;
40
37
try {
41
- const connection = await this . pool . getConnection ( ) ;
38
+ const connection = await pool . getConnection ( ) ;
42
39
return new Connection ( connection , this . log ) ;
43
40
} catch ( err ) {
44
41
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 ) ;
52
44
throw err ;
53
45
}
54
46
}
@@ -75,20 +67,21 @@ class OracleDB {
75
67
}
76
68
77
69
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 ;
81
73
}
82
- this . connection = null ;
83
74
}
84
75
85
76
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 ;
89
84
}
90
- this . connection = null ;
91
- this . pool = null ;
92
85
}
93
86
}
94
87
0 commit comments