Reusing Entity object

Posts   
 
    
Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 26-Mar-2006 19:18:00   

Frans,

Adapter, v1.0.2004.2 (sorry for using this version, will be using 1.0.2005 soon):

I am processing 5000 records from one table. I read all the records inside a TypedList, then I process each record and create two entities from each record and insert them to the DataBase. I am having memory problems, I would like to reuse the two entities object that I am inserting, that is create only one instance for each entity. I can insert the two entities for the first record; but when I try to insert the entities for the second one I get an error of FKConstrains that the field in entity2 that related it with entity1 can not be null. The two entities are related. The entity1.entity1Pk field is an identity column.

The code is something like this: entity1: entity1Pk is identity column and primary key

entity1.fields.State = Fetched entity1.propertyxxx = yyyyyy entity1.Isnew = True

entity2: entity1Pk is the field that related this entity with entity1

entity2.fields.State = Fetched entity2.entity1 = entity1 entity2.propertyxxx = yyyy entity2.Isnew = True

For the first record it works; but for the second the field entity2.entity1Pk is not being set after the entity1 is saved. I thing the problem is with the collection that entity1 has to keep track of entity2, I will check it.

Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 26-Mar-2006 23:47:41   

Frans,

What I found was that after entity2 is saved(inserted) the first time the framework does not set entity2.entity1Pk at the moment that entity2 is inserted automatically when I insert entity1, no matter if I set the entity2.entity1Pk field to 0 to clear the internal link with entity1 and then set the entity2.entity1 = entity1.

I solved my problem inserting each entity alone: simple_smile 1. Insert entity1 with the refetch option. 2. Set entity2.entity1Pk = entity1.entity1Pk 3. Insert entity2

I could reuse the entity1 and entity2 objects: smile 1. entity1.Isnew = True 2. set all fields of entity1 3. mark all entity1' fields as Changed

I enabled the persistenceInfo object cache and the difference was like day and night.. Wow

Now I can go ahead and try with the real data that has more than 40,000 records.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 27-Mar-2006 12:20:43   

Strange that enabling the cache solves the memory problems... simple_smile Apparently the GC doesn't collect fast enough so the # of memory taken by the objects created each time is higher than the cached info.

Frans Bouma | Lead developer LLBLGen Pro
Rogelio
User
Posts: 221
Joined: 29-Mar-2005
# Posted on: 27-Mar-2006 15:01:53   

Otis wrote:

Strange that enabling the cache solves the memory problems... simple_smile Apparently the GC doesn't collect fast enough so the # of memory taken by the objects created each time is higher than the cached info.

Added to the enabling the cache was the reusing of entity object (instead of creating a new entity for each record I only created one instance of the entities and reused them). Apparently the GC does not work very well when creating a lot of object continiusly.