troubleshooting Question

apache.commons: How to get a DataSource?

Avatar of spetrowitsch
spetrowitsch asked on
Java
7 Comments2 Solutions389 ViewsLast Modified:
Hi,

I plan to hide it for my application to access a "settings"-table or file with two columns: attribute and value.
For that I thought to use apache.commons.

What I did (using a database) until now is getting the connection like this:
        try {
                Class.forName(plmSettingsDBDriver);
          }    
          catch (ClassNotFoundException cnfe) {
                logger.error("Error while reading database-driver-class", cnfe );
          }
          String url = plmSettingsDBURL;
          String username = plmSettingsUser;
          String password = mySettingsPassword;
          Connection con = DriverManager.getConnection(url, username, password);

and reading the data as usual:

                        con = getPLMSettingsConnection();
              
                      for (int i = 0; i < select.length; i++) {
                          stmt = con.createStatement();
                          String query = "SELECT value FROM settings WHERE ATTRIBUTE = '" +
                                select[i] + "'";
                          ResultSet rs = stmt.executeQuery(query);
                          ...
                         }


Now I would like to replace the reading of data with Connections, Statements and ResultSets with something like this:

   String value = config.getEntry(select[0], "settings");
                      
while config is of type CCommConf, where I defined the following functions:

    private Configuration getConfiguration(String configName) throws Exception {
            ConfigurationFactory factory = new ConfigurationFactory();
               factory.setConfigurationFileName(CONFIGFILE);
               Configuration conf1 = factory.getConfiguration();
               Configuration conf2 = null;
               if (con != null) {
                     conf2 = new DatabaseConfiguration((DataSource)con,
                           "settings", "attribute", "value");
               } else {
                  ConfigurationFactory factory2 = new ConfigurationFactory();
                     factory2.setConfigurationFileName(SETTINGSFILE);
                     conf2 = factory.getConfiguration();
               }

               // Create and initialize the node combiner
            NodeCombiner combiner = new UnionCombiner();

            // Construct the combined configuration
            CombinedConfiguration cc = new CombinedConfiguration(combiner);
            cc.addConfiguration((AbstractConfiguration) conf1, "db");
            cc.addConfiguration((AbstractConfiguration) conf2, "settings");
            
            return cc.getConfiguration(configName);
      }

        /**
       * Constructor without any code
       *
       */
      public CCommConf() {
      }

      /**
       * Set´s the connection for the settings-table, if the
       * settings-table is in a database and not some other kind
       * of storage.
       * @param con Connection
       */
      public void setConnection(Connection con) {
            this.con = con;
      }

   public String getEntry(String entry, String configName) throws Exception {
            String ret = null;
        try {
              config = getConfiguration(configName);
                     if (config.getProperty(entry) != null)
                           ret = config.getProperty(entry).toString();
            } catch (ConfigurationException e) {
                  logger.fatal(e);
                  throw e;
            }
            return ret;
      }


What I don´t know is now:
- How to get a DataSource (I know, the following case is not the right thing to do it: conf2 = new DatabaseConfiguration((DataSource)con, "settings", "attribute", "value"); ). I read somewhere that I have to use JNDI, but - if it´s really necessary - I don´t know how to do that. But maybe it´s not necessary to use JNDI, there is a simpler way?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 7 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros