5 Replies Latest reply on Jan 31, 2002 5:09 AM by alcek

    Database access

    alcek

      Hello to every body.
      I'm just learning something about EJB's and I have had my first test with a Session Bean that access directly to a database, the jboss.jcml entry for the Oracle 8i database is:


      oracle.jdbc.driver.OracleDriver



      OracleDB
      org.opentools.minerva.jdbc.xa.wrapper.XADataSourceImpl
      jdbc:oracle:thin:@virtualc1:1521:mtst8i
      scott
      tiger


      The run.bat result
      [JdbcProvider] Initializing
      [JdbcProvider] Loaded JDBC-driver:oracle.jdbc.driver.OracleDriver
      [JdbcProvider] Initialized

      My META-INF/ejb.jar.xml

      <?xml version="1.0" encoding="UTF-8" ?>
      <ejb-jar>
      Seleccio Taula Emp
      <display-name>SeleccioEmp</display-name>
      <enterprise-beans>

      <ejb-name>SeleccioEmp</ejb-name>
      SelectEmpHome
      SelectEmp
      <ejb-class>SelectEmpBean</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Bean</transaction-type>

      </enterprise-beans>
      </ejb-jar>

      This is the remote interface:

      import javax.ejb.EJBObject;
      import java.rmi.RemoteException;
      public interface SelectEmp extends EJBObject {
      public int recuperarEmp() throws RemoteException;
      }

      The home interface

      import javax.ejb.EJBHome;
      import javax.ejb.CreateException;
      import java.rmi.RemoteException;
      public interface SelectEmpHome extends EJBHome {
      SelectEmp create() throws RemoteException, CreateException;
      }

      The Bean

      import javax.ejb.SessionBean;
      import javax.ejb.SessionContext;
      import java.sql.*;
      import javax.sql.DataSource;
      import javax.naming.InitialContext;

      public class SelectEmpBean implements SessionBean {
      public static String recuperarEmp(){
      System.out.println("Antes de asignar valor a la variable");
      int result = 0;
      try{
      InitialContext ctx = new InitialContext();
      javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/DefaultDS") ;
      java.sql.Connection conn = ds.getConnection() ;
      Statement stmt = conn.createStatement();
      ResultSet rs = stmt.executeQuery("SELECT EMPNO FROM EMP WHERE EMPNO = 37");
      rs.next();
      result = rs.getInt("EMPNO");
      rs.close();
      stmt.close();


      } catch(Exception e) {System.out.println(e.toString());}
      return result;
      }
      public SelectEmpBean() {}
      public void ejbCreate() {}
      public void ejbRemove() {}
      public void ejbActivate() {}
      public void ejbPassivate() {}
      public void setSessionContext(SessionContext sc) {}
      }

      The caller client program:

      import java.util.Properties;
      import javax.rmi.PortableRemoteObject;
      import javax.naming.*;
      class ClientSeleccioEmp
      {
      public static void main(String[] args)
      {

      try
      {
      Properties env = new Properties();
      env.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
      env.setProperty("java.naming.provider.url", "brahms:1099");
      env.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
      InitialContext jndiContext = new InitialContext(env);
      System.out.println("Contexto Disponible");
      Object ref = jndiContext.lookup("SeleccioEmp");
      System.out.println("Se encontro Referencia del EJB!");
      SelectEmpHome home = (SelectEmpHome) PortableRemoteObject.narrow (ref, SelectEmpHome.class);
      SelectEmp empno = home.create();
      System.out.println(empno.recuperarEmp());
      }
      catch(Exception e)
      {
      System.out.println(e.toString());
      }
      }
      }

      The screen output:

      javax.naming.NameNotFoundException: jdbc not bound

      I have seen a topic that that looks like, but there someone talk about two files (jboss.xml & jboss-web.xml), I have searching in every where of JBoss Home, but I'm not able to find it. I didn't find where they must to be in the JBoss Administration Book. Please I need some help.

        • 1. Re: Database access
          tclouser

          Alcek,

          You will want to look out how to use the ejb-jar.xml and the jboss.xml as they are the deployment descriptor used by the EJB container (the web.xml and the jboss-web.xml are deployment descriptors specific to the servlet container). From the information provided you:

          Have not provided a mapping from java:comp/env/jdbc/DefaultDS to the bound resource OracleDB

          or

          You are just using the incorrect name for the resource.

          HTH,

          TC

          • 2. Re: Database access
            alcek

            Thanks, tclouser for your answer, have been very helpfully, now I think I'm closer at solution.
            However I haven't achieved my scope, now I've configured my ejb-jar.xml file, and also I have added a new jboss.xml file.

            I made some changes to the jboss.jcml configuration:


            oracle.jdbc.driver.OracleDriver


            DefaultDS
            org.opentools.minerva.jdbc.xa.wrapper.XADataSourceImpl
            jdbc:oracle:thin:@virtualc1:1521:mtst8i

            scott
            tiger
            2
            2
            true
            20
            20
            false
            false
            true
            false
            false
            1800000
            1.0


            The META-INF/ejb-jar.xml file looks like:

            <ejb-jar>
            Seleccio Taula Emp
            <display-name>SeleccioEmp</display-name>
            <enterprise-beans>

            <ejb-name>SeleccioEmp</ejb-name>
            SelectEmpHome
            SelectEmp
            <ejb-class>SelectEmpBean</ejb-class>
            <session-type>Stateless</session-type>
            <transaction-type>Bean</transaction-type>
            <resource-ref>
            Base de dades Oracle
            <res-ref-name>jdbc/DefaultDS</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
            <res-auth>Container</res-auth>
            </resource-ref>

            </enterprise-beans>
            </ejb-jar>

            The META-INF/jboss.xml configuration:


            <enterprise-beans>

            <ejb-name>SeleccioEmp</ejb-name>
            <resource-ref>
            <res-ref-name>jdbc/DefaultDS</res-ref-name>
            <resource-name>DefaultDS</resource-name>
            </resource-ref>

            </enterprise-beans>
            <resource-managers>
            <resource-manager res-class="org.jboss.jdbc.XADataSourceLoader">
            <res-name>DefaultDS</res-name>
            <res-jndi-name>java:/DefaultDS</res-jndi-name>
            </resource-manager>
            </resource-managers>


            The home interface:

            import javax.ejb.EJBHome;
            import javax.ejb.CreateException;
            import java.rmi.RemoteException;
            //Home Interface
            public interface SelectEmpHome extends EJBHome {
            SelectEmp create() throws RemoteException, CreateException;
            }

            The remote interface:

            import javax.ejb.EJBObject;
            import java.rmi.RemoteException;
            //Remote interface
            public interface SelectEmp extends EJBObject {
            public String recuperarEmp() throws RemoteException;
            }

            The bean:

            import java.sql.*;
            import javax.sql.DataSource;
            import javax.naming.InitialContext;
            public class SelectEmpBean implements SessionBean {
            public static String recuperarEmp(){
            String result = "ko";
            try{
            InitialContext ctx = new InitialContext();
            javax.sql.DataSource ds = (javax.sql.DataSource) ctx.lookup("java:comp/env/jdbc/DefaultDS");
            result = "ok";
            java.sql.Connection conn = ds.getConnection() ;
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT EMPNO FROM EMP WHERE EMPNO = 37");
            rs.next();
            rs.close();
            stmt.close();
            } catch(Exception e) {System.out.println(e.toString());
            result = result + " " + e.toString();
            }
            return result ;
            }
            public SelectEmpBean() {}
            public void ejbCreate() {}
            public void ejbRemove() {}
            public void ejbActivate() {}
            public void ejbPassivate() {}
            public void setSessionContext(SessionContext sc) {}
            }

            The client:

            import java.util.Properties;
            import javax.rmi.PortableRemoteObject;
            import javax.naming.*;
            class ClientSeleccioEmp
            {
            public static void main(String[] args)
            {
            try
            {
            Properties env = new Properties();
            env.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
            env.setProperty("java.naming.provider.url", "brahms:1099");
            env.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming");
            InitialContext jndiContext = new InitialContext(env);
            System.out.println("Context Available");
            Object ref = jndiContext.lookup("SeleccioEmp");
            System.out.println("EJB reference finded!!");
            SelectEmpHome home = (SelectEmpHome) PortableRemoteObject.narrow (ref, SelectEmpHome.class);
            SelectEmp empno = home.create();
            System.out.println(empno.recuperarEmp());
            }
            catch(Exception e)
            {
            System.out.println(e.toString());
            }
            }
            }

            The command line output:

            Context Available
            EJB reference finded!!
            ko javax.naming.NameNotFoundException: DefaultDS not bound


            I think I didn't forget anything.

            What it could be happening?, I have not configured the standardjboss.xml file for this bean, it is necessary?.

            Thanks you in advance.

            • 3. Re: Database access
              tclouser

              Alcek,

              I believe the problem is in your resouce-manager element definition. Try this...

              <resource-manager res-class="">

              see http://www.jboss.org/online-manual/HTML/ch07s02.html for more information.

              HTH,

              TC




              • 4. Re: Database access
                alcek

                Hi,

                I have changed the <resource-manager res-class=""> in jboss.xml file.

                I get the same wrong result.

                I don't know why but, I think like you, I am convinced that it is a problem in the configuration files, there are something wrong?

                Thanks a lot.

                • 5. Re: Database access
                  alcek

                  Hi to everybody.

                  Thanks to a friend I have changed my jboss.xml because it seems that there are some errors, now this is my jboss.xml

                  <?xml version="1.0" encoding="UTF-8"?>

                  false
                  <container-configurations />
                  <enterprise-beans>

                  <ejb-name>SeleccioEmp</ejb-name>
                  <jndi-name>SeleccioEmp</jndi-name>
                  <configuration-name></configuration-name>
                  <resource-ref>
                  <res-ref-name>jdbc/dsoracle</res-ref-name>
                  <resource-name>resdsoracle</resource-name>
                  </resource-ref>

                  </enterprise-beans>
                  <resource-managers>
                  <resource-manager res-class="org.jboss.ejb.deployment.JDBCResource">
                  <res-name>resdsoracle</res-name>
                  <res-jndi-name>java:comp/env/jdbc/dsoracle</res-jndi-name>
                  </resource-manager>
                  </resource-managers>


                  Unfortunately, the error now had changed, can somebody tell me where I can find a description for the error?

                  Context Available
                  EJB reference finded!!
                  java.rmi.ServerException: RemoteException occurred in server thread; nested exce
                  ption is:
                  java.rmi.ServerError: Transaction rolled back; nested exception is:
                  java.lang.StackOverflowError