Slow hbernate query cache - java

We use a level 2 cache in the project (hibernate ehcache version 5.6.3):
<props>
<property name="hibernateProperties">
<props> <prop key="shared-cache-mode">ENABLE_SELECTIVE</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<prop key="net.sf.ehcache.configurationResourceName">ehcache.xml</prop>
<prop key="hibernate.cache.use_structured_entries">false</prop>
<prop key="hibernate.generate_statistics">true</prop>
</props>
In almost all requests, we get data from the cache in about 1 ms.
But one request to get a list of objects: when accessing the database -500 ms, from the cache - 100-500 ms,
Almost the same request, but on getting the number of objects: when accessing the database - 100 ms, from the cache - 1 ms,
The database is on localhost.
request cache:
<cache
name="query.FindDiscussionByFilter"
maxEntriesLocalHeap="500000"
eternal="false" timeToIdleSeconds="3600"
timeToLiveSeconds="3600"
memoryStoreEvictionPolicy="LRU"
statistics="true">
<persistence strategy="localTempSwap" />
</cache>
Is it possible to speed up the cache?
Is it possible to save data only in RAM?
Since they are immediately saved to disk.

Related

getting jdbc transaction failed at first hit

I am getting JDBC transaction failed at first hit to DB. I found one problem is that. My mysql is idle for 10 to 12 hours. so may be after 10 hours when i am going to hit at login time getting exception
my configuration is
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.jdbc.batch_size">50</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.connection.isolation">2</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.timeout">300</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.idle_test_period">3000</prop>
</props>
</property>
<property name="packagesToScan" value="com.perennialsys.salesoptimization.vo" />
</bean>
please help for this. thanks in advanced
one more thing that i got from googling is idle_test_period should not exceeds timeout. is this the reason?
The database server may close a connection on its side after a certain amount of time - causing some error in your application, because it'll attempt to send a query on a connection which is no longer available on the server side.
In order to avoid this you can let the pool periodically check a connection for it's validity. This is what idle_test_period is for.
timeout is the timespan after which the pool will remove a connection from the pool, because the connection wasn't checked out (used) for a while and the pool contains more connections than c3pO.min_size.
Please keep in mind, that hibernate.c3p0.idle_test_period value must never exceed that of hibernate.c3p0.timeout. Otherwise C3P0 will never detect connections that has been closed.
Thanks,
Rajesh

Issues with finding objects queried with HQL or session filter in Hibernate 4 + Spring 4 + JTA

We recently migrated from Hibernate 3.2.3, Spring 3.2.2 to Hibernate 4.3.11 and Spring 4.2.1 on Weblogic 10.3
Now in a transaction when hibernate objects are created and added to a hibernate object's persistent collection and later queried with HQL(using hibernateTemplate) or Hibernate's "Query" feature (in the same transaction and before the end of the transaction), Hibernate fails to find the objects that were added. This worked with Hibernate 3 and Spring 3 before the upgrade, but now fails.
e.g. In Psuedo code, say i have a library class with the following property.
class Library{
private Collection<Books> books;
}
In a transaction, i do the following -
...
Book book1 = new Book();
book1.setAuthor("Patrick Holt");
library.getBooks().add(book1);
and later on in the same transaction, Hibernate's "Query" is used with a Filter to look for books by the author, like so
Session s = hibernateTemplate.getSessionFactory().getCurrentSession();
Query q = s.createFilter(library.getBooks(), "where this.author = :authorName");
q.setParameter("authorName", "Patrick Holt");
List l = q.list();
q.list() in the above example returns 0 results. This would return 1 result before the upgrade. After the upgrade i get 0 results.
I read about some changes in the currentSessionContext in upgrades to Hibernate 4 and Spring 4, but I'm not sure what needs to change in order for the behavior to be just like before the upgrade, without the code having to change.
Out hibernate objects are defined in hbm.xml files and here's my configuration of the session factory.
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">/WEB-INF/ehcache.xml</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<!--
Tried adding the following properties, but it didn't work either
<prop key="hibernate.transaction.jta.platform">org.hibernate.engine.transaction.jta.platform.internal.WeblogicJtaPlatform</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.JtaTransactionFactory</prop>-->
</props>
</property>
<property name="dataSource" ref="dataSource"/>
<property name="mappingJarLocations">
<list>
<value>/WEB-INF/lib/app-1.0.jar</value>
</list>
</property>
</bean>
Transaction Manager definition:
<bean id="transactionManager" class="org.springframework.transaction.jta.WebLogicJtaTransactionManager"/>
The datasource is made available over jndi.
Any ideas on what i need to do to fix this issue?
The issue i was having is exactly as described in Spring Jira Issue SPR-13848. As i understand, it seems like the OpenSessionInViewFilter opens a request scoped session early and there are issues with syncing with a transaction that's started later.
I could not get rid of my OSIV because of the impact that it would cause throughout my application.
However, i found out that hibernate has a property that will accomplish something similar to prevent lazy loading -
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
This solved the issue. Adding this property allowed me to remove the OSIV, and hibernate's AUTO flush mode works as expected in a transaction now. I also used the CMTTransactionFactory. All my hibernate properties:
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">/WEB-INF/ehcache.xml</prop>
<prop key="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory</prop>
<prop key="hibernate.enable_lazy_load_no_trans">true</prop>
<prop key="hibernate.transaction.jta.platform">org.hibernate.engine.transaction.jta.platform.internal.WeblogicJtaPlatform</prop>
<prop key="hibernate.transaction.factory_class">org.hibernate.engine.transaction.internal.jta.CMTTransactionFactory</prop>
</props>

