Java DataSource getDataSource(String service)

Here you can find the source of getDataSource(String service)

Description

get Data Source

License

Open Source License

Declaration

private static DataSource getDataSource(String service) throws Exception 

Method Source Code


//package com.java2s;
/*/*from w ww . ja  v a2 s .  c  o  m*/
 * @(#) JndiHelper.java @VERSION@
 * 
 * Copyright (c) 2009+ Amit Kumar
 * 
 * The software is released under ASL 2.0, Please
 * read License.txt
 *
 */

import java.util.Hashtable;
import java.util.logging.Logger;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class Main {
    private static Logger logger = Logger.getAnonymousLogger();

    private static DataSource getDataSource(String service) throws Exception {
        Context ctx;
        try {
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY, "org.mortbay.naming.InitialContextFactory");
            env.put(Context.PROVIDER_URL, "localhost:1099");
            ctx = new InitialContext(env);
        } catch (Exception e) {
            logger.severe("Error configuring initial context " + e);
            throw e;
        }
        Object obj;//object to return
        try {
            obj = ctx.lookup(service); //perform lookup
        } catch (NamingException e) {
            logger.warning("Problem looking up Object " + service + " in the java:comp/env/jdbc "
                    + "JNDI namespase. Is the server namespace configured?: " + e + ":" + e.getMessage());
            throw new Exception(e);
        }
        if (obj == null) {
            throw new Exception("could not find " + service);
        } else {
            return (DataSource) obj;

        }
    }
}

Related

  1. getConnection(final DataSource ds, final boolean autoCommit)
  2. getConnectionFromDataSource(DataSource ds)
  3. getDatabaseName(DataSource dataSource)
  4. getDataSource(String jndi)
  5. getDataSource(String jndiName)
  6. getDataSourceCache(String hostName)
  7. getDBConnection(DataSource dataSource)
  8. getGlobalVariable(DataSource dataSource, String variable)
  9. getJdbcUrlFromDataSource(DataSource dataSource)