sequelize transaction - 只能在LoggedIn状态而不是SentClientRequest状态下进行请求

时间:2016-06-03 13:04:19

标签: sequelize.js sequelize-cli

如果我使用Sequelize

进行了以下更新声明
return models.UnitPlant.findOne(
        {
            where:
            {
                id: unitPlantToUpdate.id
            },
            include: [
                { model: models.ProductType },
                { model: models.VehiclePlant, as: 'Customers' }
            ]
        })
        .then(function (unitPlantFromDb) {
            return models
                .sequelize
                .transaction(
                {
                    isolationLevel: models.sequelize.Transaction.ISOLATION_LEVELS.READ_COMMITTED
                },
                function (t) {
                    return unitPlantFromDb.update(unitPlantToUpdate, { transaction: t })
                        .then(function (unitPlantFromDb) {
                            return unitPlantFromDb.setProductType(unitPlant.ProductType.id, { transaction: t })
                        })
                        .then(function (unitPlantFromDb) {
                            return unitPlantFromDb.setCustomers(unitPlant.customerIds, { transaction: t });
                        });
                })
                .then(function (result) {
                    return output.getSuccessResult(titles.SUCCESS, validationMessages.SUCCESS_ON_UPDATE, UNIT_PLANT)
                })
                .catch(function (error) {
                    console.log(error);
                    return output.getErrorResult(titles.ERROR_ON_UPDATE, validationMessages.ERROR_ON_UPDATE, error.message);
                });
        });

注意到UnitplantCustomer之间存在一对多关系。 上面的代码工作正常,但只有 - 更新时 - 我只能删除或仅插入customers

每当两者都发生时,我都会收到以下错误:

未处理拒绝SequelizeDatabaseError:请求只能在LoggedIn状态下进行,而不能在SentClientRequest状态下进行

从控制台出现这种情况:

Executing (783e1de2-5013-43b9-80e9-248dca6d83e1): BEGIN TRANSACTION;
Executing (783e1de2-5013-43b9-80e9-248dca6d83e1): SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
Executing (783e1de2-5013-43b9-80e9-248dca6d83e1): UPDATE [UnitPlants] SET [expiresAt]='2016-06-22',[updatedAt]='2016-06-03 12:57:50.000 +00:00' OUTPUT INSERTED.* WHERE [id] = 15
Executing (783e1de2-5013-43b9-80e9-248dca6d83e1): UPDATE [UnitPlants] SET [ProductTypeId]=1,[updatedAt]='2016-06-03 12:57:50.000 +00:00' OUTPUT INSERTED.* WHERE [id] = 15
Executing (783e1de2-5013-43b9-80e9-248dca6d83e1): SELECT [VehiclePlant].[id], [VehiclePlant].[code], [VehiclePlant].[name], [VehiclePlant].[expiresAt], [VehiclePlant].[createdAt], [VehiclePlant].[upoCustomer], [VehiclePlant].[isExport], [VehiclePlant].[updatedAt], [UnitPlantCustomers].[createdAt] AS [UnitPlantCustomers.createdAt], [UnitPlantCustomers].[updatedAt] AS [UnitPlantCustomers.updatedAt], [UnitPlantCustomers].[UnitPlantId] AS [UnitPlantCustomers.UnitPlantId], [UnitPlantCustomers].[VehiclePlantId] AS [UnitPlantCustomers.VehiclePlantId] FROM [VehiclePlants] AS [VehiclePlant] INNER JOIN [UnitPlantCustomers] AS [UnitPlantCustomers] ON [VehiclePlant].[id] = [UnitPlantCustomers].[VehiclePlantId] AND [UnitPlantCustomers].[UnitPlantId] = 15;
Executing (783e1de2-5013-43b9-80e9-248dca6d83e1): DELETE FROM [UnitPlantCustomers] WHERE [UnitPlantId] = 15 AND [VehiclePlantId] IN (4); SELECT @@ROWCOUNT AS AFFECTEDROWS;
Executing (783e1de2-5013-43b9-80e9-248dca6d83e1): INSERT INTO [UnitPlantCustomers] ([createdAt],[updatedAt],[UnitPlantId],[VehiclePlantId]) VALUES ('2016-06-03 12:57:50.000 +00:00','2016-06-03 12:57:50.000 +00:00',15,3);
Executing (783e1de2-5013-43b9-80e9-248dca6d83e1): ROLLBACK TRANSACTION;

请注意 INSERT DELETE

为什么只有当我同时拥有时才会发生这种情况?我该如何解决这个问题?

作为旁注,当我使用该事务时,整个事情仍然有效,这让我想知道我是否选择了错误的隔离级别,或者我的整个交易设置是错误的。

1 个答案:

答案 0 :(得分:1)

默认繁琐(节点mssql驱动程序)仅支持一个并发请求,而其他db libs自动管理查询排队。应该在续集版本3.22中修复

https://github.com/sequelize/sequelize/pull/5752