Log debug and Hibernate, a tale of two environments

Cute story.

A couple of years back I got a call for support with a recently released webapp. User is complaining that a particular functionality was returning the following hibernate exception.

org.hibernate.LazyInitializationException: could not initialize proxy – the owning Session was closed

A quick source check have indeed shown that the field in question was lazy initialised when it should not have been. But the puzzle remains, it worked in UAT environment. In fact it was extensively tested by the particular user who reported the issue before it was released into production. Not to mention all the developer testing that was done.

The simple explanation would be, we simply missed that particular functionality when we did the test. But that was soon ruled out as in UAT, the bug was still not rearing it’s ugly stack trace. So, what was it in production that is making it fail?

As it turned out, the correct question should be, what was in UAT that made it work? This:

if (logger.isDebugEnabled()) {    
  logger.debug(dataMap.toString());
}

The reason it was working UAT was because log was set to debug in that environment, while in production it was set to info only. Having the dataMap.toString() called in that debug block means hibernate actually went and fetched the lazy initialised data, as oppose to in production where the first time the code needed dataMap, the hibernate session was already closed.

I’m still not sure what exact lesson I should take away from this, but it sure was an interesting code mystery.

Tagged ,

Android “Fling” Gesture Detection

Had some issues last night trying to get my Android game to detect the fling gesture, basically a click and drag in mouse world.

I had the following code in my little View object.

private GestureDetector gestureDetector;
private View.OnTouchListener gestureListener;// Gesture detection
    gestureDetector = new GestureDetector(new MyGestureDetector());
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }
    };
this.setOnTouchListener(gestureListener);

  * Handle any fling action here
  * @author dracox
  *
  */
 protected class MyGestureDetector extends SimpleOnGestureListener {
  /**
   * We need to capture any user line drawn request here.
   */
  @Override
  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
   //This should only be caring about flings after init
   gameWalls.add(new FireWall(new Point((int)e1.getRawX(), (int)e1.getRawY()), new Point((int)e2.getRawX(), (int)e2.getRawY())));
   return true;
  }
 }

I fired up my emulator, but was not getting any fling action logged. However the onTouch events were been fired off.

Initially I though maybe the emulator couldn’t handle fling event, but after much searching I found the root of the problem.

Apparently, the onDown event was fired off first, which was consumed before it became a “fling”, so I had to override that too on my custom Gesture Detector class.

I was a little puzzled about returning true for the onDown handler, as the API stated return true to consume the event and false to let it pass.

/**
 * Need to let onDown pass through, otherwise it will block the onFling event
 */
 @Override
 public boolean onDown(MotionEvent event) {
 return true;
 }

Now we’re flinging!

Tagged , ,