Tuesday, June 2, 2009

Update values of a variable in a Script Task or Script Component

Recently I tried updating value of a variable inside a Script Task, but application was hanging. There was no response from the system when ever the script task tried to update value of the variable.The code which I was using to update the variable was

Public Sub Main()

Dim vars As Variables

Dts.VariableDispenser.LockOneForWrite("nCounter", vars)

vars("nCounter").Value = 1980
Dts.TaskResult = Dts.Results.Success
End Sub


When the code is debugged using a break point, we noticed application is hanging while trying to execute the line Dts.VariableDispenser.LockOneForWrite("nCounter", vars). After analyzing some time we identified the problem as a deadlock caused by execution of the above statement. A deadlock is created by the above statement as we have mentioned the variable nCounter
name in
ReadWriteVariables property of Script Task. When a variable name is mentioned in ReadWriteVariables property, SSIS automatically locks the variable for writing. So if you would like maintain locking of the variables in your script then don’t mention the variable name in ReadWriteVariables property.

For simplicity if you would make use of scripting task locking mechanism then use the following the code to update values of the variables

Public Sub Main()

Dts.Variables("nCounter").Value = 100

Dts.TaskResult = Dts.Results.Success

End Sub

No comments:

Post a Comment