5 Replies Latest reply on Nov 13, 2003 12:16 PM by jochenwurster

    Newbie: using other datasources

    viz

      Hi All,

      I have an EntityBean which works fine with the Hypersonic database.

      I now want the bean to use mySQL instead of Hypersonic.

      I have created the attached mysql-ds.xml and placed it into the /deploy directory.

      My question is: What steps should I now take to get the bean to use mySQL instead of Hypersonic?

      Any help gratefully received.

      Kind regards
      --
      Viz

        • 1. Re: Newbie: using other datasources

          configure the datasource in jbosscmp-jdbc.xml of your entity bean; or change the default datasource in standardjbosscmp.xml; or remove the hypersonic datasource configuration and deploy MySQL datasource under java:/DefaultDS

          • 2. Re: Newbie: using other datasources
            viz

            Again, many thanks!

            Struggling to get things working (being a newbie) I had made too many changes to too many files. So, I deleted my JBoss and reinstalled.

            Once I configure the jbosscmp-jdbc.xml and the mysql-ds.xml - it all up and fired!

            I must admit I nearly gave up with JBoss, the lack of consise documentation is a big drawback. I plan to put all my findings together into a small document so that other don't go down the same holes I did.

            Thanks again.

            Kind regards,
            --
            Marc

            • 3. Re: Newbie: using other datasources
              atli

              Generally, you configure a connection pool in your application server configuration files, and access it via the Java Naming and Directory Interface (JNDI). The following code shows how you might use a connection pool:

              import java.sql.Connection;
              import java.sql.SQLException;
              import java.sql.Statement;

              import javax.naming.InitialContext;
              import javax.sql.DataSource;


              public class MyServletJspOrEjb {

              public void doSomething() throws Exception {
              /*
              * Create a JNDI Initial context to be able to
              * lookup the DataSource
              *
              * In production-level code, this should be cached as
              * an instance or static variable, as it can
              * be quite expensive to create a JNDI context.
              *
              * Note: This code only works when you are using servlets
              * or EJBs in a J2EE application server. If you are
              * using connection pooling in standalone Java code, you
              * will have to create/configure datasources using whatever
              * mechanisms your particular connection pooling library
              * provides.
              */

              InitialContext ctx = new InitialContext();

              /*
              * Lookup the DataSource, which will be backed by a pool
              * that the application server provides. DataSource instances
              * are also a good candidate for caching as an instance
              * variable, as JNDI lookups can be expensive as well.
              */

              DataSource ds = (DataSource) ctx.lookup("jdbc/MySQLDB");

              /*
              * The following code is what would actually be in your
              * Servlet, JSP or EJB 'service' method...where you need
              * to work with a JDBC connection.
              */

              Connection conn = null;
              Statement stmt = null;

              try {
              conn = ds.getConnection();

              /*
              * Now, use normal JDBC programming to work with
              * MySQL, making sure to close each resource when you're
              * finished with it, which allows the connection pool
              * resources to be recovered as quickly as possible
              */

              stmt = conn.createStatement();
              stmt.execute("SOME SQL QUERY");
              stmt.close();

              conn.close();
              } finally {
              /*
              * Close any JDBC instances here that weren't
              * explicitly closed during normal code path, so
              * that we don't 'leak' resources...
              */

              if (stmt != null) {
              try {
              stmt.close();
              } catch (SQLException sqlEx) {
              // ignore -- as we can't do anything about it here
              }

              stmt = null;
              }

              if (conn != null) {
              try {
              conn.close();
              } catch (SQLException sqlEx) {
              // ignore -- as we can't do anything about it here
              }

              conn = null;
              }
              }
              }
              }

              • 4. Re: Newbie: using other datasources
                jochenwurster

                I am interested in your small documentation. Especially the changes you have made to jbosscmp-jdbc.xml.

                Bye Jochen

                • 5. Re: Newbie: using other datasources
                  jochenwurster

                  http://www.jboss.org/modules/bb/index.html?module=bb&op=viewtopic&t= be interesting for people having trouble with datasource configuration.

                  Bye Jochen