JBoss crashes: datasource cannot reuse connections

We have JBOSS 4.0.3.SP1 over TOMCAT 5.25.26 application server.
Usually it works fine, but about once a month we have "No ManagedConnections available"
Caused by: org.jboss.util.NestedSQLException: No ManagedConnections available within configured blocking timeout ( 5000 [ms] ); - nested throwable: (javax.resource.ResourceException: No ManagedConnections available within configured blocking timeout ( 5000 [ms] ))
at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:79)
at org.springframework.orm.hibernate3.LocalDataSourceConnectionProvider.getConnection(LocalDataSourceConnectionProvider.java:82)
at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:423)
... 73 more
Caused by: javax.resource.ResourceException: No ManagedConnections available within configured blocking timeout ( 5000 [ms] )
at org.jboss.resource.connectionmanager.InternalManagedConnectionPool.getConnection(InternalManagedConnectionPool.java:246)
at org.jboss.resource.connectionmanager.JBossManagedConnectionPool$BasePool.getConnection(JBossManagedConnectionPool.java:529)
at org.jboss.resource.connectionmanager.BaseConnectionManager2.getManagedConnection(BaseConnectionManager2.java:410)
at org.jboss.resource.connectionmanager.TxConnectionManager.getManagedConnection(TxConnectionManager.java:342)
at org.jboss.resource.connectionmanager.BaseConnectionManager2.allocateConnection(BaseConnectionManager2.java:462)
at org.jboss.resource.connectionmanager.BaseConnectionManager2$ConnectionManagerProxy.allocateConnection(BaseConnectionManager2.java:894)
at org.jboss.resource.adapter.jdbc.WrapperDataSource.getConnection(WrapperDataSource.java:73)
When I look in oracle sessions table I can see that there ara all 100 available connections are INACTIVE and the last sql was the one we use to check connection (select sysdate from dual).
Connection pool just cannot reuse them Why this can happen?
We have JNDI datasource defined in oracle-ds.xml like this:
<datasources>
<local-tx-datasource>
<jndi-name>ROT</jndi-name>
<connection-url>jdbc:oracle:thin:#ttt.tt.tt:1521:usr</connection-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<user-name>usr</user-name>
<password>usrpass</password>
<min-pool-size>5</min-pool-size>
<max-pool-size>100</max-pool-size>
<idle-timeout-minutes>10</idle-timeout-minutes>
<track-statements>true</track-statements>
<blocking-timeout-millis>5000</blocking-timeout-millis>
<!-- Uses the pingDatabase method to check a connection is still valid before handing it out from the pool -->
<check-valid-connection-sql>select sysdate from dual</check-valid-connection-sql>
<metadata>
<type-mapping>Oracle9i</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
Here is the hibernate config for datasource:
<bean id="rotSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialec</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="hibernate.connection.release_mode">after_statement</prop>
<prop key="hibernate.c3p0.min_size">5</prop>
<prop key="hibernate.c3p0.max_size">20</prop>
<prop key="hibernate.c3p0.timeout">300</prop>
<prop key="hibernate.c3p0.max_statements">50</prop>
<prop key="hibernate.c3p0.idle_test_period">3000</prop>
</props>
</property>
<property name="dataSource" ref="dataSource"/>
Data source in hibernate:

