File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JBoss/WildFly and the fly likes JBOSS CACHE second level caching for named query cache Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "JBOSS CACHE second level caching for named query cache" Watch "JBOSS CACHE second level caching for named query cache" New topic
Author

JBOSS CACHE second level caching for named query cache

ambika mullasseri
Greenhorn

Joined: Jun 01, 2009
Posts: 28
hi,

i have an application jboss cache with hibernate as JPA provider running in jboss AS.and have done entity caching successfully.but when i configured the same for named query, it is not working.
my persistance.xml has <property name="hibernate.cache.use_query_cache" value="true"/> and <property name="hibernate.cache.region.jbc2.cfg.query" value="local-query"/>

my entity class has @NamedQuery(name="allSamples",query="from Sample",hints={
@QueryHint(name="org.hibernate.cacheable",value="true")})})

from dao i am trying to access this query.but it doesnt fetch from cache.it always fetches from DB. please help....


SCJP 6.0 86%
Francesco Marchioni
author
Ranch Hand

Joined: Sep 22, 2003
Posts: 190
ambika mullasseri wrote:hi,

i have an application jboss cache with hibernate as JPA provider running in jboss AS.and have done entity caching successfully.but when i configured the same for named query, it is not working.
my persistance.xml has <property name="hibernate.cache.use_query_cache" value="true"/> and <property name="hibernate.cache.region.jbc2.cfg.query" value="local-query"/>

my entity class has @NamedQuery(name="allSamples",query="from Sample",hints={
@QueryHint(name="org.hibernate.cacheable",value="true")})})

from dao i am trying to access this query.but it doesnt fetch from cache.it always fetches from DB. please help....


Hi Ambika,
post the complete hibernate section of persistence.xml. Also, if you perform a listing of Cache regions, does the Query cache appears as activated ?
regards
Franceco


WildFly 8 Administration Book - JBoss Tutorials
ambika mullasseri
Greenhorn

Joined: Jun 01, 2009
Posts: 28
Please find the complete persistance xml

<?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="mycache" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/mycache</jta-data-source>
<class>com.Sample</class>
<properties>
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class, hbm"/>

<!-- SQL stdout logging -->
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="use_sql_comments" value="true"/>


<property name="hibernate.connection.driver_class"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="hibernate.connection.url"
value="jdbc:sqlserver://localhost:1433;databaseName=xxx"/>
<property name="hibernate.connection.username"
value="xxx"/>
<property name="hibernate.connection.password"
value="xxx"/>
<property name="hibernate.c3p0.min_size"
value="5"/>
<property name="hibernate.c3p0.max_size"
value="20"/>
<property name="hibernate.c3p0.timeout"
value="300"/>
<property name="hibernate.c3p0.max_statements"
value="50"/>
<property name="hibernate.c3p0.idle_test_period"
value="3000"/>
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory"></property>

<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"></property>

<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"/>
<property name="hibernate.cache.region_prefix" value="tempdb"/>
</properties>
</persistence-unit>
</persistence>


Also, if you perform a listing of Cache regions, does the Query cache appears as activated ?

how do i list it?

my server log says'

