Java Code Examples for java.sql.DriverManager.getDrivers()

The following are Jave code examples for showing how to use getDrivers() of the java.sql.DriverManager class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: jboot   File: JbootAppConfig.java   View Source Code Vote up 6 votes
@Override
public void beforeJFinalStop() {
    Enumeration<Driver> drivers = DriverManager.getDrivers();
    if (drivers != null) {
        while (drivers.hasMoreElements()) {
            try {
                Driver driver = drivers.nextElement();
                DriverManager.deregisterDriver(driver);
            } catch (Exception e) {
                log.error(e.toString(), e);
            }
        }
    }
    JbootConfigManager.me().destroy();
    JbootAppListenerManager.me().onJFinalStop();
}
 
Example 2
Project: jersey-2.x-webapp-for-servlet-container   File: SampleApplicationDestroyListener.java   View Source Code Vote up 6 votes
private void deregisterJdbcDrivers() {
  ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
  Enumeration<Driver> drivers = DriverManager.getDrivers();

  while (drivers.hasMoreElements()) {
    LOGGER.debug("Deregistering JDBC Drivers:");
    Driver driver = drivers.nextElement();
    if (driver.getClass().getClassLoader() == classLoader) {
      try {
        LOGGER.debug("  {} v{}.{}",
            driver.getClass().getName(),
            driver.getMajorVersion(),
            driver.getMinorVersion()
        );

        DriverManager.deregisterDriver(driver);
      } catch (SQLException e) {
        LOGGER.error("Failed to deregister JDBC driver: ", e);
      }
    }
  }
}
 
Example 3
Project: tomcat7   File: JdbcLeakPrevention.java   View Source Code Vote up 5 votes
public List<String> clearJdbcDriverRegistrations() throws SQLException {
    List<String> driverNames = new ArrayList<String>();

    /*
     * DriverManager.getDrivers() has a nasty side-effect of registering
     * drivers that are visible to this class loader but haven't yet been
     * loaded. Therefore, the first call to this method a) gets the list
     * of originally loaded drivers and b) triggers the unwanted
     * side-effect. The second call gets the complete list of drivers
     * ensuring that both original drivers and any loaded as a result of the
     * side-effects are all de-registered.
     */
    HashSet<Driver> originalDrivers = new HashSet<Driver>();
    Enumeration<Driver> drivers = DriverManager.getDrivers();
    while (drivers.hasMoreElements()) {
        originalDrivers.add(drivers.nextElement());
    }
    drivers = DriverManager.getDrivers();
    while (drivers.hasMoreElements()) {
        Driver driver = drivers.nextElement();
        // Only unload the drivers this web app loaded
        if (driver.getClass().getClassLoader() !=
            this.getClass().getClassLoader()) {
            continue;
        }
        // Only report drivers that were originally registered. Skip any
        // that were registered as a side-effect of this code.
        if (originalDrivers.contains(driver)) {
            driverNames.add(driver.getClass().getCanonicalName());
        }
        DriverManager.deregisterDriver(driver);
    }
    return driverNames;
}
 
Example 4
Project: java-jdbc   File: TracingDriver.java   View Source Code Vote up 5 votes
private List<Driver> registeredDrivers() {
  List<Driver> result = new ArrayList<>();
  for (Enumeration<Driver> driverEnumeration = DriverManager.getDrivers();
      driverEnumeration.hasMoreElements(); ) {
    result.add(driverEnumeration.nextElement());
  }
  return result;
}
 
Example 5
Project: apache-tomcat-7.0.73-with-comment   File: JdbcLeakPrevention.java   View Source Code Vote up 5 votes
public List<String> clearJdbcDriverRegistrations() throws SQLException {
    List<String> driverNames = new ArrayList<String>();

    /*
     * DriverManager.getDrivers() has a nasty side-effect of registering
     * drivers that are visible to this class loader but haven't yet been
     * loaded. Therefore, the first call to this method a) gets the list
     * of originally loaded drivers and b) triggers the unwanted
     * side-effect. The second call gets the complete list of drivers
     * ensuring that both original drivers and any loaded as a result of the
     * side-effects are all de-registered.
     */
    HashSet<Driver> originalDrivers = new HashSet<Driver>();
    Enumeration<Driver> drivers = DriverManager.getDrivers();
    while (drivers.hasMoreElements()) {
        originalDrivers.add(drivers.nextElement());
    }
    drivers = DriverManager.getDrivers();
    while (drivers.hasMoreElements()) {
        Driver driver = drivers.nextElement();
        // Only unload the drivers this web app loaded
        if (driver.getClass().getClassLoader() !=
            this.getClass().getClassLoader()) {
            continue;
        }
        // Only report drivers that were originally registered. Skip any
        // that were registered as a side-effect of this code.
        if (originalDrivers.contains(driver)) {
            driverNames.add(driver.getClass().getCanonicalName());
        }
        DriverManager.deregisterDriver(driver);
    }
    return driverNames;
}
 
