-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Cache and test problem on the JBoss
PostPosted: Tue Feb 10, 2009 11:56 am 
Newbie

Joined: Tue Feb 10, 2009 8:32 am
Posts: 1
Hello,

I'm searching on the net and in this forum and I'm not able to find out any reasonable solution.

I'm using JBoss and Hibernate together.
I wrote some junit tests that come up to database directly by jdbc connection. They delete data from database tables - it means that they prepare database.
Then some operations are provided by hibernate in the application.

The first run of tests is ok. First means the run after starting of JBoss.
Hibernate creates its entities and work correctly. But on the second run (when data in database was erased by jdbc connection) the foreign key constraint violation exception is thrown.
It seams that Hibernate returns old data that is probably stored in cache and doesn't synchronize itself with database state.

The workflow is following: I test whether entity exists by EJBQL query. Whether entity isn't found entity then new object is created and is persisted by injected entity manager by insert() method.
This new entity is set as a "parent" to another entity and when the new child entity should be created then exception occurs. The parent entity isn't created yet. The insert method doesn't save data to database. I thought that cache of hibernate stores information and at the next run of the test it returns old information because isn't synchronized with database. Is this behaviour normal? Or does I understand it correctly?

I was trying to delete all data from cache by the advise that I found on the net but no second level cache regions were found.

Code:
   @PersistenceUnit(unitName = "myPersistenceUnit")
   private EntityManagerFactory factory;

   /**
    * Clear cache.
    */
   private void attemptToClearCache()
   {
      // by injection we get InjectedEntityManagerFactory
      InjectedEntityManagerFactory injectedFactory = (InjectedEntityManagerFactory) factory;
      // by delegation we ge entitymanager factory impl
      EntityManagerFactoryImpl factoryImpl = (EntityManagerFactoryImpl) injectedFactory.getDelegate();
      SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) factoryImpl.getSessionFactory();
      Map cacheRegionsMap = sessionFactoryImpl.getAllSecondLevelCacheRegions();
      Collection<Cache> cacheRegions = cacheRegionsMap.values();
      for (Cache cache : cacheRegions)
      {
         cache.clear();
      }
    }



So I don't know whether it's possible to use managed entitymanagerfactory on JBoss container or not...? And is there some another possibility how clear cache programatically?

Or could be error somewhere else?
And is it possible to synchronize hibernate and its cache with changed database somehow?

Thanks for reactions.


Code:


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.