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?
ASKER CERTIFIED SOLUTION
Mayank S
Principal Technologist

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 2 Answers and 7 Comments.
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>

Mike

Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 2 Answers and 7 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004