Does SqlCommand.Clone () create a deep copy or a shallow copy?

Does SqlCommand.Clone() create a deep copy or a shallow copy? In addition, is it safe to call Clone() from several threads at the same time (create one command that several threads can copy, set parameter values ​​and execute)?

+6
source share
2 answers

You cannot call Clone from multiple threads, because the SqlCommand class itself is not a thread safe class. you must lock before cloning.

However, you can look at the SqlCommand.Clone() method using programs like Reflector , here is the actual code:

 public SqlCommand Clone() { SqlCommand command = new SqlCommand(this); Bid.Trace("<sc.SqlCommand.Clone|API> %d#, clone=%d#\n", this.ObjectID, command.ObjectID); return command; } internal static void Trace(string fmtPrintfW, int a1, int a2) { if (((modFlags & ApiGroup.Trace) != ApiGroup.Off) && (modID != NoData)) { NativeMethods.Trace(modID, UIntPtr.Zero, UIntPtr.Zero, fmtPrintfW, a1, a2); } } [DllImport("System.Data.dll", EntryPoint="DllBidTraceCW", CallingConvention=CallingConvention.Cdecl, CharSet=CharSet.Unicode)] internal static extern void Trace(IntPtr hID, UIntPtr src, UIntPtr info, string fmtPrintfW, int a1, int a2); private SqlCommand(SqlCommand from) : this() { this.CommandText = from.CommandText; this.CommandTimeout = from.CommandTimeout; this.CommandType = from.CommandType; this.Connection = from.Connection; this.DesignTimeVisible = from.DesignTimeVisible; this.Transaction = from.Transaction; this.UpdatedRowSource = from.UpdatedRowSource; SqlParameterCollection parameters = this.Parameters; foreach (object obj2 in from.Parameters) { parameters.Add((obj2 is ICloneable) ? (obj2 as ICloneable).Clone() : obj2); } } 

You can see that it creates a new instance and adds all the properties of the old one to it, but it does not copy all the properties of β€œ Connection , for example,” which means it is a shallow copy.

+2
source

The SqlCommand.Clone method executes a shallow copy. Any properties that are a reference type will represent the same object in both SqlCommand instances. Thus, an unsafe thread.

AFAIK, all Clone () methods (MemberwiseClone) in the .NET Framework are shallow copies.

You did not post your code, but I would suggest creating a new SqlCommand , not cloning.

+2
source

Source: https://habr.com/ru/post/893549/


All Articles