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

    Database JNDI call in EJB

    Antonio Broughton Newbie

      Hi,

      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)) {
       loConnection.close();
       }
       }
       } catch (Exception e) {
       e.printStackTrace();
       }


      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();
       bean.simpleFunction();
       bean.remote();
       ctx.close();


      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
       org.jboss.ejb.plugins.LogInterceptor.handleException(LogInterceptor.java:365)
       org.jboss.ejb.plugins.LogInterceptor.invokeHome(LogInterceptor.java:136)
       org.jboss.ejb.plugins.ProxyFactoryFinderInterceptor.invokeHome(ProxyFactoryFinderInterceptor.java:107)
       org.jboss.ejb.SessionContainer.internalInvokeHome(SessionContainer.java:637)
       org.jboss.ejb.Container.invoke(Container.java:981)
       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:

          web.xml

          <resource-ref>
           <description>Prophet Cube Data</description>
           <res-ref-name>jdbc/Prophet_Cube</res-ref-name>
           <res-type>javax.sql.DataSource</res-type>
           <res-auth>Container</res-auth>
          </resource-ref>


          jboss-web.xml:
          <jboss-web>
           <resource-ref>
           <res-ref-name>jdbc/Prophet_Cube</res-ref-name>
           <res-type>javax.sql.DataSource</res-type>
           <jndi-name>java:/Prophet_Cube</jndi-name>
           </resource-ref>
          </jboss-web>


          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.