1 Reply Latest reply on May 8, 2009 9:48 AM by Peter Johnson

    How to create DataSource dynamically in jboss

    Pugazhenthi Sivaji Newbie

      Hi,

      Could anyone have idea about creating datasource dynamically in jboss?

      I tried to use the LocalTxDataSource, but it is getting NullPointerException when it tries to get the TranscationManager.

      Here is the code for reference,

      Properties initialContextProperties = new Properties();
      initialContextProperties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
      initialContextProperties.put("java.naming.provider.url", "jnp://localhost:1099/");


      LocalTxDataSource localDataSource = new LocalTxDataSource();
      CachedConnectionManagerReference ccmr = new CachedConnectionManagerReference();


      localDataSource.setInitialContextProperties(initialContextProperties);
      localDataSource.setConnectionURL(connectionURL);
      localDataSource.setDriverClass(driverClass);
      localDataSource.setUserName(userName);
      localDataSource.setPassword(password);
      localDataSource.setMinSize(minSize);
      localDataSource.setMaxSize(maxSize);
      localDataSource.setJndiName(dsName);
      localDataSource.setNewConnectionSQL(query);
      localDataSource.setValidConnectionCheckerClassName("org.jboss.resource.adapter.jdbc.vendor.OracleValidConnectionChecker");
      localDataSource.setCheckValidConnectionSQL(query);
      localDataSource.setTransactionIsolation("TRANSACTION_READ_COMMITTED");


      try {
      InitialContext ctx = new InitialContext(initialContextProperties);
      localDataSource.setCachedConnectionManager(ccmr);
      //localDataSource.bindConnectionFactory();
      localDataSource.start();
      DataSource ds = (DataSource)ctx.lookup(dsName);
      logger.debug("DSConfigureScheduler::ds: "+ds);
      con = ds.getConnection(); //It fails here

      Thanks,
      Pugazhenthi.S

        • 1. Re: How to create DataSource dynamically in jboss
          Peter Johnson Master

          A data source is backed by several MBeans. Unless you also create the MBeans, it probably will not work. The best way to dynamically create a datasource is to have your code write out the contents of a *-ds.xml file into a temp directory, and once it is finished, move the file to the server/xxx/deploy directory, and then wait for it to be deployed. An alternative is: once your code writes the *-ds.xml file to a temp directory, call the deploy() operation on the MainDeployer MBean.