4 Replies Latest reply on Mar 13, 2004 2:40 PM by neptune5

    How to get the name of the datasource from within an EJB ?

    neptune5 Newbie

      Hi all

      Is there a jboss class somewhere where you can find out the name of the datasource from within the ejb ?.

      Its to replace a sql statement " select max(id) from orderhead" because max can't be used from an ejbSelect method. And if I wanted to change the database I don't want to have to change and recompile the program every time.

      Regards Adrian M

        • 1. Re: How to get the name of the datasource from within an EJB
          Larry Newbie

          Adrian,
          I don't know if there's a Jboss way to do this, but I use getMetaData. It works in any JDBC application. For example, assume con is your connection object:

          DatabaseMetaData dbmd=con.getMetaData();
          System.out.println("You are connected to: "+dbmd.getDatabaseProductName());

          Best wishes,
          Larry

          • 2. Re: How to get the name of the datasource from within an EJB
            neptune5 Newbie

            This is the code - I want to replace the lookup datasource.. so that the name doesn't have to be hardcoded into the program.

            Context context=new InitialContext();
            DataSource ds=(DataSource)context.lookup("java:/PostgresDS");

            Connection conn=ds.getConnection();
            Statement stmt=conn.createStatement();
            String stmtString="SELECT MAX(Id) FROM Orderhead";

            • 3. Re: How to get the name of the datasource from within an EJB
              Larry Newbie

              Hi again Adrian,

              That's a slightly different question than I thought you were asking. If I understand correctly, you want to be able to connect to different databases with the same application without changing the source code. You may not realize it, but your datasource is not "cardcoded". You can put any connection URL in the datasource xml file. For example, my application MYAPP can connect to Postgresql or Oracle, depending on which of two versions of the myapp-ds.xml I deploy. The source code references java:/MYAPPDS. myapp-ds.xml looks like this:


              <local-tx-datasource>
              <jndi-name>MYAPPDS</jndi-name>
              <connection-url>jdbc:postgresql:DBNAME</connection-url>
              <driver-class>org.postgresql.Driver</driver-class>
              <user-name>USERNAME</user-name>
              < password>PASSWORD</ password>
              </local-tx-datasource>


              or like this:


              <local-tx-datasource>
              <jndi-name>MYAPPDS</jndi-name>
              <connection-url>jdbc:oracle:thin@SERVERNAME:1521:ORCL</connection-url>
              <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
              <user-name>USERNAME</user-name>
              < password>PASSWORD</ password>
              </local-tx-datasource>


              WIthin the ejb source code, it is sometimes necessary to detect which of the two databases I am actually connected to. (For example, I might want to use a different expression for todays date, dependng on whether its Oracle or Postgresql.) I use the example I gave earlier to do this. I hope I answered the question you asked.

              Cheers.
              Larry

              • 4. Re: How to get the name of the datasource from within an EJB
                neptune5 Newbie

                Hi Larry

                Cheers for that , that was were I was getting confused . I thought you had to have a postgresql_ds.xml ( for example and say a different one for a different database) . It all makes sense now I can have a myshop-ds.xml with a java:/myshop(jindi name) but actually connect to either a test or live version of the db , by changing the DBNAME in your example above .