« Return to Thread: Inserts and Caching

Re: Inserts and Caching

by James Sutherland :: Rate this Message:

Reply to Author | View in Thread

Are you concerned with caching across transaction or within the same transaction?

Across transaction you can disable caching using the persistence.xml property,
"eclipselink.cache.shared.default"="false".  Or you can set the size or type of the cache to control how many objects are cached.

For avoiding caching within a single transaction, the best way is to flush() then clear() the EntityManager.  You could do this for each object, or probably better for each batch of objects.

Also ensure you application does not hold onto the objects, otherwise they will not be able to gc.


ossaert wrote:
Hi,

I have the following design problem. I have in EJB a remote interface which actually is a projection of my use-case. This remote method calls different local apis.

Important to know is that the use-case is a unit. Or the method is committed or rolled back completely.

Now, during the execution of this method I have a call to a method which insert event-objects into the database. Unfortunately, these event objects remain in memory until my transaction commits. Is there a way to avoid this (without having to use the SQL INSERT statement directly)? I found in EclipseLink the method InsertObjectQuery.

public void sendEvent(NodeEntity nodeEntity, EventType eventType) {

        EventEntity event = new EventEntity();
        event.setNodeGuid(.....);
        event.setEventType(....);

        /* This is the EclipseLink specific part */        
        InsertObjectQuery writeQuery = new InsertObjectQuery();
        writeQuery.setObject(event);
        writeQuery.dontMaintainCache();

        /* Convert the query to JPA query */
        Query q = JpaHelper.createQuery(writeQuery, this.getEntityManager());
        q.executeUpdate();
}

This method, I hope, should be able NOT TO CACHE the EventEntity? So, when I have let's say 5000 eventEntities in my method, they will not remain in memory and will not cause a heap space problem?

Unfortunately, this method does not function within JPA (Glassfish). I get the following error:
Objects cannot be written during a UnitOfWork, they must be registered.

I tried to register it (using entitymanager.getUnitOfWork().registerNewObject() etc), but without success.

Are my assumptions correct and/or is there a method to do insert using the API in JPA (so I can refactor it easily) ? Now, as a work-around I use the SQL INSERT statement directly, but that is not always portable.

Actually, I would be nice if you could remove one object from the cache or to prevent the object from being cached. Sometimes, you have objects you must write to the database use JPA (annotated with @PrePersist etc) which you don't need afterwards.

Does the annotations @Cache(type=CacheType.NONE) actually function when I execute the normal "entitymanager.persist(eventEntity)"?

Greetings
Jan

 « Return to Thread: Inserts and Caching