Search code examples
ejbwicketjndi

wicket serialization JNDI


I'm using Wicket with EJB 3, when I call my page, the log show me a error

Error serializing object class com.mk.view.page.CountryList [object=[Page class = com.mk.view.page.CountryList, id = 91, render count = 1]]
org.apache.wicket.core.util.objects.checker.CheckingObjectOutputStream$ObjectCheckException: The object type is not Serializable!
A problem occurred while checking object with type: javax.naming.InitialContext
Field hierarchy is:
  91 [class=com.mk.view.page.CountryList, path=91]
    private javax.naming.Context com.mk.view.page.CountryList.ctx [class=javax.naming.InitialContext] <----- field that is causing the problem

My code is

public class CountryList extends Layout {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    /**
     * 
     */


    private javax.naming.Context ctx;
    private GenericCrudService sf;



    private CountryList(){      
        try {

                ctx = new javax.naming.InitialContext();
                sf = (GenericCrudService) ctx
                        .lookup("java:global/mkEar/mkEJB/CrudService!com.mk.business.common.GenericCrudService");

        } catch (NamingException e) {
            e.printStackTrace();
        }

        addModelModule();
        addSearchModule();
    }

My app doesn't crash, I haven't found solution for these so I wonder if these would get worse than a log? Anyone know solve this?


Solution

  • Don't keep a reference to GenericCrudService as a field (nor the InitialContext instance). Refactor the lookup code to a method, and retrieve it every time you need the service. You can reuse it, but keep it to local (variables) or request scope.

    AFAIK, there's no guarantee that EJBs returned from a JNDI lookup will be serializable, even if they implement the interface and follow the serialization rules. The container will probably return a proxy, not the object instance directly.

    Since Wicket will serialize stateful pages after requests, they can't have non-serializable attributes. This is the cause of the error you're getting.