-->
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.  [ 15 posts ] 
Author Message
 Post subject: sometimes a proxy, somtimes the "real" class
PostPosted: Wed Apr 27, 2011 11:51 am 
Newbie

Joined: Thu Apr 21, 2011 5:31 am
Posts: 9
Hi I have a web-application with a Spring MVC frontend and a Hibernate backend.
I have the following problem:

I start up the application and the first screen is an overview of my main objects that are all of the same class (Event)
Next I select one for editing in another screen with a form

If I reload the same page multiple times I see that in some occasions an object of class Event is used to render the page and sometimes an object that is a proxy for this class.

To make matters worse. If I change something I get different versions of the class when reloading the page.
things have been wriiten to the database correctly.
The class is loaded through: session.load().

Can anyone explain to me what is going on?


Top
 Profile  
 
 Post subject: Re: sometimes a proxy, somtimes the "real" class
PostPosted: Thu Apr 28, 2011 3:08 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
session.load() always returns proxies. See the API for more information. You should use session.get() to retrieve objects from the database.

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: sometimes a proxy, somtimes the "real" class
PostPosted: Thu Apr 28, 2011 3:42 am 
Newbie

Joined: Thu Apr 21, 2011 5:31 am
Posts: 9
Thanks for the quick reply
What I do not understand though: if load always retrurns proxies, why do I see the non-proxied class (the original) returned many times, and proxies on other occasions when all I do is reloading the page.


Top
 Profile  
 
 Post subject: Re: sometimes a proxy, somtimes the "real" class
PostPosted: Thu Apr 28, 2011 4:51 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Hard to say without your code or a crystal ball. ;-)

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: sometimes a proxy, somtimes the "real" class
PostPosted: Thu Apr 28, 2011 6:15 am 
Regular
Regular

Joined: Thu May 07, 2009 5:56 am
Posts: 94
Location: Toulouse, France
if the object has already been loaded in memory - found in cache level 1 (session) or second cache (if active) no proxy is returned. load() method doesn't hit the database to find the object this why it returns a proxy (but if the object is available in memory it returns the real object).

_________________
everything should be made as simple as possible, but not simpler (AE)


Top
 Profile  
 
 Post subject: Re: sometimes a proxy, somtimes the "real" class
PostPosted: Thu Apr 28, 2011 7:41 am 
Newbie

Joined: Thu Apr 21, 2011 5:31 am
Posts: 9
Code:
public Object getObjectById(int objectId) throws EventManagerException {
      Session session = getSessionFactory().getCurrentSession();
      Event event = null;
      try {
         Transaction transaction = session.beginTransaction();         
         session.beginTransaction();
         event = (Event) session.load(Event.class, Integer.valueOf(objectId));
      } catch (Exception e) {
         e.printStackTrace();
         throw new EventManagerException(e);
      }
      return event;
   }

for the sessions in spring I use: OpenSessionInViewFilter with singleSession=true
I subclassed it and overwrote two methods:
Code:
   @Override
   public void afterPropertiesSet() throws javax.servlet.ServletException {
      setSingleSession(true);
   }

   public void closeSession(Session session, SessionFactory sessionFactory) {
      if(!session.getTransaction().wasCommitted() && session.getTransaction().isActive()){
         session.getTransaction().commit();
      }
      session.flush();
      super.closeSession(session, sessionFactory);
   }



Code:
@Entity
@Table(name = "event")
public class Event extends PersistentObject {


   private Date startDate;
   private Date endDate;
   private Set<SubscriptionHistory> subscriptionHistories;
   private Set<Question> questions;
   private EventType eventType;
   private String description;
   private String title;
   private String location;
   private boolean isKnowledgeRelated = false;
   private Date respondBefore;
   private Person createdBy;

   public boolean foo() {
      return true;
   }

   @Id
   @GeneratedValue
   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   @Version
   @Column(name = "version")
   public Integer getVersion() {
      return this.version;
   }

   public void setVersion(Integer version) {
      this.version = version;
   }
..........
.........
}


I hope this is sufficient, if not please tell me and I'll provide you with other parts you require


Top
 Profile  
 
 Post subject: Re: sometimes a proxy, somtimes the "real" class
PostPosted: Thu Apr 28, 2011 7:46 am 
Newbie

Joined: Thu Apr 21, 2011 5:31 am
Posts: 9
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                         "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
   <session-factory>
      <!-- Database connection settings -->
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="connection.url">jdbc:mysql://localhost:3306/event_manager</property>
      <property name="connection.username">######</property>
      <property name="connection.password">######</property>
      <!-- JDBC connection pool (use the built-in) -->
      <!-- <property name="connection.pool_size">1</property> -->

      <property name="hibernate.c3p0.minPoolSize">5</property>
      <property name="hibernate.c3p0.maxPoolSize">200</property>
      <property name="hibernate.c3p0.timeout">1800</property>
      <property name="hibernate.c3p0.max_statement">50</property>
      <property name="hibernate.generate_statistics">true</property>

      <!-- SQL dialect -->
      <!-- <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> -->
      <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
      <!-- Enable Hibernate's automatic session context management -->
      <property name="current_session_context_class">thread</property>
      <property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
      <!-- Disable the second-level cache -->
      <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
      <!-- Echo all executed SQL to stdout -->
      <property name="show_sql">true</property>
      <property name="format_sql">true</property>
      <property name="use_sql_comments">false</property>

      <property name="new_generator_mappings">true</property>

      <!-- <property name="hbm2ddl.auto">create-drop</property> -->
      <mapping class="data.Question" />
      <mapping class="data.Answer" />
      <mapping class="data.DefaultAnswer" />
      <mapping class="data.Person" />
      <mapping class="data.ExternalPerson" />

      <mapping class="data.EmployeePerson" />
      <mapping class="data.Event" />
      <mapping class="data.Profile" />

      <mapping class="data.SubscriptionEntry" />
      <mapping class="data.SubscriptionHistory" />
   </session-factory>
