List of usage examples for java.sql DriverManager getDriver
@CallerSensitive public static Driver getDriver(String url) throws SQLException
From source file:com.google.acre.keystore.MySQLKeyStore.java
@SuppressWarnings("unused") private void setupDriver(String connectURI, String username, String password) throws SQLException, ClassNotFoundException { GenericObjectPool connectionPool = new GenericObjectPool(null); connectionPool.setTimeBetweenEvictionRunsMillis(5 * 60 * 1000 - 13); // check if jdbc connections are still alive every 5 min - 13 msec ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, username, password); new PoolableConnectionFactory(connectionFactory, connectionPool, null, TEST_QUERY, false, true); Class.forName("org.apache.commons.dbcp.PoolingDriver"); PoolingDriver driver = (PoolingDriver) DriverManager.getDriver(DRIVER_PREFIX); driver.registerPool(DATASOURCE_NAME, connectionPool); active = true;/*www. j av a2 s. c om*/ // Now we can just use the connect string "jdbc:apache:commons:dbcp:keystore" // to access our pool of Connections. }
From source file:com.bloidonia.vertx.mods.JdbcProcessor.java
private static void closePool(String address, String url) throws SQLException { if (poolMap.get(address) != null) { synchronized (poolMap) { ComboPooledDataSource pool = poolMap.get(address); if (pool != null) { pool = poolMap.remove(address); if (pool != null) { pool.close();/*from w w w. j a v a2 s . c om*/ DriverManager.deregisterDriver(DriverManager.getDriver(url)); jmxReporter.stop(); } } } } }
From source file:net.fender.sql.DriverConnectionFactory.java
/** * @throws Exception//from w ww . j a v a 2 s . c o m */ public void init() throws Exception { try { driver = DriverManager.getDriver(url); } catch (SQLException e) { // no suitable driver } if (driver == null) { driver = (Driver) Class.forName(driverClassName).newInstance(); log.debug(driver); DriverManager.registerDriver(driver); } String encryptedUser = properties.getProperty("encryptedUser"); if (encryptedUser != null) { String user = null; if (cryptoUtil == null) { user = CryptoUtil.decryptUsingSystemPropertyKey(systemPropertyKeyName, encryptedUser); } else { user = cryptoUtil.decrypt(key, encryptedUser); } properties.setProperty("user", user); properties.remove("encryptedUser"); } String encryptedPassword = properties.getProperty("encryptedPassword"); if (encryptedPassword != null) { String password = null; if (cryptoUtil == null) { password = CryptoUtil.decryptUsingSystemPropertyKey(systemPropertyKeyName, encryptedPassword); } else { password = cryptoUtil.decrypt(key, encryptedPassword); } properties.setProperty("password", password); properties.remove("encryptedPassword"); } if (testOnCreate) { Connection connection = getConnection(); JdbcUtils.close(connection); } }
From source file:net.ljcomputing.config.PersistenceConfiguration.java
/** * Gets the driver class name./* w w w .j av a 2s.co m*/ * * @return the driver class name * @throws SQLException the SQL exception */ private String getDriverClassName() throws SQLException { final Driver driver = DriverManager.getDriver(url); final Class<?> driverClass = driver.getClass(); //NOPMD return driverClass.getName(); //NOPMD }
From source file:edumsg.core.PostgresConnection.java
public static void initSource() { try {/*w ww .j ava 2 s. c om*/ try { Class.forName("org.postgresql.Driver"); } catch (ClassNotFoundException ex) { LOGGER.log(Level.SEVERE, "Error loading Postgres driver: " + ex.getMessage(), ex); } try { readConfFile(); } catch (Exception e) { e.printStackTrace(); } Properties props = new Properties(); // System.out.println(DB_USERNAME); props.setProperty("user", DB_USERNAME); props.setProperty("password", DB_PASSWORD); props.setProperty("initialSize", DB_INIT_CONNECTIONS); props.setProperty("maxActive", DB_MAX_CONNECTIONS); ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(DB_URL, props); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null); poolableConnectionFactory.setPoolStatements(true); GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig(); poolConfig.setMaxIdle(Integer.parseInt(DB_INIT_CONNECTIONS)); poolConfig.setMaxTotal(Integer.parseInt(DB_MAX_CONNECTIONS)); ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, poolConfig); poolableConnectionFactory.setPool(connectionPool); Class.forName("org.apache.commons.dbcp2.PoolingDriver"); dbDriver = (PoolingDriver) DriverManager.getDriver("jdbc:apache:commons:dbcp:"); dbDriver.registerPool(DB_NAME, connectionPool); dataSource = new PoolingDataSource<>(connectionPool); } catch (Exception ex) { LOGGER.log(Level.SEVERE, "Got error initializing data source: " + ex.getMessage(), ex); } }
From source file:JDBCPool.dbcp.demo.offical.PoolingDriverExample.java
/** * ?connectURI?PoolingDriver/*www. j a v a2 s. c om*/ * @param connectURI * @throws Exception */ public static void setupDriver(String connectURI) throws Exception { // 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. ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, null); // Next, we'll create the PoolableConnectionFactory, which wraps the "real" Connections // created by the ConnectionFactory with the classes that implement the pooling functionality. PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, 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<>(poolableConnectionFactory); // 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:"); // // ...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. // }
From source file:PoolingDriverExample.java
public static void setupDriver(String connectURI) throws Exception { ////from www. ja v a 2 s.co m // 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. // ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, null); // // Next, we'll create the PoolableConnectionFactory, which wraps // the "real" Connections created by the ConnectionFactory with // the classes that implement the pooling functionality. // PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, 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<>(poolableConnectionFactory); // 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:"); // // ...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. // }
From source file:PoolingDriverExample.java
public static void setupDriver(String connectURI) throws Exception { ///* w w w. j a v a 2 s . c om*/ // 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. // ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, null); // // Next, we'll create the PoolableConnectionFactory, which wraps // the "real" Connections created by the ConnectionFactory with // the classes that implement the pooling functionality. // PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, 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<>(poolableConnectionFactory); // // 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. // }
From source file:ManualPoolingDriverExample.java
public static void setupDriver(String connectURI) throws Exception { ////from w w w . j a va2s.c o m // First, 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 connectionPool = new GenericObjectPool(null); // // Next, 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. // ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectURI, null); // // Now we'll create the PoolableConnectionFactory, which wraps // the "real" Connections created by the ConnectionFactory with // the classes that implement the pooling functionality. // PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true); // // Finally, we create the PoolingDriver itself... // Class.forName("org.apache.commons.dbcp.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. // }
From source file:de.innovationgate.webgate.api.jdbc.pool.DBCPConnectionProvider.java
public void configure(Map propsMap) throws HibernateException { try {/*from w ww .jav a2 s . co m*/ log.debug("Configure DBCPConnectionProvider"); Properties props = new Properties(); props.putAll(propsMap); String jdbcUrl = (String) props.getProperty(Environment.URL); // DBCP properties used to create the BasicDataSource Properties dbcpProperties = new Properties(); // DriverClass & url String jdbcDriverClass = props.getProperty(Environment.DRIVER); // Try to determine driver by jdbc-URL if (jdbcDriverClass == null) { Driver driver = DriverManager.getDriver(jdbcUrl); if (driver != null) { jdbcDriverClass = driver.getClass().getName(); } else { throw new HibernateException("Driver class not available"); } } dbcpProperties.put("driverClassName", jdbcDriverClass); dbcpProperties.put("url", jdbcUrl); // Username / password String username = props.getProperty(Environment.USER); if (username != null) { dbcpProperties.put("username", username); } String password = props.getProperty(Environment.PASS); if (password != null) { dbcpProperties.put("password", password); } // Isolation level String isolationLevel = props.getProperty(Environment.ISOLATION); if ((isolationLevel != null) && (isolationLevel.trim().length() > 0)) { dbcpProperties.put("defaultTransactionIsolation", isolationLevel); } // Turn off autocommit (unless autocommit property is set) String autocommit = props.getProperty(AUTOCOMMIT); if ((autocommit != null) && (autocommit.trim().length() > 0)) { dbcpProperties.put("defaultAutoCommit", autocommit); } else { dbcpProperties.put("defaultAutoCommit", String.valueOf(Boolean.FALSE)); } // Pool size String poolSize = props.getProperty(Environment.POOL_SIZE); if ((poolSize != null) && (poolSize.trim().length() > 0) && (Integer.parseInt(poolSize) > 0)) { dbcpProperties.put("maxActive", poolSize); } // Copy all "driver" properties into "connectionProperties" Properties driverProps = ConnectionProviderInitiator.getConnectionProperties(props); if (driverProps.size() > 0) { StringBuffer connectionProperties = new StringBuffer(); for (Iterator iter = driverProps.keySet().iterator(); iter.hasNext();) { String key = (String) iter.next(); String value = driverProps.getProperty(key); connectionProperties.append(key).append('=').append(value); if (iter.hasNext()) { connectionProperties.append(';'); } } dbcpProperties.put("connectionProperties", connectionProperties.toString()); } // Copy all DBCP properties removing the prefix for (Iterator iter = props.keySet().iterator(); iter.hasNext();) { String key = String.valueOf(iter.next()); if (key.startsWith(PREFIX)) { String property = key.substring(PREFIX.length()); String value = props.getProperty(key); dbcpProperties.put(property, value); } } // Backward-compatibility if (props.getProperty(DBCP_PS_MAXACTIVE) != null) { dbcpProperties.put("poolPreparedStatements", String.valueOf(Boolean.TRUE)); dbcpProperties.put("maxOpenPreparedStatements", props.getProperty(DBCP_PS_MAXACTIVE)); } if (props.getProperty(DBCP_MAXACTIVE) != null) { dbcpProperties.put("maxTotal", props.getProperty(DBCP_MAXACTIVE)); } if (props.getProperty(DBCP_MAXWAIT) != null) { dbcpProperties.put("maxWaitMillis", props.getProperty(DBCP_MAXWAIT)); } // Some debug info if (log.isDebugEnabled()) { log.debug("Creating a DBCP BasicDataSource with the following DBCP factory properties:"); StringWriter sw = new StringWriter(); dbcpProperties.list(new PrintWriter(sw, true)); log.debug(sw.toString()); } String dbKey = (String) props.get("hibernate.dbcp.dbkey"); String databaseServerId = (String) props.get("hibernate.dbcp.dbserver.id"); // Enable DBCP2 JMX monitoring information if (dbKey != null) { dbcpProperties.put("jmxName", JMX_DBCP2_DBPOOLS_ADDRESS + ",pool=" + JmxManager.normalizeJmxKey(dbKey)); } else if (databaseServerId != null) { String entityTitle = props.getProperty("hibernate.dbcp.dbserver.title"); dbcpProperties.put("jmxName", JMX_DBCP2_SERVERPOOLS_ADDRESS + ",pool=" + JmxManager.normalizeJmxKey(entityTitle)); } // Let the factory create the pool _ds = BasicDataSourceFactory.createDataSource(dbcpProperties); _ds.setLogExpiredConnections(false); // The BasicDataSource has lazy initialization // borrowing a connection will start the DataSource // and make sure it is configured correctly. Connection conn = _ds.getConnection(); conn.close(); // Create Legacy JMX monitoring information, provided by WGA if ("true".equals(props.getProperty("hibernate.dbcp.legacyJMX"))) { try { if (dbKey != null) { _entityKey = dbKey; _entityTitle = dbKey; _jmxManager = new JmxManager(new DBCPPoolInformation(this), new ObjectName(JMX_DBPOOLS_ADDRESS + ",pool=" + JmxManager.normalizeJmxKey(dbKey))); } else if (databaseServerId != null) { _server = true; _entityKey = databaseServerId; _entityTitle = (String) props.get("hibernate.dbcp.dbserver.title"); _jmxManager = new JmxManager(new DBCPPoolInformation(this), new ObjectName( JMX_SERVERPOOLS_ADDRESS + ",pool=" + JmxManager.normalizeJmxKey(_entityTitle))); } } catch (Throwable e) { log.error("Error enabling JMX metrics for connection pool", e); } } } catch (Exception e) { String message = "Could not create a DBCP pool"; if (_ds != null) { try { _ds.close(); } catch (Exception e2) { // ignore } _ds = null; } throw new HibernateException(message, e); } log.debug("Configure DBCPConnectionProvider complete"); }