Example 6
Project: jdk8u-jdk   File: DriverManagerTests.java   View Source Code Vote up 5 votes
/**
 * Utility method to remove all registered drivers
 */
private static void removeAllDrivers() {
    java.util.Enumeration e = DriverManager.getDrivers();
    while (e.hasMoreElements()) {
        try {
            DriverManager.deregisterDriver((Driver) (e.nextElement()));
        } catch (SQLException ex) {
            System.out.print(ex.getMessage());
        }
    }
}
 
Example 7
Project: jdk8u-jdk   File: DriverManagerTests.java   View Source Code Vote up 5 votes
/**
 * Utility method to see if a driver is registered
 */
private boolean isDriverRegistered(Driver d) {
    boolean foundDriver = false;
    java.util.Enumeration e = DriverManager.getDrivers();
    while (e.hasMoreElements()) {
        if (d == (Driver) e.nextElement()) {
            foundDriver = true;
            break;
        }
    }
    return foundDriver;
}
 
Example 8
Project: openjdk-jdk10   File: DriverManagerTests.java   View Source Code Vote up 5 votes
/**
 * Utility method to remove all registered drivers
 */
private static void removeAllDrivers() {
    java.util.Enumeration e = DriverManager.getDrivers();
    while (e.hasMoreElements()) {
        try {
            DriverManager.deregisterDriver((Driver) (e.nextElement()));
        } catch (SQLException ex) {
            System.out.print(ex.getMessage());
        }
    }
}
 
Example 9
Project: openjdk-jdk10   File: DriverManagerTests.java   View Source Code Vote up 5 votes
/**
 * Utility method to see if a driver is registered
 */
private boolean isDriverRegistered(Driver d) {
    boolean foundDriver = false;
    java.util.Enumeration e = DriverManager.getDrivers();
    while (e.hasMoreElements()) {
        if (d == (Driver) e.nextElement()) {
            foundDriver = true;
            break;
        }
    }
    return foundDriver;
}
 
Example 10
Project: lazycat   File: JdbcLeakPrevention.java   View Source Code Vote up 5 votes
public List<String> clearJdbcDriverRegistrations() throws SQLException {
	List<String> driverNames = new ArrayList<String>();

	/*
	 * DriverManager.getDrivers() has a nasty side-effect of registering
	 * drivers that are visible to this class loader but haven't yet been
	 * loaded. Therefore, the first call to this method a) gets the list of
	 * originally loaded drivers and b) triggers the unwanted side-effect.
	 * The second call gets the complete list of drivers ensuring that both
	 * original drivers and any loaded as a result of the side-effects are
	 * all de-registered.
	 */
	HashSet<Driver> originalDrivers = new HashSet<Driver>();
	Enumeration<Driver> drivers = DriverManager.getDrivers();
	while (drivers.hasMoreElements()) {
		originalDrivers.add(drivers.nextElement());
	}
	drivers = DriverManager.getDrivers();
	while (drivers.hasMoreElements()) {
		Driver driver = drivers.nextElement();
		// Only unload the drivers this web app loaded
		if (driver.getClass().getClassLoader() != this.getClass().getClassLoader()) {
			continue;
		}
		// Only report drivers that were originally registered. Skip any
		// that were registered as a side-effect of this code.
		if (originalDrivers.contains(driver)) {
			driverNames.add(driver.getClass().getCanonicalName());
		}
		DriverManager.deregisterDriver(driver);
	}
	return driverNames;
}
 
Example 11
Project: rapidminer   File: DatabaseService.java   View Source Code Vote up 4 votes
private static Enumeration<Driver> getAllDrivers() {
    return DriverManager.getDrivers();
}
 
Example 12
Project: uavstack   File: JdbcHookProxy.java   View Source Code Vote up 4 votes
/**
 * inject DriverManager
 * 
 * @param webapploader
 * @param appid
 * @return
 */
private void injectDriverManager(ClassLoader webapploader, InterceptContext ic) {

    if (isHookEventDone("isInjectDrvMgr")) {
        return;
    }

    JdbcDriverIT jdbcDriverIT = new JdbcDriverIT(this.getAppID(ic));

    jdbcDriverIT.initSomeDrivers(webapploader);

    Enumeration<Driver> eu = DriverManager.getDrivers();

    while (eu.hasMoreElements()) {

        Driver dr = eu.nextElement();

        jdbcDriverIT.doRegisterDriver(dr, true);
    }

}