-->
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.  [ 4 posts ] 
Author Message
 Post subject: Event Listeners and 'Collection was not processed by flush'
PostPosted: Fri Sep 23, 2005 8:20 am 
Newbie

Joined: Wed May 11, 2005 10:28 am
Posts: 10
I am using some Hibernate event listeners which all look like this one:

Code:
public final class IntegrityGuardianPreUpdate implements PreUpdateEventListener {

   private static final Class validatableObjectClass = ValidatableObject.class;
   
   private final DefaultPreUpdateEventListener defaultListener = new DefaultPreUpdateEventListener();
   
   private IntegrityGuardianPreUpdate() {      
   }
   
   public static final IntegrityGuardianPreUpdate INSTANCE = new IntegrityGuardianPreUpdate();

   @SuppressWarnings("unused")
   private Object readResolve() throws ObjectStreamException {
      return INSTANCE;
   }
   
   public boolean onPreUpdate(final PreUpdateEvent event) throws HibernateException {
      
      final Object toPreUpdate = event.getEntity();      
      
      if (validatableObjectClass.isInstance(toPreUpdate)) {
         
         final Session s = HibernateUtil.currentSession();
         s.setFlushMode(FlushMode.COMMIT);
         
         final List<Problem> theProblems;
         
         try {
            theProblems = ((ValidatableObject) toPreUpdate)
               .validate();
         } catch (final HibernateException e) {
            if (Loggers.DATA_VALIDATION.isEnabledFor(Level.FATAL)) {
               Loggers.DATA_VALIDATION.fatal("Error in validator.",e);
            }
            throw e;
         }


         s.setFlushMode(FlushMode.AUTO);
         
         if (theProblems.size()>0)
            throw new ValidationException(theProblems);
         
      }
      
      return defaultListener.onPreUpdate(event);
   }

}


I am getting lots of AssertionFailure: Collection was not processed by flush. I suspect that this has something to do with me changing FlushMode to commit during validation (but as I need to validate against the DB's state from before I save, I can't let the session autoflush).

Am I right in my assumption, and if yes, is there a way out of this?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 8:55 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
I think you are right in your assumption.

I don't think it is allowed to use the session in event code. If this is true, it would be great to mention it in the documentation.

Maybe you can implement your custom flush listener that do not flush anything during your validation.

_________________
Seb
(Please don't forget to give credits if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 23, 2005 9:20 am 
Newbie

Joined: Wed May 11, 2005 10:28 am
Posts: 10
I'm certainly not sure of this, but I think the event archtecture has been introduced precisely in order to allow callbacks into the session (and for some other reasons).

I've already tried providing a FlushListener and an AutoFlushListener, but they don't solve anything. Besides, the problem is precisely this - that not flushing seems to lead to the AssertionFailure, so killing extra flush events won't help.

How is the session's FlushMode supposed to be used, actually? The reference documentation is not quite clear on this point.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 26, 2005 7:41 am 
Newbie

Joined: Wed May 11, 2005 10:28 am
Posts: 10
Searching through JIRA I found out that although the session can be called from listeners, it's unsafe to do that during flush (which practivally means that it's always unsafe, because listners tend to be called during flushes anyway).

So I went to some extents and dropped all calls to the session (opening a different one to run the validation queries, as suggested in JIRA). I got no effect from this, same AssertionFailures came back to haunt me :-)

Than I unregistered the listeners, and everything was fine. Except for data validation, which no longer occurs, as I effectively cancelled it.

Is there a bug causing AssertionFailures when save or update listeners are registered? I remember somebody suspecting this in another post, but unfortunately I can't find it now.

Has anybody tried the last beta? How des it behave regarding this? (Actually I'll try it myself, but more opinions wouldn't hurt)


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