Current filter:
                                You should refresh the page.
                                  • Hello Dear XPO Team,

                                    I have an application built on XAF and using XPO for DB operations.

                                    I need your help for complete inserting small large data ( about 500 - 1000 record ) with using System.Threading.Tasks library and Parallel loop.

                                    Attached an application about what I trying to accomplish. Let me detaill, I have one BO named Product and one controller named MultiThreadTestController.

                                    The controller has MultiThreadTestAction action that showing on top of Product listview.

                                    I need to complete bulk operation.

                                    I know below version of parallel loop is working but I need to insert all or not, I couldn't commit record one by one.

                                    [C#]
                                    try { Parallel.For(0, 100, i => { using (XPObjectSpace controllerOS = (XPObjectSpace)Application.CreateObjectSpace()) { var foundedProduct = controllerOS.Session.FindObject<Product>(new BinaryOperator("Oid", i)); if (foundedProduct != null) { Tracing.Tracer.LogText(string.Format("{0} {1}", foundedProduct.Oid, foundedProduct.Name)); } else { Tracing.Tracer.LogText(string.Format("Product : {0} not found.", i)); Product p = new Product(controllerOS.Session); p.Name = string.Format("{0}. Product", i); } controllerOS.CommitChanges(); } }); } catch (Exception ex) { throw; }

                                    Thanks for your help.

                                    Have a nice day.

                                Show all comments
                                • Kalem Yazılım 10.18.2019

                                  Additional Image.

                                • Kalem Yazılım 10.21.2019

                                  Additional Info : I tried CreateNestedObjectSpace() method in loop for committing object one by one to Parent Object Space but no success.

                                • Manuel Grundner [DevExpress MVP] 10.21.2019

                                  Sessions are not threadsafe. So make sure you don't mix up threads when working with PersistentObjects. To use multiple threads you need to make sure to use an ThreadSafeDataLayer when working in Winforms with multiple threads.

                                  [C#]
                                  new XPObjectSpaceProvider(XPObjectSpaceProvider.GetDataStoreProvider(args.ConnectionString, args.Connection, true), true) //the last bool flag indicates to use an thread safe datalayer

                                  Hope this helps and kind regards
                                  Manuel

                                • Kalem Yazılım 10.21.2019

                                  Hello Manuel,

                                  Did you researched my attached solution, I already changed this flag to true in CustomObjectSpaceProvider event ? Please some attention

                                  [C#]
                                  protected override void CreateDefaultObjectSpaceProvider(CreateCustomObjectSpaceProviderEventArgs args) { args.ObjectSpaceProviders.Add(new XPObjectSpaceProvider(XPObjectSpaceProvider.GetDataStoreProvider(args.ConnectionString, args.Connection, true), true)); args.ObjectSpaceProviders.Add(new NonPersistentObjectSpaceProvider(TypesInfo, null)); }
                                • Kalem Yazılım 10.21.2019

                                  * CreateDefaultObjectSpaceProvider event.

                                • Kalem Yazılım 10.21.2019

                                  Hello again Manuel,

                                  I thought you're working for DevExpress Team. Of course you can share your kindly help with me regardless looking my attachment :)

                                  Have you tried working with multithread operations ?

                                  I have problem about creating new records in parallel library methods.

                                • Manuel Grundner [DevExpress MVP] 10.21.2019

                                  Hi Kalem!

                                  No I am an individual consulting company and a DevExpress MVP.
                                  I did not check your sample so far cause I wrote my comment on my mobile .
                                  I'll check it later this day and let you know.

                                  > Have you tried working with multithread operations ?
                                  I did in serval projects, so yes.

                                • Manuel Grundner [DevExpress MVP] 10.21.2019
                                  [C#]
                                  Parallel.For(0, 100, i => { try { using (XPObjectSpace controllerOS = (XPObjectSpace)Application.CreateObjectSpace()) { var foundedProduct = controllerOS.Session.FindObject<Product>(new BinaryOperator("Oid", i)); if (foundedProduct != null) { Tracing.Tracer.LogText(string.Format("{0} {1}", foundedProduct.Oid, foundedProduct.Name)); } else { Tracing.Tracer.LogText(string.Format("Product : {0} not found.", i)); Product p = new Product(controllerOS.Session); p.Name = string.Format("{0}. Product", i); } controllerOS.CommitChanges(); } } catch (Exception ex) { throw; } });

                                  Should do the trick. You can't use the same session in different threads. The action get's called from multiple threads.

                                • Kalem Yazılım 10.21.2019

                                  Hello again Manuel,
                                  I already noted that,
                                  I know below version of parallel loop is working but I need to insert all or not, I couldn't commit record one by one.

                                  [C#]
                                  try { Parallel.For(0, 100, i => { using (XPObjectSpace controllerOS = (XPObjectSpace)Application.CreateObjectSpace()) { var foundedProduct = controllerOS.Session.FindObject<Product>(new BinaryOperator("Oid", i)); if (foundedProduct != null) { Tracing.Tracer.LogText(string.Format("{0} {1}", foundedProduct.Oid, foundedProduct.Name)); } else { Tracing.Tracer.LogText(string.Format("Product : {0} not found.", i)); Product p = new Product(controllerOS.Session); p.Name = string.Format("{0}. Product", i); } controllerOS.CommitChanges(); } }); } catch (Exception ex) { throw; }

                                  As a result, I need a way to multithread and commit all or not

                                • Manuel Grundner [DevExpress MVP] 10.21.2019

                                  > As a result, I need a way to multithread and commit all or not

                                  You can't share a session or objectspace between threads, so this is not an option.

                                  > I know below version of parallel loop is working but I need to insert all or not, I couldn't commit record one by one.

                                  Please describe your business problem in more detail. Probably there is a better solution (batching together the changes, or do the detection of records in parallel, and commiting in a single phase. I'm not sure what your ultimate goal is.

                                • Kalem Yazılım 10.21.2019

                                  Of course,

                                  Let me detail in real time scenario,

                                  I have an application is running for Retail. An operation is doing that reading all sales from POS devices and writing to XAF DB.

                                  Approximately process volume is 1000-5000 line of single document . After that some transformation operations appliying the document and creating new other single document with 2000-10000 of count.

                                  So I couldn't divide this operations to little.

                                  What you reccomend for this process?

                                  Have a nice day.

                                1 Solution

                                Creation Date Importance Sort by

                                Hello Kalem,

                                I am afraid that these requirements cannot be fulfilled together - you cannot commit data from multiple threads and be able to rollback all changes at the same time. As an alternative, you can perform the insert operation in a single background thread that doesn't block the UI. Please refer to the corresponding article: How do I execute a long background task or asynchronous process using an Action?.
                                Alternatively, implement this operation without XPO using database-specific capabilities (for example, using BULK INSERT).