[Logo] Terracotta Discussion Forums
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
[Expert]
Delete object in Hibernate is not removed from cache.  XML
Forum Index -> Ehcache
Author Message
boevink

neo

Joined: 10/06/2010 00:10:28
Messages: 8
Offline

If ehcache is used as 2nd level cache for hibernate, objects deleted from the database are not removed from ehcache.

At least that is shown in the Developer Console.
Is there an explanation for this?
gluck

qaestor

Joined: 09/15/2009 18:01:23
Messages: 179
Location: Brisbane, Australia
Offline

Have you got it set up as a read-write cache?
boevink

neo

Joined: 10/06/2010 00:10:28
Messages: 8
Offline

Have you got it set up as a read-write cache? 

Yes I did:

Code:
 <hibernate-configuration>
 	<session-factory>
 		<property name="connection.url">jdbc:postgresql://host:5432/terracotta</property>
 		<property name="connection.username">user</property>
 		<property name="connection.password"></property>
 		<property name="connection.driver_class">org.postgresql.Driver</property>
 		<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
 		<property name="hibernate.hbm2ddl.auto">update</property>
 		<property name="hibernate.show_sql">true</property>
 		
 
 		<property name="hibernate.cache.use_second_level_cache">true</property>
 		<property name="hibernate.cache.region.factory_class">net.sf.ehcache.hibernate.EhCacheRegionFactory</property>
 		<property name="net.sf.ehcache.configurationResourceName">/com/donkey/demo/ehcache.xml</property>
 		<property name="hibernate.generate_statistics">true</property>
 		<property name="hibernate.cache.use_query_cache">true</property>
 						
 <!-- mappings -->
 		<mapping class="com.donkey.demo.model.User"/>
 
 <!-- cache settings -->
 		<class-cache class="com.donkey.demo.model.User" usage="read-write"/>
 	</session-factory>
 </hibernate-configuration>


And my ehcahe file:
Code:
 <cache name="com.donkey.demo.model.User" 
 	maxElementsInMemory="50"
  	maxElementsOnDisk="100"
 	eternal="true" 
 	timeToIdleSeconds="0"
 	timeToLiveSeconds="0"
 	memoryStoreEvictionPolicy="LRU">
 
 	<cacheEventListenerFactory class="net.sf.ehcache.event.TerracottaCacheEventReplicationFactory" />
 	<cacheEventListenerFactory class="com.donkey.demo.dao.ehcache.UserHibernateCacheEventListenerFactory"/> 
 
 	<terracotta clustered="true" valueMode="serialization" />
 </cache>
 
gluck

qaestor

Joined: 09/15/2009 18:01:23
Messages: 179
Location: Brisbane, Australia
Offline

Can you attach the code you are using for the delete along with the version you are using of Ehcache and Terracotta?
boevink

neo

Joined: 10/06/2010 00:10:28
Messages: 8
Offline

We are using a custom install of TerraCotta 3.3.0 with Ehcache 2.2.0.

The actual hibernate implementation is nothing more than:
Code:
 public void delete(T obj) throws ExceptionInInitializerError, HibernateException {
 	Session session = HibernateUtil.getInstance().getSession();
 	Transaction tx = session.beginTransaction();
 	try {
 		session.delete(obj);
 		tx.commit();
 	} catch (HibernateException e) {
 		System.err.println("HibernateException occurred while deleting an object. " + e);
 		tx.rollback();
 		throw e;
 	}
 	finally {
 		session.close();			
 	}
 		
 }
 
fernandopeinado

neo

Joined: 12/02/2010 10:35:58
Messages: 1
Offline

It seems like the ReadWriteEhcacheEntityRegionAccessStrategy does not implement the remove method, that is inherited from the AbstractEhcacheAccessStrategy:

Code:
 abstract class AbstractEhcacheAccessStrategy<T extends EhcacheTransactionalDataRegion> {
 
 ...
 
   /**
    * A no-op since this is an asynchronous cache access strategy.
    *
    * @see org.hibernate.cache.access.EntityRegionAccessStrategy#remove(java.lang.Object)
    * @see org.hibernate.cache.access.CollectionRegionAccessStrategy#remove(java.lang.Object)
    */
   public void remove(Object key) throws CacheException {
   }
 
 ...
 
 }
 


What is weird is that the NonStrictReadWriteEhcacheEntityRegionAccessStrategy have it implemented, and if you use it, it will work, and remove the entity from the cache.

So, try this if the NONSTRICT_READ_WRITE Strategy is possible to you:
Code:
 @Entity
 @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
 public class Entity1 {
 
 	@Id
 	@GeneratedValue
 	private Long id;
 
 	@Column(length = 100)
 	private String name;
 
 	@Version
 	private long versioncontrol;
 
 	public Long getId() {
 		return id;
 	}
 
 	public void setId(Long id) {
 		this.id = id;
 	}
 
 	public String getName() {
 		return name;
 	}
 
 	public void setName(String name) {
 		this.name = name;
 	}
 
 	public long getVersioncontrol() {
 		return versioncontrol;
 	}
 
 	public void setVersioncontrol(long versioncontrol) {
 		this.versioncontrol = versioncontrol;
 	}
 
 	@Override
 	public int hashCode() {
 		final int prime = 31;
 		int result = 1;
 		result = prime * result + ((id == null) ? 0 : id.hashCode());
 		return result;
 	}
 
 	@Override
 	public boolean equals(Object obj) {
 		if (this == obj)
 			return true;
 		if (obj == null)
 			return false;
 		if (getClass() != obj.getClass())
 			return false;
 		Entity1 other = (Entity1) obj;
 		if (id == null) {
 			if (other.id != null)
 				return false;
 		} else if (!id.equals(other.id))
 			return false;
 		return true;
 	}
 
 	@Override
 	public String toString() {
 		return "Entity1: " + id + ":" + name + ":" + versioncontrol;
 	}
 }
 
rajoshi

seraphim

Joined: 07/04/2011 04:36:10
Messages: 1461
Offline

Issue seems to be resolved.Please let us know if more information is required.

Rakesh Joshi
Terracotta.
 
Forum Index -> Ehcache
Go to:   
Powered by JForum 2.1.7 © JForum Team