Rafael Rocha
Rafael Rocha

Reputation: 79

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?

Upvotes: 0

Views: 394

Answers (2)

Roman Grigoriadi
Roman Grigoriadi

Reputation: 1928

You can also mark your GenericCrudService field transient and initialize instance in Component onAttach/onConfigure method. When using Spring there is a dedicated annotation @SpringBean in Wicket, which will make your non-serializable service field a proxy and handle instance setting for you.

Upvotes: 0

tetsuo
tetsuo

Reputation: 10896

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.

Upvotes: 1

Related Questions