2 Replies Latest reply on Feb 19, 2014 11:37 AM by max_kuffs

    Error with WildFly, RestEasy and Hibernate

    fheldt

      We've got an error with a simple REST Webservice after switching from JBoss 7.1.1 to WildFly 8.0.0.Final.

       

      It's a simple Entity Object with @ManyToOne connections.

       

        @GET

        @Produces({ MediaType.APPLICATION_JSON })

        @org.jboss.resteasy.annotations.providers.jaxb.Wrapped(element="einkauf")

        @Path("/einkauf/{aun1:[0-9][0-9]*}/{jahr:[0-9][0-9]*}/{abnr:[0-9][0-9]*}")

        @GZIP

        public Einkauf getEinkauf(@PathParam("aun1") short aun1, @PathParam("jahr") short jahr, @PathParam("abnr") String abnr) {

          Einkauf eink = weEJB.getEinkaufItem(aun1, jahr, abnr);

          return eink;

        }

       

      @Entity

      @Table(name = "EINK")

      public class Einkauf implements Serializable {

      ...

        @ManyToOne

        @JoinColumn(name="EKABNR", insertable=false, updatable=false)

        @NotFound(action = NotFoundAction.IGNORE)

        private Baustein baustein;

        public Baustein getBaustein() {

          return baustein;

        }

      ...

      }

       

      WARN  [org.jboss.resteasy.core.ExceptionHandler] (default task-2) Unknown exception while executing GET /eingang/einkauf/9507/2013/3361: org.codehaus.jackson.map.JsonMappingException: Session is closed! (through reference chain: com.mathai.entity.Einkauf["baustein"]->com.mathai.entity.Baustein["fieldHandler"]->org.hibernate.bytecode.instrumentation.internal.javassist.FieldInterceptorImpl["session"]->org.hibernate.internal.SessionImpl["statistics"]->org.hibernate.stat.internal.SessionStatisticsImpl["entityCount"])

      ...

      Caused by: org.hibernate.SessionException: Session is closed!

        at org.hibernate.internal.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:133) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]

        at org.hibernate.internal.SessionImpl.getPersistenceContext(SessionImpl.java:1990) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]

        at org.hibernate.stat.internal.SessionStatisticsImpl.getEntityCount(SessionStatisticsImpl.java:44) [hibernate-core-4.3.1.Final.jar:4.3.1.Final]

        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_51]

        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) [rt.jar:1.7.0_51]

        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.7.0_51]

        at java.lang.reflect.Method.invoke(Method.java:606) [rt.jar:1.7.0_51]

        at org.codehaus.jackson.map.ser.BeanPropertyWriter.get(BeanPropertyWriter.java:483) [jackson-mapper-asl-1.9.13.jar:1.9.13]

        at org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:418) [jackson-mapper-asl-1.9.13.jar:1.9.13]

        at org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) [jackson-mapper-asl-1.9.13.jar:1.9.13]

       

      So looking at the Exception it looks like RestEasy is trying to serialize all the getters (via reflection) until it comes to getBaustein() and then it stumbles upon some Hibernate code attached to the object.

       

      The same REST WebService is doing fine in JBoss 7.1.1! This problem is with nearly every REST WebService using Entities (and not DAOs)

       

      Anyone any idea, what's going on here?

        • 1. Re: Error with WildFly, RestEasy and Hibernate
          smarlow

          I'm not sure of what changed, that leads to this error.  Any idea if all the getters are also serialized when your web service runs on AS 7.1.1?  I'm not sure how you can tell but perhaps you have a way.

           

          Is there more to the exception call stack?  I'm curious what is calling BeanSerializerBase.serializeFields().

          • 2. Re: Error with WildFly, RestEasy and Hibernate
            max_kuffs

            I also played around with entities without dtos and i have to say it is not work it. too much bytecode instrumentation is happening. from lazy loading to any proxy issues. never again. in this point i am not adam biens opinion.

             

            When serializing to json the rest implementation accesses all getters (default behaviour). Hibernate uses a lot of bytecode instrumentation for lazy loading and dirty checks. So if you pass the entity down to the rest layer the transaction is already closed. The proxy objects which may lazy load some data simply can't access the transaction anymore. This leads to tricky errors in the serialization process. In one project such an error was covered by a very missleading jaxb exception.

             

            My advice is to rather create a simple dto or add ignore annotations to ignore this field during the serialization. (I would use a dto)

            You should do this for all proxy objects within the hibernate entities or make sure that they don't have to lazy load anything. (quite a hassle!)