@things-factory/integration-base 6.2.115 → 6.2.118

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@things-factory/integration-base",
3
- "version": "6.2.115",
3
+ "version": "6.2.118",
4
4
  "main": "dist-server/index.js",
5
5
  "browser": "client/index.js",
6
6
  "things-factory": true,
@@ -47,5 +47,5 @@
47
47
  "devDependencies": {
48
48
  "@types/cron": "^2.0.1"
49
49
  },
50
- "gitHead": "1daed375a4ad4c542f49ab14495b6613a65b1a04"
50
+ "gitHead": "3b0a5f498c8fd799c238cec391c8368ec733e901"
51
51
  }
@@ -5,7 +5,7 @@ import { Connector } from '../types'
5
5
  import { InputConnection } from '../../service/connection/connection-type'
6
6
 
7
7
  try {
8
- var Client = require('oracledb')
8
+ var oracledb = require('oracledb')
9
9
  } catch (err) {
10
10
  logger.error('oracledb module loading failed')
11
11
  }
@@ -17,7 +17,7 @@ export class OracleConnector implements Connector {
17
17
  ConnectionManager.logger.info('oracle-connector connections are ready')
18
18
  }
19
19
 
20
- async connect(connection: InputConnection) {
20
+ async recreatePool(connection: InputConnection) {
21
21
  const {
22
22
  name,
23
23
  endpoint,
@@ -25,12 +25,13 @@ export class OracleConnector implements Connector {
25
25
  domain
26
26
  } = connection
27
27
 
28
- if (!Client) {
28
+ if (!oracledb) {
29
29
  throw new Error('oracledb module loading failed')
30
30
  }
31
31
 
32
32
  const poolAlias = `${domain.name}-${name}`
33
- const client = await Client.createPool({
33
+ await oracledb.getPool(poolAlias).close(10)
34
+ await oracledb.createPool({
34
35
  user,
35
36
  password,
36
37
  // when oracle not using default port must add connection string with port like localhost:port
@@ -41,33 +42,106 @@ export class OracleConnector implements Connector {
41
42
  poolAlias
42
43
  })
43
44
 
45
+ ConnectionManager.logger.info(`Oracle Database(${connection.name}:${database}) at ${endpoint} recreated.`)
46
+ }
47
+
48
+ async connect(connection: InputConnection) {
49
+ const {
50
+ name,
51
+ endpoint,
52
+ params: { user, password, database, poolMin, poolMax, poolIncrement },
53
+ domain
54
+ } = connection
55
+
56
+ if (!oracledb) {
57
+ throw new Error('oracledb module loading failed')
58
+ }
59
+
60
+ const poolAlias = `${domain.name}-${name}`
61
+
62
+ var enableStatistics = true
63
+ const pool = await oracledb.createPool({
64
+ user,
65
+ password,
66
+ // when oracle not using default port must add connection string with port like localhost:port
67
+ connectString: `${endpoint.trim()}/${database}`,
68
+ poolMin,
69
+ poolMax,
70
+ poolIncrement,
71
+ poolAlias,
72
+ enableStatistics
73
+ })
44
74
 
45
75
  ConnectionManager.addConnectionInstance(connection, {
46
76
  query: async (query, params) => {
47
- const connection = await Client.getConnection(poolAlias)
48
- const result = (
49
- await connection.execute(query, params, {
50
- outFormat: Client.OBJECT
51
- })
52
- ).rows
53
- await connection.close()
54
- return result
77
+ let dbConnection: any = {}
78
+ let taskResult: any = {}
79
+ try {
80
+ dbConnection = await oracledb.getConnection(poolAlias)
81
+
82
+ taskResult = (
83
+ await dbConnection.execute(query, params, {
84
+ outFormat: oracledb.OBJECT
85
+ })
86
+ ).rows
87
+ } catch (e) {
88
+ if (e.name === 'Error' && e.message.includes('NJS-040')) {
89
+ await this.recreatePool(connection)
90
+ }
91
+ throw e
92
+ }
93
+ finally {
94
+ await dbConnection.close()
95
+ }
96
+ return taskResult
55
97
 
56
98
  },
57
99
  execute: async (procedure, params) => {
58
- // TODO: need to check if this is available when procedure string is a common query.
59
- procedure = `BEGIN
60
- ${procedure}
61
- END;`
62
- const connection = await Client.getConnection(poolAlias)
63
- const result = await connection.execute(procedure, params, {
64
- outFormat: Client.OBJECT
65
- })
66
- await connection.close()
67
- return result
100
+ let dbConnection: any = {}
101
+ let taskResult: any = {}
102
+ try {
103
+ // TODO: need to check if this is available when procedure string is a common query.
104
+ procedure = `BEGIN
105
+ ${procedure}
106
+ END;`
107
+ dbConnection = await oracledb.getConnection(poolAlias)
108
+
109
+ // console.log('\n************* stat after get ****************')
110
+ // await oracledb.getPool(poolAlias).logStatistics()
111
+
112
+ const result = await dbConnection.execute(procedure, params, {
113
+ outFormat: oracledb.OBJECT
114
+ })
115
+
116
+ let paramKeys = Object.keys(params)
117
+
118
+ for (const paramKey of paramKeys) {
119
+ if (params[paramKey].dir === oracledb?.BIND_OUT) {
120
+ if (params[paramKey].type === oracledb?.CURSOR) {
121
+ const resultSetTemp = result.outBinds[paramKey]
122
+ taskResult[paramKey] = await resultSetTemp.getRows()
123
+ await resultSetTemp.close()
124
+ } else {
125
+ taskResult[paramKey] = result.outBinds[paramKey]
126
+ }
127
+ }
128
+ }
129
+ } catch (e) {
130
+ if (e.name === 'Error' && e.message.includes('NJS-040')) {
131
+ await this.recreatePool(connection)
132
+ }
133
+ throw e
134
+ }
135
+ finally {
136
+ await dbConnection.close()
137
+
138
+ // console.log('\n************* stat after close ****************')
139
+ // await oracledb.getPool(poolAlias).logStatistics()
140
+ }
141
+ return taskResult
68
142
  },
69
143
  close: async () => {
70
- await Client.getPool(poolAlias).close(10)
144
+ await oracledb.getPool(poolAlias).close(10)
71
145
  }
72
146
  })
73
147
 
@@ -97,23 +97,8 @@ async function OracleProcedure(step: InputStep, context: Context) {
97
97
 
98
98
  const result = await dbconnection.execute(code, procedureParameters)
99
99
 
100
- var taskResult = {}
101
- let paramKeys = Object.keys(procedureParameters)
102
-
103
- for (const paramKey of paramKeys) {
104
- if (procedureParameters[paramKey].dir === oracledb?.BIND_OUT) {
105
- if (procedureParameters[paramKey].type === oracledb?.CURSOR) {
106
- const resultSetTemp = result.outBinds[paramKey]
107
- taskResult[paramKey] = await resultSetTemp.getRows()
108
- await resultSetTemp.close()
109
- } else {
110
- taskResult[paramKey] = result.outBinds[paramKey]
111
- }
112
- }
113
- }
114
-
115
100
  return {
116
- data: taskResult
101
+ data: result
117
102
  }
118
103
  }
119
104