3 Replies Latest reply on Oct 11, 2005 12:06 PM by Claudia Rego

    ERROR EJB connecting DB throw datasource

    Claudia Rego Newbie

      Hello people!
      I need help :(
      I have a sateless session bean and i want access a database.
      For that i already did:
      1) Create a new datasource
      Copied jar files from JDBC (SQL Server) Driver to C:\jboss-4.0.2\server\default\lib

      Create mssql-ds.xml in the folder C:\jboss-4.0.2\server\default\deploy

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

      <local-tx-datasource>
      <jndi-name>MSSQLDS</jndi-name>
      <connection-url>jdbc:microsoft:sqlserver://192.168.193.52:1433;DatabaseName=aveiro_aguas_2</connection-url>
      <driver-class>com.microsoft.jdbc.sqlserver.SQLServerDriver</driver-class>
      <user-name>sa</user-name>



      <type-mapping>MS SQLSERVER2000</type-mapping>


      </local-tx-datasource>


      In file C:\jboss-4.0.2\server\default\conf\standardjaws.xml i configured the following elements:



      java:/MSSQLDS
      <type-mapping>MS SQLServer2000</type-mapping>


      2) I tested my datasource with a jsp and it is working fine!!!

      3) For my EJB access the same database i wrote the following bean:

      package comtwo;

      import java.util.*;
      import javax.ejb.*;
      import javax.ejb.CreateException;
      import javax.naming.InitialContext;
      import javax.naming.NamingException;
      import javax.sql.DataSource;
      import java.sql.*;


      /**
      *
      * <!-- begin-user-doc -->
      * A generated session bean
      * <!-- end-user-doc -->
      * *
      * <!-- begin-xdoclet-definition -->
      * @ejb.bean name="My2"
      * description="A session bean named My2"
      * display-name="My2"
      * jndi-name="My2"
      * type="Stateless"
      * transaction-type="Container"
      *
      * <!-- end-xdoclet-definition -->
      * @generated
      */

      public abstract class My2Bean implements javax.ejb.SessionBean {

      /**
      *
      * <!-- begin-xdoclet-definition -->
      * @ejb.create-method view-type="remote"
      * <!-- end-xdoclet-definition -->
      * @generated
      *
      * //TODO: Must provide implementation for bean create stub
      */
      public void ejbCreate() throws CreateException{
      try {
      makeConnection();
      } catch (Exception ex) {
      throw new CreateException(ex.getMessage());
      }
      }

      /**
      *
      * <!-- begin-xdoclet-definition -->
      * @ejb.create-method view-type="remote"
      * <!-- end-xdoclet-definition -->
      * @generated
      *
      * //TODO: Must provide implementation for bean create stub
      */
      public void ejbRemove() {

      try {
      con.close();
      } catch (SQLException ex) {
      throw new EJBException(ex.getMessage());
      }
      }


      /**
      *
      * <!-- begin-xdoclet-definition -->
      * @ejb.interface-method view-type="remote"
      * <!-- end-xdoclet-definition -->
      * @generated
      *
      * //TODO: Must provide implementation for bean method stub
      */
      public String say2() {
      return "TWO";
      }


      /**
      *
      * <!-- begin-xdoclet-definition -->
      * @ejb.create-method view-type="remote"
      * <!-- end-xdoclet-definition -->
      * @generated
      *
      * //TODO: Must provide implementation for bean create stub
      */
      public My2Bean() {}




      //DataBase related Routines
      DataSource ds = null;
      Connection con = null;
      // PreparedStatement pr = null;
      Statement st = null;
      String dsJNDI = "java:/MSSQLDS";

      InitialContext ic;


      /**
      *
      * <!-- begin-xdoclet-definition -->
      * @ejb.create-method view-type="remote"
      * <!-- end-xdoclet-definition -->
      * @generated
      *
      * //TODO: Must provide implementation for bean create stub
      */

      private void makeConnection()throws NamingException, SQLException {

      ic = new InitialContext();
      ds = (DataSource) ic.lookup(dsJNDI);
      con = ds.getConnection();
      }

      }


      4) My deployment descriptor is

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

      <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">

      <ejb-jar >

      <![CDATA[MyEJBTest2 generated by eclipse wtp xdoclet extension.]]>
      <display-name>MyEJBTest2</display-name>

      <enterprise-beans>

      <!-- Session Beans -->

      <![CDATA[A session bean named My2]]>
      <display-name>My2</display-name>

      <ejb-name>My2</ejb-name>

      comtwo.My2Home
      comtwo.My2
      <local-home>comtwo.My2LocalHome</local-home>
      comtwo.My2Local
      <ejb-class>comtwo.My2Session</ejb-class>
      <session-type>Stateless</session-type>
      <transaction-type>Container</transaction-type>




      </enterprise-beans>

      <ejb-client-jar>MyEJBTest2Client.jar</ejb-client-jar>

      </ejb-jar>


      5) I create a new file named jboss.xml in META-INF

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


      <enterprise-beans>

      <ejb-name>My2</ejb-name>
      <jndi-name>My2</jndi-name>
      <resource-ref>
      <res-ref-name>jdbc/MSSQLDS</res-ref-name>
      <jndi-name>java:/MSSQLDS</jndi-name>
      </resource-ref>


      </enterprise-beans>


      6) When i made depoly i get ERROR!!!

      6:17:10,523 ERROR [MainDeployer] could not create deployment: file:/C:/jboss-4.0.2/server/default/tmp/deploy/tmp43901MyEJBTest2EAR.ear-contents/MyEJBTest2.jar
      org.jboss.deployment.DeploymentException: Error in jboss.xml for Bean null: expected one ejb-name tag



      Please help me...
      What it is wrong???

      I'm using JBoss4.0.2 + WTP + XDoclet1.2.3 + Eclipse3.1

      Thankxx
      CLÁUDIA

        • 2. Re: ERROR EJB connecting DB throw datasource
          Darran Lofthouse Master

          I think your XML is invalid but as you have not wrapped it in [ code ][ /code ] tags without the spaces it is difficult to give you any constructive feedback.

          • 3. Re: ERROR EJB connecting DB throw datasource
            Claudia Rego Newbie

            OK!

            The datasource is working ok! I can access DB throw a JSP page.
            The problem is when i want access DB throw the session bean.

            My META-INF has jboss.xml:

            <?xml version="1.0" encoding="UTF-8"?>
            
            <jboss>
             <enterprise-beans>
             <session>
             <ejb-name>My2</ejb-name>
             <jndi-name>My2</jndi-name>
             <resource-ref>
             <res-ref-name>jdbc/MSSQLDS</res-ref-name>
             <jndi-name>java:/MSSQLDS</jndi-name>
             </resource-ref>
            
             </session>
             </enterprise-beans>
            </jboss>
            


            Also i have ejb-jar.xml:

            <?xml version="1.0" encoding="UTF-8"?>
            
            <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" "http://java.sun.com/dtd/ejb-jar_2_0.dtd">
            
            <ejb-jar >
            
             <description><![CDATA[MyEJBTest2 generated by eclipse wtp xdoclet extension.]]></description>
             <display-name>MyEJBTest2</display-name>
            
             <enterprise-beans>
            
             <!-- Session Beans -->
             <session >
             <description><![CDATA[A session bean named My2]]></description>
             <display-name>My2</display-name>
            
             <ejb-name>My2</ejb-name>
            
             <home>comtwo.My2Home</home>
             <remote>comtwo.My2</remote>
             <local-home>comtwo.My2LocalHome</local-home>
             <local>comtwo.My2Local</local>
             <ejb-class>comtwo.My2Session</ejb-class>
             <session-type>Stateless</session-type>
             <transaction-type>Container</transaction-type>
            
             <resource-ref>
             <description>O meu ejb</description>
             <res-ref-name>jdbc/MSSQLDS</res-ref-name>
             <res-type>javax.sql.DataSource</res-type>
             <res-auth>Container</res-auth>
             </resource-ref>
             </session>
            
            
             </enterprise-beans>
            
             <ejb-client-jar>MyEJBTest2Client.jar</ejb-client-jar>
            
            </ejb-jar>
            


            My stateless session bean:
            /**
             *
             */
            package comtwo;
            
            import java.util.*;
            import javax.ejb.*;
            import javax.ejb.CreateException;
            import javax.naming.InitialContext;
            import javax.naming.NamingException;
            import javax.sql.DataSource;
            import java.sql.*;
            
            
            /**
             *
             * <!-- begin-user-doc -->
             * A generated session bean
             * <!-- end-user-doc -->
             * *
             * <!-- begin-xdoclet-definition -->
             * @ejb.bean name="My2"
             * description="A session bean named My2"
             * display-name="My2"
             * jndi-name="My2"
             * type="Stateless"
             * transaction-type="Container"
             *
             * <!-- end-xdoclet-definition -->
             * @generated
             */
            
            public abstract class My2Bean implements javax.ejb.SessionBean {
            
             /**
             *
             * <!-- begin-xdoclet-definition -->
             * @ejb.create-method view-type="remote"
             * <!-- end-xdoclet-definition -->
             * @generated
             *
             * //TODO: Must provide implementation for bean create stub
             */
             public void ejbCreate() throws CreateException{
             try {
             makeConnection();
             } catch (Exception ex) {
             throw new CreateException(ex.getMessage());
             }
             }
            
             /**
             *
             * <!-- begin-xdoclet-definition -->
             * @ejb.create-method view-type="remote"
             * <!-- end-xdoclet-definition -->
             * @generated
             *
             * //TODO: Must provide implementation for bean create stub
             */
             public void ejbRemove() {
            
             try {
             con.close();
             } catch (SQLException ex) {
             throw new EJBException(ex.getMessage());
             }
             }
            
            
             /**
             *
             * <!-- begin-xdoclet-definition -->
             * @ejb.interface-method view-type="remote"
             * <!-- end-xdoclet-definition -->
             * @generated
             *
             * //TODO: Must provide implementation for bean method stub
             */
             public String say2() {
             return "TWO";
             }
            
            
             /**
             *
             * <!-- begin-xdoclet-definition -->
             * @ejb.create-method view-type="remote"
             * <!-- end-xdoclet-definition -->
             * @generated
             *
             * //TODO: Must provide implementation for bean create stub
             */
             public My2Bean() {}
            
            
            
            // ------------------------------------------------------------------------------
             //DataBase related Routines
             DataSource ds = null;
             Connection con = null;
            // PreparedStatement pr = null;
             Statement st = null;
             String dsJNDI = "java:/MSSQLDS";
            
             InitialContext ic;
            
            
             /**
             *
             * <!-- begin-xdoclet-definition -->
             * @ejb.create-method view-type="remote"
             * <!-- end-xdoclet-definition -->
             * @generated
             *
             * //TODO: Must provide implementation for bean create stub
             */
            
             private void makeConnection()throws NamingException, SQLException {
            
             ic = new InitialContext();
             ds = (DataSource) ic.lookup(dsJNDI);
             con = ds.getConnection();
             }
            
            }
            
            


            Please give some help!!
            Thanks,
            Claudia