У меня есть серверная база данных aurora, и мне нужно написать лямбду, которая использует API-интерфейс rdsDataservice для обновления списка таблиц, связанных с внешним ключом. Итак, я использую поддержку транзакций здесь. Когда я вызвал beginTransaction, он вызывается дважды. Я не уверен, что не так.
async (orderMock, orderId) => {
const rdsDataService = new RDSDataService({
accessKeyId: '*******',
secretAccessKey: '****************',
region: 'us-east-1',
sslEnabled: true,
});
const beginTransactionParams = {
resourceArn: '*************',
secretArn: '******************',
database: '************',
};
let transactionId = '';
await rdsDataService.beginTransaction(
beginTransactionParams,
(beginTransactionError, beginTransactionMessage) => {
if (beginTransactionError) {
console.log('beginTransactionError', beginTransactionError, beginTransactionError.stack);
}
console.log('beginTransactionMessage', beginTransactionMessage);
transactionId = beginTransactionMessage.transactionId;
},
).promise();
console.log('transactionId', transactionId);
const queryResult = await rdsDataService.executeStatement({
resourceArn: '****************',
secretArn: '********************',
sql: 'insert into orders(id,user_profile_id) values(:id,:userProfileId)',
database: '**********',
continueAfterTimeout: true,
parameters: [{
name: 'id',
value: {
stringValue: orderId,
},
},
{
name: 'userProfileId',
value: {
stringValue: orderMock.orders.userProfileId,
},
},
],
transactionId,
}, (err, updatedData) => {
if (err) {
console.log('err in inserting order Table', err);
const rollbackParam = {
resourceArn: '****************',
secretArn: '*********************',
transactionId,
};
rdsDataService.rollbackTransaction(rollbackParam, (rollError, rollMessage) => {
if (rollError) console.log('rollError', rollError);
console.log('rollMessage', rollMessage);
}).promise();
}
console.log('data', JSON.stringify(updatedData));
}).promise();
const param = {
resourceArn: '*****************',
secretArn: '**************************',
transactionId,
};
console.log('transactioncommitParams', param);
await rdsDataService.commitTransaction(param, (error, msg) => {
if (error) console.log('error in commit Transaction', error);
console.log('msg', msg);
}).promise();
return queryResult;
};
Обратный вызов начала транзакции вызывается дважды. Это логи
beginTransactionMessage { transactionId: '***********' }
2019-10-01T09:39:06.357Z cd6a4379-f7ae-1b9c-c083-ffc652a32e70 transactionId ****************************
2019-10-01T09:39:06.383Z cd6a4379-f7ae-1b9c-c083-ffc652a32e70 beginTransactionMessage { transactionId: '******************' }
2019-10-01T09:39:07.126Z cd6a4379-f7ae-1b9c-c083-ffc652a32e70 data {"generatedFields":[],"numberOfRecordsUpdated":1}
2019-10-01T09:39:07.126Z cd6a4379-f7ae-1b9c-c083-ffc652a32e70 transactioncommitParams { resourceArn: '**************',
secretArn: '*******************',
transactionId: '**************************' }
2019-10-01T09:39:07.204Z cd6a4379-f7ae-1b9c-c083-ffc652a32e70 err in inserting order Table { BadRequestException: Duplicate entry '7570951d-1c5c-4e49-a5b1-d0c255c00e9d' for key 'PRIMARY'
at Object.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/json.js:51:27)
at Request.extractError (/var/runtime/node_modules/aws-sdk/lib/protocol/rest_json.js:55:8)
at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:683:14)
at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)
at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)
at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10
at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)
at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:685:12)
message: 'Duplicate entry \'7570951d-1c5c-4e49-a5b1-d0c255c00e9d\' for key \'PRIMARY\'',
code: 'BadRequestException',
time: 2019-10-01T09:39:07.201Z,
requestId: 'fe03bb4f-eba3-497b-9b3e-59a7f2f529e9',
statusCode: 400,
retryable: false,
retryDelay: 39.156389921388104 }
2019-10-01T09:39:07.207Z cd6a4379-f7ae-1b9c-c083-ffc652a32e70 data null
2019-10-01T09:39:07.853Z cd6a4379-f7ae-1b9c-c083-ffc652a32e70 msg { transactionStatus: 'Transaction Committed' }
2019-10-01T09:39:07.854Z cd6a4379-f7ae-1b9c-c083-ffc652a32e70 orderDataResposne { generatedFields: [], numberOfRecordsUpdated: 1 }
END RequestId: cd6a4379-f7ae-1b9c-c083-ffc652a32e70