2 Replies Latest reply on Apr 26, 2002 11:16 AM by Tim

    datasource referencing

    Tim Newbie

      Hi,

      In terms of accessing a given datasource within JBoss, I'd like to know how to configure the system so there is only one single entry to modify if you want to change your datasource.

      Currently I have:
      --------
      In jbosscmp-jdbc.xml (for the cmps)


      java:/PostgresDS
      <datasource-mapping>PostgreSQL</datasource-mapping>

      ---------
      In some custom finders: (to get database connection)

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

      ---------
      In some test cases (using cactus and dbunit)

      connection = new DatabaseDataSourceConnection(new InitialContext(), "java:/PostgresDS");

      I need to have a level of redirection so that I can access something like:
      myDS (and point that to PostgresDS, or OracleDS etc)

      Whats the best way to do this?

      I had a look at the resource-manager but I'm not sure this will work because you need references set up to it from each bean and I don't think this will work for the test cases.

      Any info greatfully received!

      Many thanks,

      Tim



        • 1. Re: datasource referencing
          Dirk Bogun Newbie

          There are several ways to do that. You have to make a indirection.

          1. You can put an entry into the context from jboss.
          2. You can use a helper class, which gives you the name over a getDsName() method.
          3. You can put your own entry into the JNDI, to reference your actual DB name.

          • 2. Re: datasource referencing
            Tim Newbie

            Thanks,

            I'm probably going to go for option 3 however I have tried this before and can't get it to work. Could you check this out and maybe point out what I'm doing wrong?

            In jboss.xml:

            <resource-managers>
            <resource-manager>
            <res-name>jdbc/MyDS</res-name>
            <res-jndi-name>java:/PostgresDS</res-jndi-name>
            </resource-manager>
            </resource-managers>

            In ejb-jar.xml (for Member bean)

            <resource-ref>
            The Postgres database
            <res-ref-name>jdbc/MyDS</res-ref-name>
            <res-type>javax.sql.DataSource</res-type>
            <res-auth>Container</res-auth>
            </resource-ref>

            Under the JNDI view for JBoss:

            java:comp namespace of the MemberEJB bean:

            +- env (class: org.jnp.interfaces.NamingContext)
            | +- jdbc (class: org.jnp.interfaces.NamingContext)
            | | +- MyDS[link -> java:/PostgresDS] (class: javax.naming.LinkRef)

            In my code (for Member)

            Context ctx = new InitialContext();
            ds = (javax.sql.DataSource) ctx.lookup("jdbc/MyDS");

            I have tried multiple variations and can't get it to work. I am using 3.0.0 RC1 btw.

            Many thanks,

            Tim