(
tag: string,
transaction: (client: PoolClient) => Promise<ReturnType>
)
| 227 | } |
| 228 | |
| 229 | public postgresTransaction<ReturnType>( |
| 230 | tag: string, |
| 231 | transaction: (client: PoolClient) => Promise<ReturnType> |
| 232 | ): Promise<ReturnType> { |
| 233 | return instrumentQuery(this.statsd, 'query.postgres_transation', undefined, async () => { |
| 234 | const timeout = timeoutGuard(`Postgres slow transaction warning after 30 sec!`) |
| 235 | const client = await this.postgres.connect() |
| 236 | try { |
| 237 | await client.query('BEGIN') |
| 238 | const response = await transaction(client) |
| 239 | await client.query('COMMIT') |
| 240 | return response |
| 241 | } catch (e) { |
| 242 | await client.query('ROLLBACK') |
| 243 | |
| 244 | // if Postgres is down the ROLLBACK above won't work, but the transaction shouldn't be committed either |
| 245 | if (e.message && POSTGRES_UNAVAILABLE_ERROR_MESSAGES.some((message) => e.message.includes(message))) { |
| 246 | throw new DependencyUnavailableError(e.message, 'Postgres', e) |
| 247 | } |
| 248 | throw e |
| 249 | } finally { |
| 250 | client.release() |
| 251 | clearTimeout(timeout) |
| 252 | } |
| 253 | }) |
| 254 | } |
| 255 | |
| 256 | public async postgresBulkInsert<T extends Array<any>>( |
| 257 | // Should have {VALUES} as a placeholder |
no test coverage detected