List of usage examples for java.sql DriverManager getDriver
@CallerSensitive public static Driver getDriver(String url) throws SQLException
From source file:org.sqsh.SQLDriverManager.java
/** * Similar to DriverManager.getDriver() except that it searches * through our shiny new classloader./*from w ww .j a va2s. c o m*/ * * @param url * @return * @throws SQLException */ private Driver getDriverFromUrl(String url) throws SQLException { for (SQLDriver driver : drivers.values()) { try { Driver d = (Driver) Class.forName(driver.getDriverClass(), true, classLoader).newInstance(); if (d.acceptsURL(url)) { return d; } } catch (Exception e) { /* IGNORED */ } } return DriverManager.getDriver(url); }
From source file:science.freeabyss.hulk.jdbc.dbcp.PoolingDriverExample.java
public static void initDataSource() { try {//from ww w . j av a 2 s. com // // First, we'll create a ConnectionFactory that the // pool will use to create Connections. // We'll use the DriverManagerConnectionFactory, // using the connect string passed in the command line // arguments. // PropertiesUtil.init("dbcp.properties"); Class.forName(PropertiesUtil.getString("dbcp.driver")); ConnectionFactory connFactory = new DriverManagerConnectionFactory(PropertiesUtil.getString("dbcp.url"), PropertiesUtil.getString("dbcp.username"), PropertiesUtil.getString("dbcp.password")); // // Next, we'll create the PoolableConnectionFactory, which wraps // the "real" Connections created by the ConnectionFactory with // the classes that implement the pooling functionality. // PoolableConnectionFactory poolFactory = new PoolableConnectionFactory(connFactory, null); // // Now we'll need a ObjectPool that serves as the // actual pool of connections. // // We'll use a GenericObjectPool instance, although // any ObjectPool implementation will suffice. // ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolFactory); // Set the factory's pool property to the owning pool poolFactory.setPool(connectionPool); // // Finally, we create the PoolingDriver itself... // Class.forName("org.apache.commons.dbcp2.PoolingDriver"); PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); // // ...and register our pool with it. // driver.registerPool("example", connectionPool); // // Now we can just use the connect string "jdbc:apache:commons:dbcp:example" // to access our pool of Connections. // } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } }
From source file:za.co.wilderness.WildernessPoolingDriver.java
private void setupDriver() throws Exception { String jdbcDriverName = "com.microsoft.sqlserver.jdbc.SQLServerConnectionPoolDataSource"; try {// w ww . ja va 2 s . c o m java.lang.Class.forName(jdbcDriverName).newInstance(); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { //System.out.println("Error when attempting to obtain DB Driver: " + jdbcDriverName + " on "+ new Date().toString() + e.getMessage()); logger.log(Level.SEVERE, "Error when attempting to obtain DB Driver: " + jdbcDriverName + " on " + new Date().toString(), e); throw new Exception(e); } ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(this.connectURI, this.properties); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null); GenericObjectPoolConfig genConfig = new GenericObjectPoolConfig(); genConfig.setMaxIdle(this.config.getMaxIdle()); genConfig.setMaxTotal(this.config.getMaxActive()); genConfig.setMinIdle(this.config.getMinIdle()); genConfig.setMaxWaitMillis(this.config.getMaxWaitMillis()); genConfig.setTimeBetweenEvictionRunsMillis(5000); genConfig.setTestWhileIdle(true); ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, genConfig); // Set the factory's pool property to the owning pool poolableConnectionFactory.setPool(connectionPool); // // Finally, we create the PoolingDriver itself... // Class.forName("org.apache.commons.dbcp2.PoolingDriver"); PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); //System.out.println("Driver : " + driver.toString()); logger.log(Level.FINE, "Driver : " + driver.toString()); // // ...and register our pool with it. // driver.registerPool(EXTERNAL_SERVICE.name(), connectionPool); // // Now we can just use the connect string "jdbc:apache:commons:dbcp:example" // to access our pool of Connections. // }
From source file:za.co.wilderness.WildernessPoolingDriver.java
public Connection GetPoolConnection() throws Exception { Connection conn = null;/*w w w . j a va 2s . c o m*/ try { long startTime = System.currentTimeMillis(); conn = DriverManager.getConnection("jdbc:apache:commons:dbcp:" + this.EXTERNAL_SERVICE.name()); PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); ObjectPool<? extends Connection> connectionPool = driver .getConnectionPool(this.EXTERNAL_SERVICE.name()); logger.log(Level.FINE, "NumActive: " + connectionPool.getNumActive() + " NumIdle: " + connectionPool.getNumIdle()); //System.out.println("NumActive: " + connectionPool.getNumActive() + " NumIdle: " + connectionPool.getNumIdle()); if (connectionPool.getNumActive() == config.getMaxActive()) { config.setMaxActive(config.getMaxActive() + 50); try { shutdownDriver(); } catch (Exception e) { logger.log(Level.FINE, e.getMessage()); } } long endTime = System.currentTimeMillis(); //System.out.println("Total connection time: " + this.EXTERNAL_SERVICE.name() + " " + (endTime - startTime) ); logger.log(Level.FINE, "Open connection for " + this.EXTERNAL_SERVICE.name()); } catch (SQLException e) { //System.out.println("Create connection failed. " + e.getMessage()); logger.log(Level.SEVERE, "Create connection failed. ", e); throw new Exception(e); } return conn; }
From source file:za.co.wilderness.WildernessPoolingDriver.java
public void shutdownDriver() throws Exception { PoolingDriver driver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); boolean setServiceStatus = EXTERNAL_SERVICE_STATUS.setServiceStatus(EXTERNAL_SERVICE, ProcessingStatus.STOPPED);//from ww w .j av a 2 s. co m driver.closePool(this.EXTERNAL_SERVICE.name()); logger.log(Level.FINE, "Pooling driver shutdown for " + this.EXTERNAL_SERVICE.name()); }