permaculture playing cards*
The moose likes Object Relational Mapping and the fly likes Toplink Cache removing on delete objects from database manually Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Toplink Cache removing on delete objects from database manually" Watch "Toplink Cache removing on delete objects from database manually" New topic
Author

Toplink Cache removing on delete objects from database manually

allareddy suneel
Greenhorn

Joined: Sep 18, 2012
Posts: 13
We are using Toplink 9.0.3,SoftCacheWeakIdentityMap and JMS cache coordination. Is there any way we can delete the objects from cache when objects manually deleting from DB directly. We were able to succeed for DB manual updates but not for delete.
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
The UnitOfWorkChangeSet has a set of deleted objects that you can register an ObjectChangeSet with to have it deleted in the cluster.

You could also invalidate the object, but this is not available in 9.0.3.


TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
allareddy suneel
Greenhorn

Joined: Sep 18, 2012
Posts: 13
Thanks James for the response.
I have tried and it is not functioning as expected. Basically delete objects will remove the objects from session.removeFromIdentityMap(), we are using SoftCacheWeakIdentityMap as a policy so i am assuming it will not remove the object immediately (though from DB it is deleted the object but not from other applications in which object is in session). Please let me know if my understanding correct.
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
If you broadcast the UnitOfWorkChangeSet with the deleted objects yourself, it should remove them from the cache of all of the other applications.

Perhaps include the code you are using?

Note that since the remove object is removed on the database, the only way to find it on another server would be to query it by Id and get a cache hit.
allareddy suneel
Greenhorn

Joined: Sep 18, 2012
Posts: 13
James,
Please find the code for updates as well as deletes.

public static boolean cacheInvalidate(String entityName, String entityId)
throws Exception {

Class baseTableName = Class.forName(entityName);

Session session = getSession();
UnitOfWork uow = session.acquireUnitOfWork();
Object objectTobeRefreshed = null;
Object objectBackup = null;
Vector primaryKeysList = new Vector();
primaryKeysList.add(entityId);
if (session.containsObjectInIdentityMap(primaryKeysList, baseTableName)) {
objectTobeRefreshed = session.getFromIdentityMap(primaryKeysList,
baseTableName);
}else{
objectTobeRefreshed = findByIdByCacheRefresh(baseTableName,
getPrimaryKey(session, entityName), entityId);
}

if(objectTobeRefreshed==null) return false;

Descriptor descriptor = getDescriptor(session, entityName);
objectBackup = descriptor.getObjectBuilder().instantiateClone(
objectTobeRefreshed,
(oracle.toplink.publicinterface.Session) uow);

objectTobeRefreshed = findByIdByCacheRefresh(baseTableName,
getPrimaryKey(session, entityName), entityId);

UnitOfWorkChangeSet changeSet = new UnitOfWorkChangeSet();
ObjectChangeSet objectChangeSet = null;
if(objectTobeRefreshed==null){//for delete
objectChangeSet = descriptor.getObjectBuilder().createObjectChangeSet(objectBackup, changeSet, false, (oracle.toplink.publicinterface.Session) uow);

changeSet.getDeletedObjects().put(objectChangeSet, objectChangeSet);

}else{//for updates
objectChangeSet = descriptor.getObjectBuilder()
.compareForChange(objectTobeRefreshed, objectBackup, changeSet,
(oracle.toplink.publicinterface.Session) uow);
changeSet.addObjectChangeSet(objectChangeSet, objectTobeRefreshed);
}


if(!changeSet.hasChanges()) return false;

Session severSession = getServerSession();
severSession.getCacheSynchronizationManager().propagateChanges(
changeSet);

return true;

}
--------------------------------------------------------------------------------------------------------------------------
Interesting one is i tried deleting the object from front end UI using following code then also object not removed from not only same session but other applications too. As i mentioned earlier we are using softweakidentitymap for our cache.

public static boolean cacheDeleteObject(String entityName, String entityId)
throws Exception {


Session session = getSession();
UnitOfWork uow = session.acquireUnitOfWork();
Class baseTableName = Class.forName(entityName);

Object objectTobeRefreshed = findByIdByCacheRefresh(baseTableName,
getPrimaryKey(session, entityName), entityId);

if(objectTobeRefreshed==null) return false;

uow.deleteObject(objectTobeRefreshed);
try {
uow.commit();
}
catch(TopLinkException te) {
throw new ResourceException("Exception in cacheDelete()");
}
return true;
}
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
How are you determining the object was not removed?

If you execute a find() using the object's id, do you get anything back?
allareddy suneel
Greenhorn

Joined: Sep 18, 2012
Posts: 13
James,
As i mentioned earlier, we are accessing the object in 2 different applications. We are not using findbyid but we are querying the record using other columns in forms. For admin screen alone, i given entity and pk to delete/update the cache. After i deleted from admin screen, i went to the forms in both applications and checked object coming (though physically record deleted from DB). I am able to see the JMS messages when i deleted the object.
You mean to say though we use Softweakidentiymap, system removes the object immediately from session?
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
If you are not querying by Id, then even if you don't broadcast the deletion you should still not get the object back.
All non-Id queries go to the database, if the object is truly deleted on your database, your should not get it back.

Try enabling logging. Also ensure you are not using a query cache, or caching the query results in your application. Try executing the same SQL, and see if the database returns the object.
 
Don't get me started about those stupid light bulbs.
 
subject: Toplink Cache removing on delete objects from database manually
 
Similar Threads
SpyJMSException
EJB 3 CascadeType annotation. What for it is required exactly.
EJB3 Caching problem (Toplink)
Toplink Cache coordination using JMS for manual DB updates
Manually delete entity bean in memory?