[jboss-user] [Persistence, JBoss/CMP, Hibernate, Database] - Hibernate and persisting problem

chalda do-not-reply at jboss.com
Wed Feb 11 04:10:56 EST 2009


Hello, 

I have quite annoying problem during persisting data through Hibernate to database on the Jboss application server.

I wrote some junit tests that come up to database directly by jdbc connection. They delete all data from database tables (preparing database state for testing). And application uses hibernate for data manipulation.

And then I have problem.
On the second run of the tests (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.
Is this explanation real?

The workflow is following: I test whether entity exists by EJBQL query. Whether entity isn't found then new object is created and is persisted by injected entity manager by insert() method.

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. 

My settings of persistence.xml for cache is following:

  | <property name="hibernate.cache.provider_class" value="org.jboss.ejb3.entity.TreeCacheProviderHook" />
  | <property name="hibernate.treecache.mbean.object_name" value="jboss.cache:service=EJB3EntityTreeCache" />
  | <property name="hibernate.cache.use_query_cache" value="false" />
  | <property name="hibernate.cache.use_second_level_cache" value="false" />
  | <property name="hibernate.cache.use_structured_entries" value="true" />
  | 

Is possible to uses this "retyping" on JBoss server? Is possible to clear cache by this 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();
  |       }
  |     }
  | 


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.


View the original post : http://www.jboss.org/index.html?module=bb&op=viewtopic&p=4208889#4208889

Reply to the post : http://www.jboss.org/index.html?module=bb&op=posting&mode=reply&p=4208889



More information about the jboss-user mailing list