14 Replies Latest reply on Jul 18, 2002 8:39 PM by rschiavi

    JBoss 3.0, JDBC and DataSource

    asharzaman

      Hello All,

      I am new to JBoss and have background in WebLogic. I am trying to create a DB2 Datasource in JBoss 3 and am having trouble. I was reading the forum and found that it was rather easy in 2.x, but I can't find anything about how to configure it in 3.0.

      Any help is appreciated. Thanks in advance

      Ashar

        • 1. Re: JBoss 3.0, JDBC and DataSource

          Use hsqldb-service.xml as an example. Is is in the deploy directory under the default configuration.

          Jim

          • 2. Re: JBoss 3.0, JDBC and DataSource

            asharzaman,

            You can find the example configuration in the %JBOSS_HOME%/docs/example/jca/db2-service.xml and go from there. I have not worked with DB2 but so far I was able to get the Oracle and MySQL working fine.

            JG

            • 3. Re: JBoss 3.0, JDBC and DataSource
              asharzaman

              Thanks for the reply everyone. I made the changes and the server says that the Data Source was bound. When I try to lookup the DS, I get NameNotFoundException. When I try to lookup other beans in the JNDI, I have no problems.

              Any suggestions?

              Thanks

              Ashar

              • 4. Re: JBoss 3.0, JDBC and DataSource

                asharzaman,

                Do you have an example of how you call it? Please post it, we might be able to help you debug or solve the problem.

                JG

                • 5. Re: JBoss 3.0, JDBC and DataSource
                  asharzaman

                  JavaGuy,

                  Thanks for your reply. When the server starts, I get that the data source was bound to JNDI. Here is the message:

                  20:38:23,067 INFO [Ashar] Bound connection factory for resource adapter 'JBoss LocalTransaction JDBC Wrapper' to JNDI name 'java:/Ashar'


                  Here is my test Code


                  import javax.ejb.*;
                  import javax.naming.*;
                  import java.util.*;

                  public class JBossClient {
                  private Context getInitialContext() {

                  String url = "jnp://localhost:1099";
                  String user = null;
                  String password = null;
                  Properties properties = null;

                  try {

                  properties = new Properties();
                  properties.put(Context.INITIAL_CONTEXT_FACTORY,
                  "org.jnp.interfaces.NamingContextFactory");
                  properties.put(Context.PROVIDER_URL, url);

                  if (user != null) {
                  properties.put(Context.SECURITY_PRINCIPAL, user);
                  properties.put(Context.SECURITY_CREDENTIALS,
                  password == null ? "" : password);
                  }

                  return new InitialContext(properties);
                  } catch (Exception e) {
                  System.out.println("Unable to connect to JBoss server at " + url);
                  e.printStackTrace();
                  }

                  return null;
                  }

                  public JBossClient() {
                  Context ctx = getInitialContext();
                  try {

                  Object o = ctx.lookup("Ashar");
                  // Object o = ctx.lookup("java:/Ashar");
                  // Object o = ctx.lookup("MyBean"); // Works

                  System.out.println("done");
                  } catch(Exception ex) {
                  System.out.println(ex.toString());
                  }
                  }
                  public static void main(String[] args) {
                  JBossClient j = new JBossClient();
                  }
                  }


                  Again, Thank you for your help.

                  Ashar Zaman

                  • 6. Re: JBoss 3.0, JDBC and DataSource

                    asharzaman,

                    From what I saw in you code that should have worked fine with the:
                    .
                    .
                    .
                    Object o = ctx.lookup("java:/Ashar");
                    .
                    .
                    .

                    I cast mine to Datasource and get a connection with it to test using a simple query.

                    JG

                    • 7. Re: JBoss 3.0, JDBC and DataSource
                      davidjencks

                      It's not 100% clear where you are running your client, but you can only access the java: context within the jboss vm. So you should test your datasource from a session bean or an mbean.

                      • 8. Re: JBoss 3.0, JDBC and DataSource
                        asharzaman

                        What you are talking about is Remote vs. Local interfaces (I think). Well, I should be able to do a lookup. If I can lookup any other bound object from JNDI (remote), it is not any different. Well, I don't know what else to do. Again, thanks for everybody's help.

                        Ashar

                        • 9. Re: JBoss 3.0, JDBC and DataSource
                          tbfmicke

                          Not to presume too much but I think he meant what he wrote :-)

                          As you have seen a datasource named DefaultDS (for example) is put in JNDI with the name java:/DefaultDS.

                          But (I think this is something specific for JBoss) anything in the java: namespace can only be found when it is looked up from inside JBoss. So as he said... try the lookup from inside an EJB or servlet and it should succeed.

                          I guess that the reasoning here is that it might be a security problem to give out a connection to a database to anybody that can get the right JNDI name...

                          Regards
                          Mikael


                          • 10. Re: JBoss 3.0, JDBC and DataSource
                            asharzaman

                            Mikael and David,

                            I really appreciate your help. You were correct when you suggested that anything bound to java:/ tree can only be looked up from within the JBoss JVM.

                            Again, I want to thank everyone for their help with this problem.

                            Ashar

                            • 11. Re: JBoss 3.0, JDBC and DataSource

                              asharzaman,

                              Sorry for not reading the message correctly.

                              JG

                              • 12. Re: JBoss 3.0, JDBC and DataSource
                                buster

                                But how do you get a PooledDataSource, or a ConnectionPool.

                                Thanks

                                • 13. Re: JBoss 3.0, JDBC and DataSource
                                  rschiavi


                                  a simple question, i see the db2-service.xml, but like, where does it go?

                                  i used 2.x and can't figure out this new setting up a datasource dance.

                                  thanks!

                                  rich

                                  • 14. Re: JBoss 3.0, JDBC and DataSource
                                    rschiavi



                                    So, if you can only access "java:/" bound JNDI in the jboss jvm, how can you bind a datasource so a remote client (say a junit test harness app) can access those JNDI things?

                                    Thanks,

                                    Rich