views:

191

answers:

1

HI,

I am using Grails 1.2.1 and I always got this message when I run my apps and leave it without anyone using the apps.

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

at H__project_ilinkdev_grails_app_views_layouts_main_gsp$_run_closure2.doCall(H__project_ilinkdev_grails_app_views_layouts_main_gsp:220)

at H__project_ilinkdev_grails_app_views_layouts_main_gsp$_run_closure2.doCall(H__project_ilinkdev_grails_app_views_layouts_main_gsp)

at H__project_ilinkdev_grails_app_views_layouts_main_gsp.run(H__project_ilinkdev_grails_app_views_layouts_main_gsp:259)

at org.jsecurity.web.servlet.JSecurityFilter.doFilterInternal(JSecurityFilter.java:382)

at org.jsecurity.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:180)

at java.lang.Thread.run(Thread.java:619)

In my view I have load a class Menu:

/**

Menu Domain rule: one controller for one menu * */ class Menu { String menuName = ""; String menuController = ""; String menuAction = ""; Integer menuOrder = 1; String menuClass = ''; // html class Integer myid = 0 ;

boolean isUser = false; // is this user menu (user only menu) 
boolean isAdmin = false; // is this administrator menu 
boolean isHorizontal =false; // is this horizontal or vertical (if false)
String menuType='all'; // pia, dam, da, br, all  
Menu parentMenu;

static hasMany = [childMenu:Menu] ;
static belongsTo = [parentMenu:Menu];

static constraints = {
    menuName(blank:false, nullable:false);
    menuOrder(blank:false, nullable:false);
    menuController(blank:true,nullable:false); // blank/nullable if there is no controller
    menuAction(blank:true,nullable:false); // blank/nullable if there is no controller
    menuClass(blank:true,nullable:false); // css class ... blank : using default menu
    parentMenu(blank:true, nullable:true);
    childMenu(blank:true, nullable:true);
}

String toString(){
    "${menuName}";
}

}

and inside the layout/main.gsp I have this code:

                <%
            def currMenu = Menu.findByMenuController(params.controller);
            %>
            <g:if test="${currMenu}">
            <g:if test="${currMenu.isHorizontal}">
            <g:each in="${Menu.findAllByParentMenu(currMenu,[sort:'menuOrder',order:'asc'])}" status='x' var='submenu'>
            <li class='${submenu.menuClass} <% 
            if(submenu.menuController == params.controller){
                // dont hi-light on sign out 
                if(params.controller.equals("auth")){
                }else{
                    out << "selected";
                }
            }
            %>'> 
                    <g:if test="${!submenu.menuController.equals('')}">
                        <g:link controller='${submenu?.menuController}' action='${submenu?.menuAction}'>
                            ${submenu?.menuName} 
                        </g:link>
                    </g:if>
                    <g:else>
                            ${submenu?.menuName} 
                    </g:else>
                </li>
            </g:each>
            </g:if>
            <g:else>
            <%
            // get who is the parent 
            def parentMenu = currMenu.parentMenu ;
            %> 
            <g:each in="${Menu.findAllByParentMenu(parentMenu,[sort:'menuOrder',order:'asc'])}" status='x' var='submenu'>
            <li class='${submenu.menuClass} <% 
            if(submenu.menuController == params.controller){
                // dont hi-light on sign out 
                if(params.controller.equals("auth")){
                }else{
                    out << "selected";
                }
            }
            %>'> 
                <g:if test="${!submenu.menuController.equals('')}">
                    <g:link controller='${submenu?.menuController}' action='${submenu?.menuAction}'>
                        ${submenu?.menuName} 
                    </g:link>
                </g:if>
                <g:else>
                        ${submenu?.menuName} 
                </g:else>
            </li>
            </g:each>
            </g:else>
            </g:if>
        </ul>
        </g:if>

How to fix this bug ? I can restart the grails and it work as normal.. but I just get curious how to fix this

+1  A: 

Your bug seems related to stale connections when the application breaks after every few hours of inactivity.

See http://sacharya.com/grails-dbcp-stale-connections/ for the description and the solution of this issue.

Fabien.

fabien7474