sequelize transaction - 请求只能在 LoggedIn 状态下进行,不能在 SentClientRequest 状态下进行
sequelize transaction - Requests can only be made in the LoggedIn state, not the SentClientRequest state
前提是我有以下使用 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);
});
});
注意到我在 Unitplant
和 Customer
上有一个一对多的关系。
上面的代码工作正常,但只要 - 更新时 - 我要么只 delete 要么只 insert 到 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。
为什么只有当我同时拥有两者时才会出现这种情况?我该如何解决这个问题?
作为旁注,当我 不 使用事务时,整个事情都有效,这让我想知道我是否选择了错误的隔离级别,或者我的整个事务设置是只是错了。
默认情况下繁琐(节点 mssql 驱动程序)仅支持一个并发请求,而其他数据库库自动管理查询队列。应该在 sequelize 版本 3.22
中修复
前提是我有以下使用 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);
});
});
注意到我在 Unitplant
和 Customer
上有一个一对多的关系。
上面的代码工作正常,但只要 - 更新时 - 我要么只 delete 要么只 insert 到 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。
为什么只有当我同时拥有两者时才会出现这种情况?我该如何解决这个问题?
作为旁注,当我 不 使用事务时,整个事情都有效,这让我想知道我是否选择了错误的隔离级别,或者我的整个事务设置是只是错了。
默认情况下繁琐(节点 mssql 驱动程序)仅支持一个并发请求,而其他数据库库自动管理查询队列。应该在 sequelize 版本 3.22
中修复