</hibernate-configuration>



Top
 Profile  
 
 Post subject: Re: sometimes a proxy, somtimes the "real" class
PostPosted: Thu Apr 28, 2011 7:49 am 
Newbie

Joined: Thu Apr 21, 2011 5:31 am
Posts: 9
hallmit wrote:
if the object has already been loaded in memory - found in cache level 1 (session) or second cache (if active) no proxy is returned. load() method doesn't hit the database to find the object this why it returns a proxy (but if the object is available in memory it returns the real object).


I understand what you say. What I´m missing though is why it is not consistent. I´d expect the first view to be from database and the rest from cache. It alternates irregularly.


Top
 Profile  
 
 Post subject: Re: sometimes a proxy, somtimes the "real" class
PostPosted: Thu Apr 28, 2011 9:07 am 
Regular
Regular

Joined: Thu May 07, 2009 5:56 am
Posts: 94
Location: Toulouse, France
humm...very bizarre. have you tried to turn on the DEBUG mode? maybe it can show you where objects are found at each displaying.
take a look at http://docs.jboss.org/hibernate/core/3. ... on-logging to enable loggers in Hibernate.

_________________
everything should be made as simple as possible, but not simpler (AE)


Top
 Profile  
 
 Post subject: Re: sometimes a proxy, somtimes the "real" class
PostPosted: Thu Apr 28, 2011 2:07 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
So... I am just wondering why is this important? Are you experiencing any problems because of this? The proxy is a subclass of the Event class so everything should work the same as with a "real" object. Assuming of course that the proxy has been initialized and you are not getting any LazyInitializationExceptions.


Top
 Profile  
 
 Post subject: Re: sometimes a proxy, somtimes the "real" class
PostPosted: Thu Apr 28, 2011 3:56 pm 
Newbie

Joined: Thu Apr 21, 2011 5:31 am
Posts: 9
I discovered this after some other problems:
After changes made to the object. I see the old values and the new values alternating with page reloads.
If I do a change again I get a stacktrace (can't remember which right now)
After investigating and seeing that I get different instances, I think it all boils down to mutiple instances being loaded for the same "object".

I hope that if I understand what's going on, I also know how to deal with the other problem I run into.
I must be doing something just not quite right.
I browsed the internet for days but have not found anything helpfull.


Top
 Profile  
 
 Post subject: Re: sometimes a proxy, somtimes the "real" class
PostPosted: Thu Apr 28, 2011 4:17 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Quote:
I see the old values and the new values alternating with page reloads.


This could be a browser caching problem... I have seen it happen from time to time. Usually when there is some other problem on the server side, the browser may display a cached version of an old page instead. Your friend here is to write debug logs on the server side. Otherwise it can be hard to track down what is actually happening.

Quote:
If I do a change again I get a stacktrace (can't remember which right now)


Would be good if you could get hold of that and post it here...

Quote:
I think it all boils down to mutiple instances being loaded for the same "object".


This should not really happen since Hibernate guarantees that within a session a given entity can only be represented by a single object (http://docs.jboss.org/hibernate/core/3. ... s-identity).


Top
 Profile  
 
 Post subject: Re: sometimes a proxy, somtimes the "real" class
PostPosted: Fri Apr 29, 2011 3:42 am 
Newbie

Joined: Thu Apr 21, 2011 5:31 am
Posts: 9
the stacktrace I see starts with
org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect):

It happens if I save the object again, but not all the time. I do not see a relation between saving a proxy or the real object and the stacktrace.

I checked whether it is a browser cache problem with a date in my HTML: it is not my browser showing a cached page


Top
 Profile  
 
 Post subject: Re: sometimes a proxy, somtimes the "real" class
PostPosted: Fri Apr 29, 2011 1:05 pm 
Newbie

Joined: Thu Apr 21, 2011 5:31 am
Posts: 9
little clarifcation about my last post:
There is no corrrelation between trying to save a proxy or the actual object and the stacktrace
from an outside view it seems too happen randomly.

but as pointed out by nordborg: I'd expect only one instance in the session, and only one session available.
If I understand correctly sesssions are not being recycled

This all makes it a riddle to me


Top
 Profile  
 
 Post subject: Re: sometimes a proxy, somtimes the "real" class
PostPosted: Fri Apr 29, 2011 1:38 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
My guess is that there is something strange going on in your Spring setup. The issues you are seeing could for example be explained by not properly closing the session at the end of the request. For example, if there is an exception in your closeSession() method (posted earlier) before it reaches super.closeSession(), I guess the session would never be closed and still be around the next time the same thread is used to process another request. By the way, you should probably flush() before you commit().


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 15 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.