2 Replies Latest reply on Dec 17, 2007 11:47 AM by Peter Johnson

    Database JNDI call in EJB

    Antonio Broughton Newbie


      I am fairly new to JBoss, so I am possibly missing something.

      I have a simple EJB, which just sets up a Connection to a JNDI datasource, then closes the connection.

      try {
       Context loContext = (Context) new InitialContext().lookup("java:");
       DataSource loDataSource = (DataSource)loContext.lookup("Prophet_Data");
       Connection loConnection = loDataSource.getConnection();
       try {
       } finally {
       if (!(loConnection == null)) {
       } catch (Exception e) {

      The JNDI Prophet_Data is defined within a seperate application, which is packaged into its own EAR (Which is Pentaho).

      If I deploy this EJB jar to JBoss, then call the EJB within a simple jsp:
      Properties props = new Properties();
       props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
       props.put(Context.PROVIDER_URL, "localhost:1099");
       Context ctx = new InitialContext(props);
       HelloHome home = (HelloHome)ctx.lookup("ejb/Hello");
       Hello bean = home.create();

      When browsing to the jsp (eg: http://localhost:8080/testejb/testejb.jsp), it works fine.

      However, If I modify the EJB code (and just add say, the initialisation of an integer eg:
      int i = 0;

      Then I redeploy the EJB jar... when I goto http://localhost:8080/testejb/testejb.jsp, it causes an exception to occur:
      java.rmi.ServerException: EJBException:; nested exception is:
       javax.ejb.EJBException: Invalid invocation, check your deployment packaging, method=public abstract nz.co.mcpond.test.ejb.helloejb.Hello nz.co.mcpond.test.ejb.helloejb.HelloHome.create() throws javax.ejb.CreateException,java.rmi.RemoteException
       sun.reflect.GeneratedMethodAccessor402.invoke(Unknown Source)...

      I can get around this by setting, CallByValue to true, however, reading http://wiki.jboss.org/wiki/Wiki.jsp?page=ClassLoadingConfiguration seems to suggest that this is a performance hit.

      Am I doing something wrong? or not setting something correctly?

      Is this because the JNDI datasource is not defined within my EJB jar? (ie: because it is defined within Pentaho.ear, does this mean that only that application can use the JNDI (without using CallByValue??)

      Any help would be appreciated

      Antonio Broughton

        • 1. Re: Database JNDI call in EJB
          Antonio Broughton Newbie

          To the war file (containing the jsp file), I added a resource-ref element to web.xml and jboss-web.xml:


           <description>Prophet Cube Data</description>


          I also found, that when I modify the EJB jar, I need to also re-deploy the WAR file.

          I forgot to mention that I was deploying _two_ deployment files:
          * jar - EJB code
          * war - jsp code

          Is this the proper practice for deploying EJBs? That you also need to re-deploy the war / client files also? (some sort of re-freshing?)

          • 2. Re: Database JNDI call in EJB
            Peter Johnson Master

            Your changes to web.xml and jboss-web.xml to enable datasource lookup are correct.

            And redeploying the war after the ear also makes sense - otherwise the classes in the war could still be referencing classes in the old ear.