List of usage examples for org.apache.commons.dbcp2 PoolingDataSource getConnection
@Override public Connection getConnection() throws SQLException
From source file:com.linuxrouter.netcool.test.DbPoolTest.java
public static void main(String[] args) { String host = "192.168.0.201"; String port = "4100"; String dbName = "alerts"; String url = "jdbc:sybase:Tds:" + host + ":" + port + "/" + dbName; Driver drv = new com.sybase.jdbc3.jdbc.SybDriver(); try {/* w ww . jav a 2s .c o m*/ DriverManager.registerDriver(drv); } catch (SQLException ex) { Logger.getLogger(DbPoolTest.class.getName()).log(Level.SEVERE, null, ex); } ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, "root", "omni12@#"); PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null); ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory); poolableConnectionFactory.setPool(connectionPool); PoolingDataSource<PoolableConnection> poolingDataSource = new PoolingDataSource<>(connectionPool); try { Connection con = poolingDataSource.getConnection(); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("select * from alerts.status"); int x = 0; while (rs.next()) { //System.out.println(":::" + rs.getString(1)); x++; } System.out.println("::::::" + x); } catch (SQLException ex) { Logger.getLogger(DbPoolTest.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.parallax.server.blocklyprop.config.PersistenceModule.java
@Provides PoolingDataSource dataSource() throws ClassNotFoundException { PoolingDataSource ds = DataSourceSetup.connect(configuration); try {/* www . j av a 2 s . c o m*/ ds.getConnection(); } catch (SQLException ex) { Logger.getLogger(PersistenceModule.class.getName()).log(Level.SEVERE, null, ex); } return ds; }
From source file:eu.peppol.persistence.jdbc.OxalisDataSourceFactoryDbcpImplTest.java
/** * Verifies that we can create a pooled jdbc data source using the JDBC .jar-file supplied in the global configuration * file./*from w ww .j a va 2 s. c o m*/ * * @throws Exception */ @Test public void testLoadJdbcDriverUsingCustomClassLoader() throws Exception { ConnectionFactory driverConnectionFactory = createConnectionFactory(false); ObjectName poolName = new ObjectName("no.difi.oxalis", "connectionPool", "TestPool"); PoolableConnectionFactory factory = new PoolableConnectionFactory(driverConnectionFactory, poolName); GenericObjectPool<PoolableConnection> pool = new GenericObjectPool<>(factory); factory.setPool(pool); pool.setMaxTotal(10); pool.setMaxWaitMillis(100); assertEquals(pool.getFactory(), factory); PoolableConnectionFactory pcf = (PoolableConnectionFactory) ((GenericObjectPool<?>) pool).getFactory(); ObjectPool<PoolableConnection> pool1 = pcf.getPool(); PoolingDataSource<PoolableConnection> poolingDataSource = new PoolingDataSource<>(pool); Connection connection = poolingDataSource.getConnection(); assertNotNull(connection); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("select current_date()"); assertTrue(resultSet.next()); }
From source file:eu.peppol.persistence.jdbc.OxalisDataSourceFactoryDbcpImplTest.java
private void runTwoSqlStatementsWithTwoConnections(PoolingDataSource poolingDataSource) throws SQLException, InterruptedException { Connection connection = poolingDataSource.getConnection(); if (connection.getMetaData().getDatabaseProductName().toLowerCase().contains("mysql")) { assertNotNull(connection);/*from w ww.j ava 2 s. c o m*/ Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("select current_date()"); statement = connection.createStatement(); statement.execute("set session wait_timeout=1"); assertTrue(resultSet.next()); connection.close(); // return to pool // Wait for 2 seconds System.err.print("Sleeping for 2 seconds...."); Thread.sleep(2 * 1000L); System.err.println("Running again now"); connection = poolingDataSource.getConnection(); statement = connection.createStatement(); resultSet = statement.executeQuery("select current_time()"); } }