tags:

views:

80

answers:

4

Hi all,

I've got a very curious problem in Hibernate that I would like some opinions on. In my code if I do the following:

  • Go to page A
  • Click a link on page A to be taken to page B
  • Click on data item on page B
  • Exception thrown

I get an error telling me:

failed to lazily initialize a collection of role: XYZ, no session or session was closed

Fair enough. But when I do the same thing but add an alt+tab in the middle, everything is fine. E.g.

  • Go to page A
  • Click a link on page A to be taken to page B
  • Hit ALt+Tab to switch to another application
  • Hit ALt+Tab to switch back to the web browser
  • Click on data item on page B
  • Everything is fine.

I'm a little confused as to how switching focus from my application makes it act as I want it to. Does anyone have any light to shine on the subject? I don't think it's a locking issue as even if I do the second set of steps quicker than the first, still no error.

It's a Seam application using Hibernate 3.3.2.GA & 3.4.0.GA.

A: 

are you using javascript? Maybe an event is firing when you switch back to the browser that somehow clears the bad state?

Ken Liu
A: 

It's not really related to switching the applications; maybe your browser updates something during focus lost/gained.

The reason for your issue is this:

  1. In the first request, you load an object and store that in the session. This object contains a collection of some kind.

  2. In the next request, you try to do something with the collection but you forgot to call session.refresh(object), first.

What happens is that the session is automatically closed when the first request is finished. But the backed collection in your object still contains a reference to it (so that it can be loaded automatically when you access it for the first time).

Aaron Digulla
A: 

Does your page complete it's rendering? I have found that sometimes when I get errors, instead of displaying the error, the server simply quits producing output. It outputs the error to the log, but thats about it. I end up with a half completed page. View source and see if you get to the end of the body, perhaps your switching caused the server to get lazy, or your browser to terminate the connection prematurely when the error occured

Zoidberg
A: 

My advice is to do a unit test to see if you can reproduce the error in other environment isolated from the browser ;)

Rafa de Castro