0 Replies Latest reply: Jan 14, 2011 8:23 AM by 831264 RSS

    NullPointerException using @Resource for JDBC lookup

    831264
      I had the following working and am trying to convert to using @Resource and have been unsuccessful:

      First I had the following working, inside a jar within an ear deployed to glassfish v3 and no sun-ejb-jar.xml file:

      public CM
      {
      public CM() {
      InitialContext ctx = new InitialContext();
      DataSource ds = (DataSource)ctx.lookup("jdbc/xDataSource");
      Connection conn = ds.getConnection();
      }
      }

      When I switched to the following I get a NullPointerException when I deploy, indicating that the resource could not be found
      @Stateful
      public CM
      {
      @Resource(name="jdbc/xDataSource", mappedName="jdbc/xDataSource")
      private DataSource ds;

      public CM() {
      Connection conn = ds.getConnection();
      }
      }

      and added sun-ejb-jar.xml file to META-INF directory
      <?xml version="1.0" encoding="UTF-8"?>
      <!DOCTYPE sun-ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD GlassFish Application Server 3.0 EJB 3.1//EN"
      "http://www.sun.com/software/appserver/dtds/sun-ejb-jar_3_1-0.dtd">
      <sun-ejb-jar>
      <enterprise-beans>
      <ejb>
      <ejb-name>CM</ejb-name>
      <resource-ref>
      <res-ref-name>jdbc/xDataSource</res-ref-name>
      <jndi-name>jdbc/xDataSource</jndi-name>
      </resource-ref>
      </ejb>
      </enterprise-beans>
      </sun-ejb-jar>

      P.S. Sorry about not using code tags I'm not sure which tag to use to properly format it.