2 Replies Latest reply on Aug 6, 2002 2:09 PM by Dain Sundstrom

    DataSource Binding

    goolam Newbie

      i am attempting to deploy a datasource object to jboss-tomcat.
      however context.lookup() always returns null

      the complete compilable source is listed below.
      any suggestions

      import java.sql.*;
      import java.io.*;
      import java.text.*;
      import javax.sql.*;
      import javax.naming.*;
      import java.util.*;
      import com.microsoft.jdbcx.sqlserver.*;
      public class Intro {
      private DataSource dataSource= null;
      //----------------//
      //--Creation------//
      //----------------//
      public Intro() throws NamingException {
      initialize();
      }
      public Intro(String source) throws NamingException {
      initialize(source);
      }
      public void initialize() throws NamingException {
      initialize("History");
      }
      public void initialize(String source) throws NamingException {
      // Create Naming Context
      Context context= MyContext.getContext();
      // Get DataSource
      Object obj= context.lookup(source);
      if (obj != null)
      System.out.println("Object Class - " + obj.getClass());
      else
      System.out.println("lookup returned null");
      //dataSource= (DataSource)
      /* post-condition
      // the history database is now referable via dataSource
      */
      }
      //----------------//
      //--General-------//
      //----------------//
      public Connection getConnection() throws SQLException {
      /* pre-condition
      // the dataSource field is initialized
      */
      return dataSource.getConnection();
      }
      //----------------//
      //--Debugging-----//
      //----------------//
      public static void main(String[] args) {
      try {
      // create a DataSource
      SQLServerDataSource x= new SQLServerDataSource();
      x.setServerName("localhost");
      x.setDatabaseName("History");
      x.setDescription("Maintains instrument info change history");
      x.setDataSourceName("History");
      x.setUser("goolamr");
      x.setPassword("password");

      // deploy the datasource
      try {
      Context context= MyContext.getContext();
      context.addToEnvironment("History", x);
      context.bind("History", x);
      System.out.println(x.toString() + " binded to History");
      }
      catch (NamingException ne) {
      System.out.println("Failed to create the context or bind");
      System.out.println("Caught Exception: " + ne.getMessage());
      ne.printStackTrace();
      }
      //attempt to get the DataSource
      System.out.println("Called Intro() Constructor");
      Intro test= new Intro("/History");
      System.out.println("Intro() initialization completed");
      }
      catch (Throwable e) {
      System.out.println("Caught Exception: " + e.getMessage());
      e.printStackTrace();
      }
      // undeploy the DataSource
      finally {
      try {
      Context context= MyContext.getContext();
      context.unbind("History");
      System.out.println("History unbounded");
      }
      catch (NamingException ne) {
      System.out.println("Failed to create the context or bind");
      System.out.println("Caught Exception: " + ne.getMessage());
      ne.printStackTrace();
      }
      }
      }
      }