What is wrong is that you don't shut down the SessionFactory
, which maintains a lot of system resources. Some of those resources must be threads, which keep your application from ending.
Hibernate second level cacheing - Program does not terminate
-
30-05-2022 - |
Question
@Entity
@Table(name="user_details")
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.READ_ONLY)
public class UserDetails {...}
The above code is how I defined my Entity class, UserDetails, as cacheable
. I also added two lines in the hibernate.cfg.xml
:
<!-- second-level cache -->
<property name="cache.use_second_level_cache">true</property>
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
In my service main method I am accessing twice the same UserDetails Object, in different sessions :
session = sessionFactory.openSession();
session.beginTransaction();
UserDetails user = new UserDetails();
user = (UserDetails)session.get(UserDetails.class, 3);
logger.info(user);
session.getTransaction().commit();
session.close();
Session session2 = sessionFactory.openSession();
session2.beginTransaction();
UserDetails user2 = new UserDetails();
user2 = (UserDetails)session2.get(UserDetails.class, 3);
logger.info(user2);
session2.getTransaction().commit();
session2.close();
I get, as expected only one SELECT
query, which implies that hibernate fetches the data from the second level cache. I also print fine my objects on the console.
But, my program does not end. It sits there running, on Eclipse
, and I have to stop it manually. Am I doing something wrong? Thanks.
Solution
OTHER TIPS
Try changing the version of ehcache. I have tried this thing and it worked. See this http://forums.terracotta.org/forums/posts/list/6450.page
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow