Example usage for java.sql DriverManager getDrivers

List of usage examples for java.sql DriverManager getDrivers

Introduction

In this page you can find the example usage for java.sql DriverManager getDrivers.

Prototype

@CallerSensitive
public static Enumeration<Driver> getDrivers() 

Source Link

Document

Retrieves an Enumeration with all of the currently loaded JDBC drivers to which the current caller has access.

Usage

From source file:org.deegree.enterprise.servlet.OGCServletController.java

@Override
public void destroy() {
    super.destroy();
    Enumeration<Driver> e = DriverManager.getDrivers();
    while (e.hasMoreElements()) {
        Driver driver = e.nextElement();
        try {/*from  w w w .ja v a 2 s. co  m*/
            if (driver.getClass().getClassLoader() == getClass().getClassLoader())
                DriverManager.deregisterDriver(driver);
        } catch (SQLException e1) {
            LOG.logError("Cannot unload driver: " + driver);
        }
    }
    LogFactory.releaseAll();
    LogManager.shutdown();
    // SLF4JLogFactory.releaseAll(); // should be the same as the LogFactory.releaseAll call
    Iterator<Class<?>> i = IIORegistry.getDefaultInstance().getCategories();
    while (i.hasNext()) {
        Class<?> c = i.next();
        Iterator<?> k = IIORegistry.getDefaultInstance().getServiceProviders(c, false);
        while (k.hasNext()) {
            Object o = k.next();
            if (o.getClass().getClassLoader() == getClass().getClassLoader()) {
                IIORegistry.getDefaultInstance().deregisterServiceProvider(o);
                LOG.logDebug("Deregistering JAI driver ", o.getClass());
            }
        }
    }
    Introspector.flushCaches();
    // just clear the configurations for now, it does not hurt
    CRSConfiguration.DEFINED_CONFIGURATIONS.clear();
}

From source file:com.streamsets.pipeline.lib.jdbc.JdbcUtil.java

private HikariConfig createDataSourceConfig(HikariPoolConfigBean hikariConfigBean, boolean autoCommit,
        boolean readOnly) throws StageException {
    HikariConfig config = new HikariConfig();

    // Log all registered drivers
    LOG.info("Registered JDBC drivers:");
    Collections.list(DriverManager.getDrivers()).forEach(driver -> {
        LOG.info("Driver class {} (version {}.{})", driver.getClass().getName(), driver.getMajorVersion(),
                driver.getMinorVersion());
    });//from  ww  w.  ja v  a2 s  . c  o m

    config.setJdbcUrl(hikariConfigBean.getConnectionString());
    if (hikariConfigBean.useCredentials) {
        config.setUsername(hikariConfigBean.username.get());
        config.setPassword(hikariConfigBean.password.get());
    }
    config.setAutoCommit(autoCommit);
    config.setReadOnly(readOnly);
    config.setMaximumPoolSize(hikariConfigBean.maximumPoolSize);
    config.setMinimumIdle(hikariConfigBean.minIdle);
    config.setConnectionTimeout(hikariConfigBean.connectionTimeout * MILLISECONDS);
    config.setIdleTimeout(hikariConfigBean.idleTimeout * MILLISECONDS);
    config.setMaxLifetime(hikariConfigBean.maxLifetime * MILLISECONDS);

    if (!StringUtils.isEmpty(hikariConfigBean.driverClassName)) {
        config.setDriverClassName(hikariConfigBean.driverClassName);
    }

    if (!StringUtils.isEmpty(hikariConfigBean.connectionTestQuery)) {
        config.setConnectionTestQuery(hikariConfigBean.connectionTestQuery);
    }

    if (hikariConfigBean.transactionIsolation != TransactionIsolationLevel.DEFAULT) {
        config.setTransactionIsolation(hikariConfigBean.transactionIsolation.name());
    }

    if (StringUtils.isNotEmpty(hikariConfigBean.initialQuery)) {
        config.setConnectionInitSql(hikariConfigBean.initialQuery);
    }

    config.setDataSourceProperties(hikariConfigBean.getDriverProperties());

    return config;
}

From source file:org.mifos.framework.ApplicationInitializer.java

private void unregisterMySQLDriver() {
    // unregister any jdbc drivers (mysql driver)
    try {// w  w w.  ja  v  a  2s  .c o m
        for (Enumeration<Driver> e = DriverManager.getDrivers(); e.hasMoreElements();) {
            Driver driver = e.nextElement();
            if (driver.getClass().getClassLoader() == getClass().getClassLoader()) {
                DriverManager.deregisterDriver(driver);
            }
        }
    } catch (Exception e) {
        logger.error("can't unregister jdbc drivers", e);
    }
}

From source file:com.jaspersoft.jasperserver.api.common.service.impl.JdbcDriverServiceImpl.java

protected Enumeration<Driver> getDriversRegisteredInJVM() throws SQLException {
    return DriverManager.getDrivers();
}

From source file:org.lexevs.system.service.LexEvsResourceManagingService.java

private void deregisterDrivers() {
    // Work around MySQL bug http://bugs.mysql.com/bug.php?id=65909
    try {/*w  ww .ja v a  2 s . co m*/
        Class.forName("com.mysql.jdbc.AbandonedConnectionCleanupThread", false, this.myClassLoader)
                .getMethod("shutdown").invoke(null);
    } catch (Exception e) {
        e.printStackTrace();
    }

    Enumeration<Driver> drivers = DriverManager.getDrivers();
    while (drivers.hasMoreElements()) {
        Driver driver = drivers.nextElement();
        try {
            DriverManager.deregisterDriver(driver);
        } catch (SQLException e) {
            //
        }
    }
}

From source file:com.flexive.core.Database.java

/**
 * Cleanup cached data sources./*  www  . j  a v  a 2s .  c om*/
 * 
 * @since 3.1.4
 */
public static synchronized void cleanup() {
    if (defaultDataSourceInitialized) {
        // try to deregister bundled H2 driver
        try {
            final Enumeration<Driver> drivers = DriverManager.getDrivers();
            while (drivers.hasMoreElements()) {
                final Driver driver = drivers.nextElement();
                if ("org.h2.Driver".equals(driver.getClass().getCanonicalName())) {
                    // deregister our bundled H2 driver
                    LOG.info("Uninstalling bundled H2 driver");
                    DriverManager.deregisterDriver(driver);
                }
            }
        } catch (Exception e) {
            LOG.warn("Failed to deregister H2 driver", e);
        }
    }

    dataSourcesByName.clear();
    for (int i = 0; i < dataSources.length; i++) {
        dataSources[i] = null;
    }
    for (int i = 0; i < dataSourcesNoTX.length; i++) {
        dataSourcesNoTX[i] = null;
    }
    globalDataSource = null;
    testDataSource = null;
    testDataSourceNoTX = null;
}

From source file:com.rapid.server.RapidServletContextListener.java

@Override
public void contextDestroyed(ServletContextEvent event) {

    _logger.info("Shutting down...");

    // interrupt the page monitor if we have one
    if (_monitor != null)
        _monitor.interrupt();// ww  w . j a  va 2  s. c  o m

    // get the servletContext
    ServletContext servletContext = event.getServletContext();

    // get all of the applications
    Applications applications = (Applications) servletContext.getAttribute("applications");
    // if we got some
    if (applications != null) {
        // loop the application ids
        for (String id : applications.getIds()) {
            // get the application
            Versions versions = applications.getVersions(id);
            // loop the versions of each app
            for (String version : versions.keySet()) {
                // get the application
                Application application = applications.get(id, version);
                // have it close any sensitive resources 
                application.close(servletContext);
            }
        }
    }

    // sleep for 2 seconds to allow any database connection cleanup to complete
    try {
        Thread.sleep(2000);
    } catch (Exception ex) {
    }

    // This manually deregisters JDBC drivers, which prevents Tomcat from complaining about memory leaks from this class
    Enumeration<Driver> drivers = DriverManager.getDrivers();
    while (drivers.hasMoreElements()) {
        Driver driver = drivers.nextElement();
        try {
            DriverManager.deregisterDriver(driver);
            _logger.info(String.format("Deregistering jdbc driver: %s", driver));
        } catch (SQLException e) {
            _logger.error(String.format("Error deregistering driver %s", driver), e);
        }
    }

    // Thanks to http://stackoverflow.com/questions/11872316/tomcat-guice-jdbc-memory-leak
    Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
    Thread[] threadArray = threadSet.toArray(new Thread[threadSet.size()]);
    for (Thread t : threadArray) {
        if (t.getName().contains("Abandoned connection cleanup thread")) {
            synchronized (t) {
                try {
                    _logger.info("Forcing stop of Abandoned connection cleanup thread");
                    t.stop(); //don't complain, it works
                } catch (Exception ex) {
                    _logger.info("Error forcing stop of Abandoned connection cleanup thread", ex);
                }
            }
        }
    }

    // sleep for 1 second to allow any database connection cleanup to complete
    try {
        Thread.sleep(1000);
    } catch (Exception ex) {
    }

    // last log
    _logger.info("Logger shutdown");
    // shutdown logger
    if (_logger != null)
        LogManager.shutdown();

}

From source file:org.apache.hive.beeline.BeeLine.java

private Driver findRegisteredDriver(String url) {
    for (Enumeration drivers = DriverManager.getDrivers(); drivers != null && drivers.hasMoreElements();) {
        Driver driver = (Driver) drivers.nextElement();
        try {//from  w  w  w. j  av  a2s .com
            if (driver.acceptsURL(url)) {
                return driver;
            }
        } catch (Exception e) {
        }
    }
    return null;
}