Hello
I have a jar with some EJB 3.0 beans (no hibernate specific annotation) wich are used by serveral projects.
I want to use them now with Hibernate and EHCache inside an Appserver, without a large configuration overhead.
So I choose to use only the persistence.xml and ehcache.xml.
It is possible to use the property hibernate.ejb.classcache... to enable read-only cache, as I dont want to write to these beans.
However, Hibernate still want to write to these objects, and I get the exception:
Code:
java.lang.UnsupportedOperationException: Can't write to a readonly object
at net.sf.ehcache.hibernate.strategy.ReadOnlyEhcacheCollectionRegionAccessStrategy.lockItem(ReadOnlyEhcacheCollectionRegionAccessStrategy.java:74)
at org.hibernate.action.CollectionAction.beforeExecutions(CollectionAction.java:88)
at org.hibernate.engine.ActionQueue.prepareActions(ActionQueue.java:285)
at org.hibernate.engine.ActionQueue.prepareActions(ActionQueue.java:194)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:320)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375)
at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:1001
1)How can this problem be resolved?
2)I have seen that the properties mutable="false", or flushmode="MANUAL" could help. However, It is not possible to define these in the persistence.xml. I would have to create a hbm.xml file, but then I could also write the read-only cache setting in there. Which means that the possibility to configure read-only inside persistence.xml is useless here.
My persistence.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="myPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/PostgresDS</jta-data-source>
<jar-file>BEANS.jar</jar-file>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.cache.region.factory_class" value="net.sf.ehcache.hibernate.EhCacheRegionFactory" />
<property name="hibernate.cache.provider_class" value="net.sf.ehcache.hibernate.EhCacheProvider" />
<property name="net.sf.ehcache.configurationResourceName" value="META-INF/ehcache.xml" />
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.ejb.classcache.entityBeans.BEAN1" value="read-only"/>
...
</properties>
</persistence-unit>
</persistence>
My ehcache.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="true"
monitoring="autodetect"
dynamicConfig="true">
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="1000"
maxElementsOnDisk="1"
eternal="false"
overflowToDisk="false"
timeToIdleSeconds="60"
timeToLiveSeconds="120"
memoryStoreEvictionPolicy="LRU"/>
<cache name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="10"
eternal="false"
timeToLiveSeconds="120"
overflowToDisk="false"/>
<cache name="org.hibernate.cache.UpdateTimestampsCache"
maxElementsInMemory="5000"
eternal="true"
overflowToDisk="false"/>
</ehcache>