Parallel.For并行录入10万条数据出错

用了一个CodeTimer测试性能,在循环方法体的时候,老大说让用Parallel.For代替for循环。

结果瞬间快了100倍的测试速度,但是又出现一个问题了,数据录不进去了。

经常循环10万次才录入一条,断点调试后发现就第一条录进去了,后面的都是数据库连接池错误。

我想问下用并行之后,这种情况是正常的吗,如果不正常,有什么方法解决呢

代码中的Time()的三个参数,第一个是方法名(无视),第二个是循环次数,第三个是方法体,关于Time()方法我也列出来,详情可以看http://blog.zhaojie.me/2009/03/codetimer.html

 Time("注册性能", 100, () =>
            {
                try
                {
                    User user = new User() { UserName = Guid.NewGuid().ToString(), Password = "123" };
                    ips.Register(user);
                }
                catch(Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            });

图片说明

Timer方法体

 public static void Initialize()
        {
            Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.High;
            Thread.CurrentThread.Priority = ThreadPriority.Highest;
            Time("", 1, () => { });
        }

        public static void Time(string name, int iteration, Action action)
        {
            if (String.IsNullOrEmpty(name)) return;

            // 1.
            ConsoleColor currentForeColor = Console.ForegroundColor;
            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.WriteLine(name);

            // 2.
            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
            int[] gcCounts = new int[GC.MaxGeneration + 1];
            for (int i = 0; i <= GC.MaxGeneration; i++)
            {
                gcCounts[i] = GC.CollectionCount(i);
            }

            // 3.
            Stopwatch watch = new Stopwatch();
            watch.Start();
            ulong cycleCount = GetCycleCount();
            //for (int i = 0; i < iteration; i++) action();
            Parallel.For(0, iteration, (i) => { action(); });
            ulong cpuCycles = GetCycleCount() - cycleCount;
            watch.Stop();

            // 4.
            Console.ForegroundColor = currentForeColor;
            Console.WriteLine("\tTime Elapsed:\t" + watch.ElapsedMilliseconds.ToString("N0") + "ms");
            Console.WriteLine("\tCPU Cycles:\t" + cpuCycles.ToString("N0"));

            // 5.
            for (int i = 0; i <= GC.MaxGeneration; i++)
            {
                int count = GC.CollectionCount(i) - gcCounts[i];
                Console.WriteLine("\tGen " + i + ": \t\t" + count);
            }

            Console.WriteLine();
        }

        private static ulong GetCycleCount()
        {
            ulong cycleCount = 0;
            QueryThreadCycleTime(GetCurrentThread(), ref cycleCount);
            return cycleCount;
        }

        [DllImport("kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool QueryThreadCycleTime(IntPtr threadHandle, ref ulong cycleTime);

        [DllImport("kernel32.dll")]
        static extern IntPtr GetCurrentThread();

可能你的连接池用完了,因为你用的并行,获取连接池的时候数据库的处理较慢,连接释放的慢,导致连接池里的连接不够用。