-
1. Re: Configuring JBoss cache for EJB3 entities (JBoss-5.0.0.GA)
piotrekde May 22, 2011 8:06 AM (in response to piotrekde)It was my fault... I tried to use cache from JBoss server configuration without cache available (default). In order to do not run whole 'all' server configuration, I've copied following fies/directories from 'all' to 'default':
deploy/cluster
lib/jgroups.jar
lib/hibernate-jbosscache2.jar
lib/jbosscache-core.jar
lib/jbosscache-pojo.jar
deploy-hasinleton
Now server starts without errors, however when I take a look at hibernate sql logs I can still see the same number of queries hitting database as without cache configured. Here is my persistence.xml file:
<persistence 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" version="1.0"> <persistence-unit name="mydb"> <jta-data-source>java:/myds</jta-data-source> (...) classes definitions <class>com.my.class.MyEntityOne</class> (...) <properties> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> <property name="hibernate.format_sql" value="false" /> <property name="hibernate.hbm2ddl.auto" value="update"/> <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/> <property name="hibernate.transaction.factory_class" value="org.hibernate.ejb.transaction.JoinableCMTTransactionFactory"/> <!-- 2nd level cache --> <property name="hibernate.cache.use_second_level_cache" value="true"/> <property name="hibernate.cache.use_query_cache" value="true"/> <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.jbc2.JndiMultiplexedJBossCacheRegionFactory"/> <property name="hibernate.cache.region.jbc2.cachefactory" value="java:CacheManager"/> <property name="hibernate.cache.region.jbc2.cfg.entity" value="mvcc-entity"/> <property name="hibernate.cache.region.jbc2.cfg.query" value="local-query"/> </properties> </persistence-unit> </persistence>
Also, all my entities are annotated with:
@Cache (usage=CacheConcurrencyStrategy.TRANSACTIONAL)
Should I do some additional setup?
Maybe a little hint will be that when I shut down server the following exception can be seen in logs:
org.hibernate.cache.CacheException: java.lang.IllegalStateException: Cache not in STARTED state!
at org.hibernate.cache.jbc2.BasicRegionAdapter.destroy(BasicRegionAdapter.java:243)
at org.hibernate.impl.SessionFactoryImpl.close(SessionFactoryImpl.java:813)
at org.hibernate.ejb.EntityManagerFactoryImpl.close(EntityManagerFactoryImpl.java:46)
at org.jboss.jpa.deployment.ManagedEntityManagerFactory.destroy(ManagedEntityManagerFactory.java:93)
at org.jboss.jpa.deployment.PersistenceUnitDeployment.stop(PersistenceUnitDeployment.java:343)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
Thanks,
Piotr
-
2. Re: Configuring JBoss cache for EJB3 entities (JBoss-5.0.0.GA)
piotrekde May 26, 2011 2:36 AM (in response to piotrekde)It seems that query caching and entity caching are quite distinct.
Queries has to explicitly state that they want to be cached:
Query query = manager.createQuery("SELECT i FROM .."); query.setHint("org.hibernate.cacheable", true);
For entities, in my case (I don't know if it's a general rule) it was not enough to annotate classes with @Cache at the top, but also include this annotation on every field that refers to other cached entity, for example:
@Cache (usage=CacheConcurrencyStrategy.TRANSACTIONAL) @Entity public class EntityA { @Cache (usage=CacheConcurrencyStrategy.TRANSACTIONAL) private Set<EntityB> entitiesB; } @Cache (usage=CacheConcurrencyStrategy.TRANSACTIONAL) @Entity public class EntityB { }
As I mentioned, it may be not a general rule about caching entities, but in my case, after I've added these annotations on fields-level everything started to work as expected.
-
3. Re: Configuring JBoss cache for EJB3 entities (JBoss-5.0.0.GA)
jaikiran May 26, 2011 2:46 AM (in response to piotrekde)1 of 1 people found this helpfulpiotrekde wrote:
It seems that query caching and entity caching are quite distinct.
Yes, they are different things. This is a good read http://www.javalobby.org/java/forums/t48846.html to understand what each one does.