4 Replies Latest reply on May 25, 2006 8:30 PM by bdecoste

    StackOverflowError Looking Up Stateless EJB3 Bean

    michael.litherland

      Hi,

      I'm writing a trivial JSF webapp to test using EJB3 for a business logic and persistence layer. I'm using JBoss 4.0.4.GA installed with EJB3 support on Intel OS X. NetBeans 5.5 beta is helping me build the code, and it's roughly based in part on the trailblazer app.

      Basically on my call to lookup the bean:

      return (calctest.ejb.CalculatorRemote) c.lookup("java:comp/env/ejb/CalculatorBean");


      It throws a number of exceptions. Seems like the root cause is a stack overflow:
      <snip>
      Caused by: java.lang.StackOverflowError
       at java.security.AccessController.doPrivileged(Native Method)
       at com.sun.naming.internal.VersionHelper12.getJndiProperties(VersionHelper12.java:85)
       at com.sun.naming.internal.ResourceManager.getInitialEnvironment(ResourceManager.java:138)
       at javax.naming.InitialContext.init(InitialContext.java:219)
       at javax.naming.InitialContext.<init>(InitialContext.java:197)
       at org.jnp.interfaces.NamingContext.resolveLink(NamingContext.java:1061)
       at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:700)
       at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:716)
       at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:587)
      <snip>
      


      I've seen some other posts related to stack overflows but none seem to address this. Can anybody give me a clue where to start to look to troubleshoot this? What else can I post that would be helpful.

      Thanks much in advance.

        • 1. Re: StackOverflowError Looking Up Stateless EJB3 Bean
          michael.litherland

          I may have an idea why it's failing. I'm deploying the EJBs in a .jar and the web app separately as a .war. Is there something in this setup that could cause a failure like this? Previously I had tried to combine the two into a single .ear, but had other issues so I broke them out to make things more clear for me.

          Thanks,
          Mike

          • 2. Re: StackOverflowError Looking Up Stateless EJB3 Bean
            bdecoste

            You should be able to deploy as separate jar/war and in a single ear.

            Can you post the deployment descriptions and annotated bean classes? Looks like you may have a cyclical jndi link that is causing the overflow.

            • 3. Re: StackOverflowError Looking Up Stateless EJB3 Bean
              michael.litherland

              OK, here are my ejbs, two stateless and en entity:

              package calctest.ejb;
              
              import javax.ejb.Stateless;
              import javax.persistence.EntityManager;
              import javax.persistence.PersistenceContext;
              
              /**
               *
               * @author mike
               */
              @Stateless
              public class CalculatorBean implements CalculatorRemote, CalculatorLocal {
              
               @PersistenceContext
               private EntityManager em;
              
               /** Creates a new instance of CalculatorBean */
               public CalculatorBean() {
               }
              
               public float add(float a, float b) {
               float result = a + b;
               Calculations c = new Calculations(a, b, "add", result);
               em.persist(c);
               return result;
               }
              
               public float subtract(float a, float b) {
               float result = a - b;
               Calculations c = new Calculations(a, b, "subtract", result);
               em.persist(c);
               return result;
               }
              
              }
              


              package calctest.ejb;
              
              import java.util.List;
              
              import javax.ejb.Stateless;
              import javax.persistence.EntityManager;
              import javax.persistence.PersistenceContext;
              
              /**
               *
               * @author mike
               */
              @Stateless
              public class HistoryBean implements HistoryRemote, HistoryLocal {
              
               @PersistenceContext
               private EntityManager em;
              
               /** Creates a new instance of HistoryBean */
               public HistoryBean() {
               }
              
               public List getHistory() {
               return em.createQuery("from Calculations c").getResultList();
               }
              
              }
              


              package calctest.ejb;
              
              import java.io.Serializable;
              import javax.persistence.Column;
              import javax.persistence.Entity;
              import javax.persistence.Id;
              import javax.persistence.Table;
              
              /**
               *
               * @author mike
               */
              @Entity
              @Table(name = "calculations")
              public class Calculations implements Serializable {
              
               @Id
               @Column(name = "id", nullable = false)
               private Integer id;
              
               @Column(name = "floatOne")
               private Float floatOne;
              
               @Column(name = "floatTwo")
               private Float floatTwo;
              
               @Column(name = "operation")
               private String operation;
              
               @Column(name = "result")
               private Float result;
              
               /** Creates a new instance of Calculations */
               public Calculations() {
               }
              
               public Calculations(float floatOne, float floatTwo, String operation, float result) {
               this.floatOne = floatOne;
               this.floatTwo = floatTwo;
               this.operation = operation;
               this.result = result;
               }
              
               public Calculations(Integer id) {
               this.id = id;
               }
              
               public Integer getId() {
               return this.id;
               }
              
               public void setId(Integer id) {
               this.id = id;
               }
              
               public Float getFloatOne() {
               return this.floatOne;
               }
              
               public void setFloatOne(Float floatOne) {
               this.floatOne = floatOne;
               }
              
               public Float getFloatTwo() {
               return this.floatTwo;
               }
              
               public void setFloatTwo(Float floatTwo) {
               this.floatTwo = floatTwo;
               }
              
               public String getOperation() {
               return this.operation;
               }
              
               public void setOperation(String operation) {
               this.operation = operation;
               }
              
               public Float getResult() {
               return this.result;
               }
              
               public void setResult(Float result) {
               this.result = result;
               }
              
               public int hashCode() {
               int hash = 0;
               hash += (this.id != null ? this.id.hashCode() : 0);
               return hash;
               }
              
               public boolean equals(Object object) {
               if (object == null || !this.getClass().equals(object.getClass())) {
               return false;
               }
               Calculations other = (Calculations)object;
               if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) return false;
               return true;
               }
              
               public String toString() {
               //TODO change toString() implementation to return a better display name
               return "" + this.id;
               }
              
              }
              


              I was playing with trying to use @EJB to inject the stateless beans instead of using the direct lookup code (this doesn't work apparently unless you are in a single ear?) so I have things kind of messed up in my deployment descriptor right now. But this is what it looked like before:

              
               <ejb-ref>
               <ejb-ref-name>ejb/CalculatorBean</ejb-ref-name>
               <ejb-ref-type>Session</ejb-ref-type>
               <home>calctest.ejb.CalculatorLocal</home>
               <remote>calctest.ejb.CalculatorRemote</remote>
               </ejb-ref>
               <ejb-ref>
               <ejb-ref-name>ejb/HistoryBean</ejb-ref-name>
               <ejb-ref-type>Session</ejb-ref-type>
               <home>calctest.ejb.HistoryLocal</home>
               <remote>calctest.ejb.HistoryRemote</remote>
               </ejb-ref>
              


              Thanks for the help,
              Mike

              • 4. Re: StackOverflowError Looking Up Stateless EJB3 Bean
                bdecoste

                You can use @EJB injection without an ear, but you need to specify the global jndi binding of the target bean with the mappedName param.

                Do you have a corresponding jboss.xml where the <jndi-name>'s are specified for the <ejb-ref>?