:28:39,688 INFO [ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
11:28:39,688 INFO [SettingsFactory] Query language substitutions: {}
11:28:39,688 INFO [SettingsFactory] JPA-QL strict compliance: enabled
11:28:39,688 INFO [SettingsFactory] Second-level cache: enabled
11:28:39,688 INFO [SettingsFactory] Query cache: enabled
11:28:39,688 INFO [SettingsFactory] Cache region factory : org.hibernate.cache.jbc2.JndiMultiplexedJBossCacheRegionFactory
11:28:39,688 INFO [SettingsFactory] Optimize cache for minimal puts: enabled
11:28:39,688 INFO [SettingsFactory] Cache region prefix: tempdb
11:28:39,688 INFO [SettingsFactory] Structured second-level cache entries: disabled
11:28:39,688 INFO [SettingsFactory] Query cache factory: org.hibernate.cache.StandardQueryCacheFactory
11:28:39,688 INFO [SettingsFactory] Echoing all SQL to stdout
11:28:39,688 INFO [SettingsFactory] Statistics: disabled
11:28:39,688 INFO [SettingsFactory] Deleted entity synthetic identifier rollback: disabled
11:28:39,688 INFO [SettingsFactory] Default entity-mode: pojo
11:28:39,688 INFO [SettingsFactory] Named query checking : enabled
11:28:39,704 INFO [SessionFactoryImpl] building session factory
11:28:39,704 INFO [NamingHelper] JNDI InitialContext properties:{}
11:28:39,704 INFO [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
11:28:39,704 INFO [NamingHelper] JNDI InitialContext properties:{}
11:28:39,704 INFO [UpdateTimestampsCache] starting update timestamps cache at region: tempdb.org.hibernate.cache.UpdateTimestampsCache
11:28:39,704 INFO [StandardQueryCache] starting query cache at region: tempdb.org.hibernate.cache.StandardQueryCache
11:28:39,720 WARN [AbstractEntityManagerImpl] Cannot join transaction: do not override hibernate.transaction.factory_class
11:28:39,720 INFO [STDOUT] #########################################1267163919720
11:28:39,766 INFO [STDOUT] Hibernate:
select
sample0_.id as id10_,
sample0_.name as name10_
from
sample sample0_
11:28:39,782 INFO [STDOUT] 1
11:28:39,782 INFO [STDOUT] Lala Lajpat Rai
11:28:39,782 INFO [STDOUT] 2
11:28:39,782 INFO [STDOUT] Dr. S Radhakrishnan
11:28:39,782 INFO [STDOUT] 3
11:28:39,782 INFO [STDOUT] Ustad Zakir Hussain
11:28:39,782 INFO [STDOUT] 4
11:28:39,782 INFO [STDOUT] Sardar Vallabhbhai Patel
11:28:39,782 INFO [STDOUT] 5
11:28:39,782 INFO [STDOUT] Gautam Buddha
11:28:39,782 INFO [STDOUT] #########################################1267163919782
11:28:39,813 WARN [Ejb3Configuration] Overriding hibernate.transaction.factory_class is dangerous, this might break the EJB3 specification implementation
11:28:39,813 INFO [AnnotationBinder] Binding entity from annotated class: com.Sample
11:28:39,813 INFO [QueryBinder] Binding Named query: allSamples => from Sample
11:28:39,813 INFO [EntityBinder] Bind entity com.Sample on table sample
11:28:39,860 INFO [HibernateSearchEventListenerRegister] Unable to find org.hibernate.search.event.FullTextIndexEventListener on the classpath. Hibernate Search is not enabled.
11:28:39,860 INFO [NamingHelper] JNDI InitialContext properties:{}
11:28:39,860 INFO [DatasourceConnectionProvider] Using datasource: java:/mycache
11:28:39,860 INFO [SettingsFactory] RDBMS: Microsoft SQL Server, version: 10.00.1600
11:28:39,860 INFO [SettingsFactory] JDBC driver: Microsoft SQL Server JDBC Driver 2.0, version: 2.0.1803.100
11:28:39,860 INFO [Dialect] Using dialect: org.hibernate.dialect.SQLServerDialect
11:28:39,860 INFO [TransactionFactoryFactory] Transaction strategy: org.hibernate.transaction.JTATransactionFactory
11:28:39,860 INFO [NamingHelper] JNDI InitialContext properties:{}
11:28:39,860 INFO [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
11:28:39,860 INFO [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
11:28:39,860 INFO [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
11:28:39,860 INFO [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
11:28:39,860 INFO [SettingsFactory] Automatic flush during beforeCompletion(): disabled
11:28:39,860 INFO [SettingsFactory] Automatic session close at end of transaction: disabled
11:28:39,860 INFO [SettingsFactory] Scrollable result sets: enabled
11:28:39,860 INFO [SettingsFactory] JDBC3 getGeneratedKeys(): enabled
11:28:39,860 INFO [SettingsFactory] Connection release mode: auto
11:28:39,860 INFO [SettingsFactory] Default batch fetch size: 1
11:28:39,860 INFO [SettingsFactory] Generate SQL with comments: disabled
11:28:39,860 INFO [SettingsFactory] Order SQL updates by primary key: disabled
11:28:39,860 INFO [SettingsFactory] Order SQL inserts for batching: disabled
11:28:39,860 INFO [SettingsFactory] Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
11:28:39,860 INFO [ASTQueryTranslatorFactory] Using ASTQueryTranslatorFactory
11:28:39,860 INFO [SettingsFactory] Query language substitutions: {}
11:28:39,860 INFO [SettingsFactory] JPA-QL strict compliance: enabled
11:28:39,860 INFO [SettingsFactory] Second-level cache: enabled
11:28:39,860 INFO [SettingsFactory] Query cache: enabled
11:28:39,860 INFO [SettingsFactory] Cache region factory : org.hibernate.cache.jbc2.JndiMultiplexedJBossCacheRegionFactory
11:28:39,860 INFO [SettingsFactory] Optimize cache for minimal puts: enabled
11:28:39,860 INFO [SettingsFactory] Cache region prefix: tempdb
11:28:39,876 INFO [SettingsFactory] Structured second-level cache entries: disabled
11:28:39,876 INFO [SettingsFactory] Query cache factory: org.hibernate.cache.StandardQueryCacheFactory
11:28:39,876 INFO [SettingsFactory] Echoing all SQL to stdout
11:28:39,876 INFO [SettingsFactory] Statistics: disabled
11:28:39,876 INFO [SettingsFactory] Deleted entity synthetic identifier rollback: disabled
11:28:39,876 INFO [SettingsFactory] Default entity-mode: pojo
11:28:39,876 INFO [SettingsFactory] Named query checking : enabled
11:28:39,876 INFO [SessionFactoryImpl] building session factory
11:28:39,876 INFO [NamingHelper] JNDI InitialContext properties:{}
11:28:39,876 INFO [SessionFactoryObjectFactory] Not binding factory to JNDI, no JNDI name configured
11:28:39,876 INFO [NamingHelper] JNDI InitialContext properties:{}
11:28:39,876 INFO [UpdateTimestampsCache] starting update timestamps cache at region: tempdb.org.hibernate.cache.UpdateTimestampsCache
11:28:39,876 INFO [StandardQueryCache] starting query cache at region: tempdb.org.hibernate.cache.StandardQueryCache
11:28:39,891 WARN [AbstractEntityManagerImpl] Cannot join transaction: do not override hibernate.transaction.factory_class
11:28:39,891 INFO [STDOUT] #########################################1267163919891
11:28:39,891 INFO [STDOUT] Hibernate:
select
sample0_.id as id12_,
sample0_.name as name12_
from
sample sample0_
11:28:39,907 INFO [STDOUT] 1
11:28:39,907 INFO [STDOUT] Lala Lajpat Rai
11:28:39,907 INFO [STDOUT] 2
11:28:39,907 INFO [STDOUT] Dr. S Radhakrishnan
11:28:39,907 INFO [STDOUT] 3
11:28:39,907 INFO [STDOUT] Ustad Zakir Hussain
11:28:39,907 INFO [STDOUT] 4
11:28:39,907 INFO [STDOUT] Sardar Vallabhbhai Patel
11:28:39,907 INFO [STDOUT] 5
11:28:39,907 INFO [STDOUT] Gautam Buddha
11:28:39,923 INFO [STDOUT] #########################################1267163919923


it is hiting DB always when i call the query

i am calling it twice here

Francesco Marchioni
author
Ranch Hand

Joined: Sep 22, 2003
Posts: 190
Hello,
well from your logs, you can see that the QueryCache has been activated.
The configuration looks good except that I don't use the property

<property name="hibernate.cache.region.jbc2.cfg.query" value="local-query"/>

in my standard 2nd level cache configuration as it doesn't replicate across the cluster the data (and doesn't use the timestamp to verify the validity of the cache)

I advice you to run a test using a plain query instead of a named query:

Query query = session.createQuery("from tableXX");
query.setCacheable(true);

and see if it hits just one time or not.....
hope it helps
Francesco
ambika mullasseri
Greenhorn

Joined: Jun 01, 2009
Posts: 28
but there is no setCacheable() for import javax.persistence.Query
Francesco Marchioni
author
Ranch Hand

Joined: Sep 22, 2003
Posts: 190
ambika mullasseri wrote:but there is no setCacheable() for import javax.persistence.Query

yes I forgot to mention, it's org.hibernate.Query and you can get it in your EJB by injecting the SessionFactory.

ambika mullasseri
Greenhorn

Joined: Jun 01, 2009
Posts: 28

i am using JPA for persistance.hibernate is the JPA provider.still can i do dis???
Francesco Marchioni
author
Ranch Hand

Joined: Sep 22, 2003
Posts: 190
Yes Hibernate can bind the current Session to the current JTA Session (see https://www.hibernate.org/42.html)
However this was just for testing if the problem was in your named query.
If it works using a plain Hibernate Query (with setCacheable true) then you're sure the Hibernate configuration is ok and you restrict the possible causes of errors......
ambika mullasseri
Greenhorn

Joined: Jun 01, 2009
Posts: 28
i got this error while deploying ..am i doing something wrong??

java.lang.RuntimeException: <injection-target> could not be found: com.SampleDao.mycache



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JBOSS CACHE second level caching for named query cache
 
Similar Threads
Weird @Cache annotation usage error
Hibernate second level cache questions
EJB3 Query Cache
Second level cache does not work
ehcache.xml file I have in my ejb app is not used