1 Reply Latest reply on May 4, 2007 11:13 AM by jaikiran

    lookup database source weird problem

    joshuayeo

      Hi all ,

      I am using jdk 1.6.0_01 and jboss 4.0.5.GA.
      I have setup jndi lookup in my startup servlet init() method , however it did not lookup the jndi database name at all.
      It always give me error message cannot lookup the jndi name.
      Anyone have any ideas what is happening here ? Thanks

      Below is the startup servlet for my webapps:

      public class AdminStartupServlet extends HttpServlet {

      protected Logger logger = LoggerManager.getLogger();


      public void init(ServletConfig config) throws ServletException {

      super.init(config);
      DatabaseAction databaseAction = ActionManager.getDatabaseAction();

      try{

      databaseAction.process();

      } catch(NamingException ne){
      ne.printStackTrace();
      } catch(Exception ex){
      ex.printStackTrace();
      }

      }
      }

      This is DatabaseAction :

      public class DatabaseAction {

      protected DatabaseAction() {
      }

      public void process() throws Exception

      {


      try {
      OracleDatabaseManager.config();
      } catch(Exception ex) {
      throw new Exception("Fail to config ORACLE database connection.", ex);
      }


      }

      }


      This is the Database Manager to lookup database jndi name


      public class OracleDatabaseManager {

      protected static DataSource oracleDS;


      public static synchronized void config() throws Exception {
      try{
      InitialContext initialContext = new InitialContext();
      oracleDS = (javax.sql.DataSource) initialContext.lookup("jdbc/OracleDS");
      }catch(Exception ex){
      ex.printStackTrace();
      }

      }


      public static Connection getOracleConnection() throws Exception {
      try {
      //InitialContext initialContext = new InitialContext();
      //oracleDS = (javax.sql.DataSource) //initialContext.lookup("jdbc/OracleDS");
      return oracleDS.getConnection();
      } catch (Exception ex) {
      throw new Exception(ex);
      }
      }

      }

      web.xml resource-ref tag

      <resource-ref>
      Oracle DB Connection
      <res-ref-name>jdbc/OracleDS</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
      </resource-ref>

      jboss-web.xml tag

      <?xml version="1.0" encoding="UTF-8"?>
      <jboss-web>
      jboss.jca:service=LocalTxCM,name=jdbc/OracleDS
      <context-root>/</context->
      <resource-ref>
      <res-ref-name>jdbc/OracleDS</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <jndi-name>jdbc/OracleDS</jndi-name>
      </resource-ref>
      </jboss-web>

        • 1. Re: lookup database source weird problem
          jaikiran

           

          oracleDS = (javax.sql.DataSource) initialContext.lookup("jdbc/OracleDS");


          This should be

          oracleDS = (javax.sql.DataSource) initialContext.lookup("java:comp/env/jdbc/OracleDS");


          Also,

          <resource-ref>
          <res-ref-name>jdbc/OracleDS</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <jndi-name>jdbc/OracleDS</jndi-name>
          </resource-ref>


          The jndi-name here doesnt look right to me. Most of the times it is java:/OracleDS. So try using the following:


          <resource-ref>
          <res-ref-name>jdbc/OracleDS</res-ref-name>
          <res-type>javax.sql.DataSource</res-type>
          <jndi-name>java:/OracleDS</jndi-name>
          </resource-ref>
          


          If that doesnt work, post the error message that you are seeing and also the *-ds.xml file which you are using for configuring the datasource