-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate EntityManager gives stale data randomly
PostPosted: Wed Jan 21, 2009 12:45 pm 
Newbie

Joined: Wed Jan 21, 2009 12:19 pm
Posts: 2
I have a problem related to getting stale (cached?) data from Hibernate
EntityManager injected to SFSB (so I'm using it through JPA) using
extended persistence context. Apache Geronimo 2.3.1 is used as
the Java EE 5 container.

I have disabled query and 2nd level caches in the configuration and
Hibernate logs state both caches as being disabled.

The problem occurs randomly: after persisting and modifying some data,
the EntityManager starts to return old instances/values for the objects.

The application is a web application and I'm creating new Session Beans for
each request through JNDI lookups, and a new EntityManager gets injected
for each request, so that should not be the problem (I've even checked the
object hashcodes of the injected EntityManagers and Session Beans).

Here's a log excerpt from a simple "SELECT * FROM CONFIGURATION" query.
This fetches all instances of ConfigurationAttribute class that you can see in
the debug log - the contents of the class don't matter. The class does not
have any mappings to other EntityBeans, just plain string values.

See the log: http://pastebin.com/m1e308e8f

The weird thing is that on line 101 I can see "[Printer] listing entities:" and
then a list of old cached instances of the objects (before the query is
executed). Why does Hibernate store these objects and why do newly injected
EntityManagers have (managed?) objects inside them ?

Although Hibernate then makes the query to the database, it seems to return
the old instances anyway... and it does this randomly!

I wonder if I should call EntityManager.clear() in SFSB initialization or is there
something else to know about injected EntityManagers...?

Hibernate version:
3.3.1

Apache Geronimo 2.3.1 as Java EE 5 container

Mapping documents:
None.

Code between sessionFactory.openSession() and session.close():
The code uses container-managed injected EntityFactory.

Full stack trace of any exception that occurs:
None.

Name and version of the database you are using:
MySQL 5.0

The generated SQL (show_sql=true):
On line 131 of the log:

Code:
[SQL] select configurat0_.ID as ID12_, configurat0_.NAME as NAME12_, configurat0_.VALUE as VALUE12_, configurat0_.TYPE as TYPE12_ from CONFIGURATION configurat0_


Debug level Hibernate log excerpt:
For full log see: http://pastebin.com/m1e308e8f


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 28, 2009 6:30 am 
Newbie

Joined: Wed Jan 21, 2009 12:19 pm
Posts: 2
I've done some more debugging and one option that I tried was
to call EntityManager.clear() in @PostConstruct method of all SFSBs that use
the injected EntityManagers.

This indeed solved the problem of getting stale data from session cache,
but it also introduced another problem: when using multiple DAOs in
one (http) request (I'm using entitymanager-per-request pattern),
looking up (JNDI) additional (more than one) SFSB caused the
session cache of all active EntityManagers to get cleared and
all objects to be detached. Is this normal behavior ?

Do EntityManagers injected in separate SFSBs have a common
session cache ? How should I deal with this ?

------

Then about configuring Hibernate with CMT:

Could someone point out how to properly configure Hibernate
to use container-managed transactions in Geronimo ?
I have the following stuff in persistence.xml:

Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence">
   <persistence-unit name="lobby" transaction-type="JTA">
      <description>Design Factory Lobby Database</description>
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <jta-data-source>LobbyDataSource</jta-data-source>
      <!-- <class></class> -->
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
         <property name="hibernate.default_batch_fetch_size" value="8"/>
         <property name="hibernate.jdbc.batch_size" value="20"/>
         <property name="hibernate.jdbc.fetch_size" value="30"/>

         <!--
            0 = TRANSACTION_NONE
            1 = TRANSACTION_READ_UNCOMMITTED
            2 = TRANSACTION_READ_COMMITTED
            4 = TRANSACTION_REPEATABLE_READ
            8 = TRANSACTION_SERIALIZABLE
         -->
         <property name="hibernate.connection.isolation" value="4"/>

         <!-- Cache -->
         <property name="hibernate.cache.use_query_cache" value="false"/>
         <property name="hibernate.cache.use_second_level_cache" value="false"/>
         <!-- <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider"/> -->

         <!-- Transaction integration -->
         <!-- Use container-managed transactions (CMT) -->
         <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.GeronimoTransactionManagerLookup"/>

         <!-- Debugging -->
         <!--  <property name="hibernate.show_sql" value="true"/> -->
         <!--  <property name="hibernate.format_sql" value="true"/> -->
      </properties>
   </persistence-unit>
</persistence>



The question is: should I set the following properties
and if so, what values should I use:

(I've tried with the values below)


Code:
<property name="hibernate.current_session_context_class" value="org.hibernate.context.JTASessionContext"/>


Setting transaction factory class causes Hibernate
to print out warnings in the logs, although Hibernate FAQ
about CMT specifically states that the property should be set.

Code:
<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory"/>


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

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.