Ehcache configuration issue (Element for key **** is null)

I have big issue with Ehcache configuration.
I use:
ehcache 2.6.2
hibernate 3.3.1 GA
My current configuration is next:
1) Hibernate
<bean id="hibernateProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.default_schema">#db.schema#</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="connection.pool_size">10</prop>
<prop key="hibernate.jdbc.batch_size">30</prop>
<prop key="hibernate.default_batch_fetch_size">30</prop>
<prop key="hibernate.max_fetch_depth">30</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.region.factory_class">
net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
</prop>
<prop key="net.sf.ehcache.configurationResourceName">
/ehcache-config.xml
</prop>
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
</bean>
2) ehcache-config.xml
<ehcache name="xxxx">
<diskStore path="java.io.tmpdir"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU"
/>
<cache name="org.hibernate.cache.UpdateTimestampsCache"
maxEntriesLocalHeap="5000"
eternal="true">
<persistence strategy="localTempSwap"/>
</cache>
<cache name="org.hibernate.cache.StandardQueryCache"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskExpiryThreadIntervalSeconds="120">
<persistence strategy="localTempSwap"/>
</cache>
</ehcache>
All java-objects implements Serializable and I use read-write caching, all queries is setCacheable(true), but I still can't see performance improvements.
First of all, I have many messages like this:
19:30:51,829 DEBUG [Segment] fault added 0 on disk
19:30:51,829 DEBUG [Segment] put added 0 on heap
19:30:51,829 DEBUG [Segment] fault removed 0 from heap
19:30:51,829 DEBUG [Segment] fault added 0 on disk
19:30:51,829 DEBUG [Segment] put added 0 on heap
19:30:51,830 DEBUG [Segment] fault removed 0 from heap
19:30:51,830 DEBUG [Segment] fault added 0 on disk
And the most important thing that I have noticed: all attempts to use cache ends with:
1) 19:30:47,396 DEBUG [EhcacheGeneralDataRegion] Element for key sql: **** is null
2) 20:26:31,939 DEBUG [EhcacheGeneralDataRegion] key: ****
20:26:31,939 DEBUG [EhcacheGeneralDataRegion] Element for key **** is null
Thank you for your assistance.
Best regards.

Has Anyone Gotten Hibernate to Use Elasticache as its 2nd Level Cache?

I found some threads saying this was doable, but did not find specific instructions or config information.
I want to do this from Beanstalk as well: the app should get deployed to beanstalk with a config that points hibernate to the elasticache instance(s).
Yes, we were able to configure hibernate with 2nd level cache.. Not with beanstalk though.. This code should help you with it.
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.cache.use_structured_entries">true</prop>
<!-- prop key="hibernate.hbm2ddl.auto" >update</prop -->
<prop key="hibernate.jdbc.batch_size">100</prop>
<prop key="hibernate.cache.provider_class">com.googlecode.hibernate.memcached.MemcachedCacheProvider
</prop>
<!-- Cache disabled -->
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.memcached.servers"><elasticachehostname>:11211</prop>
<prop key="hibernate.memcached.cacheTimeSeconds">300</prop>
<prop key="hibernate.memcached.connectionFactory">DefaultConnectionFactory</prop>
<prop key="hibernate.memcached.clearSupported">false</prop>
</props>
You would need the hibernate memcached jar as well

Categories