3 Replies Latest reply on Oct 26, 2007 1:13 PM by shelly.mcgowan

    Refering to a datasource wih its ENC name

    marcusdidiusfalco

      Hallo!

      I have a datsource

      <datasources>
       <local-tx-datasource>
       <jndi-name>FencingDS</jndi-name>
       <connection-url>jdbc:mysql://localhost:3306/fencing</connection-url>
       .....
       </local-tx-datasource>
      </datasources>


      deployed. Using the JMX console it shows up in the JNDI java: Namespace under FencingDS.

      So I want to get a connection from it in an EJB
      ctx = new InitialContext();
      DataSource ds = (DataSource) ctx.lookuk("java:/comp/env/jdbc/FencingDS");
      Connection con = (Connection) ds.getConnection();


      ejb-jar.xml:
      <ejb-jar>
       <enterprise-beans>
       <session>
       <description>TestBean </description>
       <ejb-name>TestBean</ejb-name>
       <resource-ref>
       <description>Beschreibung der Resourcen</description>
       <res-ref-name>jdbc/FencingDS</res-ref-name>
       <res-type>javax.sql.DataSource</res-type>
       <res-auth>Container</res-auth>
       </resource-ref>
       </session>
       </enterprise-beans>
      </ejb-jar>


      In jboss.xml I try to map the global JNDI name to the ENC name:
      <jboss>
       <enterprise-beans>
       <session>
       <ejb-name>TestBean</ejb-name>
       <resource-ref>
       <res-ref-name>jdbc/FencingDS</res-ref-name>
       <resource-name>java:FencingDS</resource-name>
       </resource-ref>
       </session>
       </enterprise-beans>
      </jboss>


      But when I try to deploy the jar I always get an Exception
      java.lang.RuntimeException: mapped-name is required for jdbc/FencingDS of deployment TestBean
      and the EJB is not bound.

      I have so far not found a reference how to do this right.

      I would greatly appreciate any help.

      Thanks,

      Hans

        • 1. Re: Refering to a datasource wih its ENC name
          marcusdidiusfalco

          OK found out the error myself:
          jboss.xml should be

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

          (I did not use the jndi-name element)

          • 2. Re: Refering to a datasource wih its ENC name
            marcusdidiusfalco

            Ok the Bean is now deployed, but it seems I can get a connection from the datasource, but somehow I cannot work with it:

            ctx = new InitialContext();
            DataSource ds = (DataSource) ctx.lookup("java:/comp/env/jdbc/FencingDS");
            System.out.println(ds.toString());
            Connection con = (Connection) ds.getConnection();
            System.out.println("Connection is closed: " + con.isClosed());
            System.out.println(con.toString() + " " + con.getCatalog());
            Statement stmt = con.createStatement();
            ResultSet resultSet = stmt.executeQuery("SELECT * FROM fencer");
            while (resultSet.next()) {
             System.out.println(resultSet.getString("firstName"));
             System.out.println(resultSet.getString("lastName"));
            }


            I know that the Select should work, because I have copied it from thy MySQL console.

            13:41:21,109 INFO [EJB3Deployer] Deployed: file:/C:/jboss-4.2.1.GA/server/defau
            lt/deploy/GeneralTest.jar
            13:41:25,656 INFO [STDOUT] org.jboss.resource.adapter.jdbc.WrapperDataSource@13
            dc4d5
            13:41:25,656 INFO [STDOUT] Connection is closed: false
            13:41:25,656 INFO [STDOUT] org.jboss.resource.adapter.jdbc.WrappedConnection@17
            b650a null
            13:41:25,656 ERROR [STDERR] java.sql.SQLException: Table not found in statement
            [SELECT * FROM fencer]


            When I change the lookup to
            DataSource ds = (DataSource) ctx.lookup("java:FencingDS");

            everything works fine.
            So I guess, I still have some problem with the mapping of the jndi-Names.
            Can somebody help me out with this problem please?

            Thanks,

            Hans

            • 3. Re: Refering to a datasource wih its ENC name
              shelly.mcgowan

              From the trace it looks like the Tables were not created in the DB.