Quantcast
Channel: DevExpress Support Center (Examples)
Viewing all articles
Browse latest Browse all 7205

How to import a large data set using XPO efficiently within a transaction

$
0
0
When you are required to import a large data set into a database as XPO persistent objects, the straightforward approach might be inappropriate. Specifically, if you would create objects objects one by one and commit them individually, you cannot roll back changes if one object failed to commit. If you use an XPO transaction or unit of work, changes can be rolled back, but it will require a lot of memory to keep all objects until the final commit.

The solution demonstrated in this example commits objects in small batches by creating a unit of work for each batch and disposing of it after it is committed. To be able to roll back all batches at once, it utilizes the database-level transaction using the XPO data layer's command channel. Although XPO provides a public API for using database transactions (Using Explicit Transactions), it cannot be used in this scenario because explicit transactions belong to sessions, but here we use separate sessions for each batch.

Below is a managed memory allocation chart produced by this example if you log GC.GetTotalMemory(true) values in the CreatePersistentObject method:


Question Comments

Added By: Casey Chester 2 at: 1/14/2016 7:30:31 AM    

Nice!

Added By: Saif Khan at: 5/30/2016 3:40:56 AM    How can this be achieved with nested UOWs? Example a parent record (Invoice) with one row and a child (InvoiceDetails) with 4k rows - I have a similar situation and borrowing from the sample below, my result is taking a long time to insert the details to the DB while plain old ADO.NET is much faster.

It was suggested that I also "recreate the parent Unit of Work after committing changes to reset its cache...", but I don't see how that's possible when i am using the parent object to pass to the child in order to create the relationship.

Viewing all articles
Browse latest Browse all 7205

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>