List of usage examples for org.apache.commons.dbcp BasicDataSource getDriverClassName
public synchronized String getDriverClassName()
From source file:org.apache.openejb.assembler.classic.OpenEJBXmlByModuleTest.java
@Test public void test() throws Exception { assertNotNull(bean.datasource());/* w ww . j a va 2 s. c o m*/ assertTrue(bean.datasource() instanceof BasicDataSource); final BasicDataSource ds = (BasicDataSource) bean.datasource(); assertEquals("org.hsqldb.jdbcDriver", ds.getDriverClassName()); assertEquals("not:used:url", ds.getUrl()); assertEquals("foo", ds.getUsername()); assertEquals("bar", ds.getPassword()); assertNotNull(bean.resource()); assertEquals("ok", bean.resource().attr); }
From source file:org.jsconf.core.sample.Sample00.java
@Test public void test() { BasicDataSource basic = (BasicDataSource) this.datasource; Assert.assertEquals("jdbc:mysql://localhost:3306/test", basic.getUrl()); Assert.assertEquals("com.mysql.jdbc.Driver", basic.getDriverClassName()); }
From source file:org.jumpmind.symmetric.DbSqlCommand.java
@Override protected boolean executeWithOptions(CommandLine line) throws Exception { BasicDataSource basicDataSource = getDatabasePlatform(false).getDataSource(); String url = basicDataSource.getUrl(); String user = basicDataSource.getUsername(); String password = basicDataSource.getPassword(); String driver = basicDataSource.getDriverClassName(); Shell shell = new Shell(); if (line.hasOption(OPTION_SQL)) { String sql = line.getOptionValue(OPTION_SQL); shell.runTool("-url", url, "-user", user, "-password", password, "-driver", driver, "-sql", sql); } else {/*from www .ja v a 2 s .c om*/ shell.runTool("-url", url, "-user", user, "-password", password, "-driver", driver); } return true; }
From source file:org.mybatis.guice.datasource.dbcp.BasicDataSourceProviderTest.java
@Test public void get() throws Throwable { final String driver = "org.mybatis.guice.TestDriver"; final String url = "jdbc:h2:mem:testdb"; final String username = "test_user"; final String password = "test_password"; final boolean autoCommit = true; final Properties driverProperties = new Properties(); driverProperties.put("my_property", "true"); final boolean accessToUnderlyingConnectionAllowed = true; final String defaultCatalog = "test_catalog"; final boolean defaultReadOnly = true; final int defaultTransactionIsolation = 20; final int initialSize = 30; final int maxActive = 40; final int maxIdle = 50; final int maxOpenPreparedStatements = 60; final long maxWait = 70; final int minIdle = 80; final int numTestsPerEvictionRun = 90; final boolean poolPreparedStatements = true; final boolean testOnBorrow = true; final boolean testOnReturn = true; final boolean testWhileIdle = true; final int timeBetweenEvictionRunsMillis = 100; final String validationQuery = "SELECT 1"; Injector injector = Guice.createInjector(new AbstractModule() { @Override//from w ww . j a v a 2s . co m protected void configure() { bind(ClassLoader.class).annotatedWith(Names.named("JDBC.driverClassLoader")) .toInstance(driverClassLoader); bindConstant().annotatedWith(Names.named("JDBC.driver")).to(driver); bindConstant().annotatedWith(Names.named("JDBC.url")).to(url); bindConstant().annotatedWith(Names.named("JDBC.username")).to(username); bindConstant().annotatedWith(Names.named("JDBC.password")).to(password); bindConstant().annotatedWith(Names.named("JDBC.autoCommit")).to(autoCommit); bind(Properties.class).annotatedWith(Names.named("JDBC.driverProperties")) .toInstance(driverProperties); bindConstant().annotatedWith(Names.named("DBCP.accessToUnderlyingConnectionAllowed")) .to(accessToUnderlyingConnectionAllowed); bindConstant().annotatedWith(Names.named("DBCP.defaultCatalog")).to(defaultCatalog); bindConstant().annotatedWith(Names.named("DBCP.defaultReadOnly")).to(defaultReadOnly); bindConstant().annotatedWith(Names.named("DBCP.defaultTransactionIsolation")) .to(defaultTransactionIsolation); bindConstant().annotatedWith(Names.named("DBCP.initialSize")).to(initialSize); bindConstant().annotatedWith(Names.named("DBCP.maxActive")).to(maxActive); bindConstant().annotatedWith(Names.named("DBCP.maxIdle")).to(maxIdle); bindConstant().annotatedWith(Names.named("DBCP.maxOpenPreparedStatements")) .to(maxOpenPreparedStatements); bindConstant().annotatedWith(Names.named("DBCP.maxWait")).to(maxWait); bindConstant().annotatedWith(Names.named("DBCP.minIdle")).to(minIdle); bindConstant().annotatedWith(Names.named("DBCP.numTestsPerEvictionRun")).to(numTestsPerEvictionRun); bindConstant().annotatedWith(Names.named("DBCP.poolPreparedStatements")).to(poolPreparedStatements); bindConstant().annotatedWith(Names.named("DBCP.testOnBorrow")).to(testOnBorrow); bindConstant().annotatedWith(Names.named("DBCP.testOnReturn")).to(testOnReturn); bindConstant().annotatedWith(Names.named("DBCP.testWhileIdle")).to(testWhileIdle); bindConstant().annotatedWith(Names.named("DBCP.timeBetweenEvictionRunsMillis")) .to(timeBetweenEvictionRunsMillis); bindConstant().annotatedWith(Names.named("DBCP.validationQuery")).to(validationQuery); } }); BasicDataSourceProvider provider = injector.getInstance(BasicDataSourceProvider.class); BasicDataSource dataSource = (BasicDataSource) provider.get(); assertEquals(driverClassLoader, dataSource.getDriverClassLoader()); assertEquals(driver, dataSource.getDriverClassName()); assertEquals(url, dataSource.getUrl()); assertEquals(username, dataSource.getUsername()); assertEquals(password, dataSource.getPassword()); assertEquals(autoCommit, dataSource.getDefaultAutoCommit()); // Cannot test driver properties. assertEquals(accessToUnderlyingConnectionAllowed, dataSource.isAccessToUnderlyingConnectionAllowed()); assertEquals(defaultCatalog, dataSource.getDefaultCatalog()); assertEquals(defaultReadOnly, dataSource.getDefaultReadOnly()); assertEquals(defaultTransactionIsolation, dataSource.getDefaultTransactionIsolation()); assertEquals(initialSize, dataSource.getInitialSize()); assertEquals(maxActive, dataSource.getMaxActive()); assertEquals(maxIdle, dataSource.getMaxIdle()); assertEquals(maxOpenPreparedStatements, dataSource.getMaxOpenPreparedStatements()); assertEquals(maxWait, dataSource.getMaxWait()); assertEquals(minIdle, dataSource.getMinIdle()); assertEquals(numTestsPerEvictionRun, dataSource.getNumTestsPerEvictionRun()); assertEquals(poolPreparedStatements, dataSource.isPoolPreparedStatements()); assertEquals(testOnBorrow, dataSource.getTestOnBorrow()); assertEquals(testOnReturn, dataSource.getTestOnReturn()); assertEquals(testWhileIdle, dataSource.getTestWhileIdle()); assertEquals(timeBetweenEvictionRunsMillis, dataSource.getTimeBetweenEvictionRunsMillis()); assertEquals(validationQuery, dataSource.getValidationQuery()); }
From source file:org.mybatis.guice.datasource.dbcp.BasicDataSourceProviderTest.java
@Test public void get_OtherValues() throws Throwable { final String driver = "org.mybatis.guice.TestDriver2"; final String url = "jdbc:h2:mem:testdb2"; final String username = "test_user2"; final String password = "test_password2"; final boolean autoCommit = false; final Properties driverProperties = new Properties(); driverProperties.put("my_property", "false"); final boolean accessToUnderlyingConnectionAllowed = false; final String defaultCatalog = "test_catalog2"; final boolean defaultReadOnly = false; final int defaultTransactionIsolation = 21; final int initialSize = 31; final int maxActive = 41; final int maxIdle = 51; final int maxOpenPreparedStatements = 61; final long maxWait = 71; final int minIdle = 81; final int numTestsPerEvictionRun = 91; final boolean poolPreparedStatements = false; final boolean testOnBorrow = false; final boolean testOnReturn = false; final boolean testWhileIdle = false; final int timeBetweenEvictionRunsMillis = 101; final String validationQuery = "SELECT 2"; Injector injector = Guice.createInjector(new AbstractModule() { @Override/* www . j a va2 s .co m*/ protected void configure() { bind(ClassLoader.class).annotatedWith(Names.named("JDBC.driverClassLoader")) .toInstance(driverClassLoader); bindConstant().annotatedWith(Names.named("JDBC.driver")).to(driver); bindConstant().annotatedWith(Names.named("JDBC.url")).to(url); bindConstant().annotatedWith(Names.named("JDBC.username")).to(username); bindConstant().annotatedWith(Names.named("JDBC.password")).to(password); bindConstant().annotatedWith(Names.named("JDBC.autoCommit")).to(autoCommit); bind(Properties.class).annotatedWith(Names.named("JDBC.driverProperties")) .toInstance(driverProperties); bindConstant().annotatedWith(Names.named("DBCP.accessToUnderlyingConnectionAllowed")) .to(accessToUnderlyingConnectionAllowed); bindConstant().annotatedWith(Names.named("DBCP.defaultCatalog")).to(defaultCatalog); bindConstant().annotatedWith(Names.named("DBCP.defaultReadOnly")).to(defaultReadOnly); bindConstant().annotatedWith(Names.named("DBCP.defaultTransactionIsolation")) .to(defaultTransactionIsolation); bindConstant().annotatedWith(Names.named("DBCP.initialSize")).to(initialSize); bindConstant().annotatedWith(Names.named("DBCP.maxActive")).to(maxActive); bindConstant().annotatedWith(Names.named("DBCP.maxIdle")).to(maxIdle); bindConstant().annotatedWith(Names.named("DBCP.maxOpenPreparedStatements")) .to(maxOpenPreparedStatements); bindConstant().annotatedWith(Names.named("DBCP.maxWait")).to(maxWait); bindConstant().annotatedWith(Names.named("DBCP.minIdle")).to(minIdle); bindConstant().annotatedWith(Names.named("DBCP.numTestsPerEvictionRun")).to(numTestsPerEvictionRun); bindConstant().annotatedWith(Names.named("DBCP.poolPreparedStatements")).to(poolPreparedStatements); bindConstant().annotatedWith(Names.named("DBCP.testOnBorrow")).to(testOnBorrow); bindConstant().annotatedWith(Names.named("DBCP.testOnReturn")).to(testOnReturn); bindConstant().annotatedWith(Names.named("DBCP.testWhileIdle")).to(testWhileIdle); bindConstant().annotatedWith(Names.named("DBCP.timeBetweenEvictionRunsMillis")) .to(timeBetweenEvictionRunsMillis); bindConstant().annotatedWith(Names.named("DBCP.validationQuery")).to(validationQuery); } }); BasicDataSourceProvider provider = injector.getInstance(BasicDataSourceProvider.class); BasicDataSource dataSource = (BasicDataSource) provider.get(); assertEquals(driverClassLoader, dataSource.getDriverClassLoader()); assertEquals(driver, dataSource.getDriverClassName()); assertEquals(url, dataSource.getUrl()); assertEquals(username, dataSource.getUsername()); assertEquals(password, dataSource.getPassword()); assertEquals(autoCommit, dataSource.getDefaultAutoCommit()); // Cannot test driver properties. assertEquals(accessToUnderlyingConnectionAllowed, dataSource.isAccessToUnderlyingConnectionAllowed()); assertEquals(defaultCatalog, dataSource.getDefaultCatalog()); assertEquals(defaultReadOnly, dataSource.getDefaultReadOnly()); assertEquals(defaultTransactionIsolation, dataSource.getDefaultTransactionIsolation()); assertEquals(initialSize, dataSource.getInitialSize()); assertEquals(maxActive, dataSource.getMaxActive()); assertEquals(maxIdle, dataSource.getMaxIdle()); assertEquals(maxOpenPreparedStatements, dataSource.getMaxOpenPreparedStatements()); assertEquals(maxWait, dataSource.getMaxWait()); assertEquals(minIdle, dataSource.getMinIdle()); assertEquals(numTestsPerEvictionRun, dataSource.getNumTestsPerEvictionRun()); assertEquals(poolPreparedStatements, dataSource.isPoolPreparedStatements()); assertEquals(testOnBorrow, dataSource.getTestOnBorrow()); assertEquals(testOnReturn, dataSource.getTestOnReturn()); assertEquals(testWhileIdle, dataSource.getTestWhileIdle()); assertEquals(timeBetweenEvictionRunsMillis, dataSource.getTimeBetweenEvictionRunsMillis()); assertEquals(validationQuery, dataSource.getValidationQuery()); }
From source file:org.openkoala.koala.monitor.support.DbcpDataSourceCollector.java
public synchronized JdbcPoolStatusVo currentPoolStatus() { JdbcPoolStatusVo poolStatus = new JdbcPoolStatusVo(); try {/*from w ww .j av a2s . c o m*/ BasicDataSource realDS = (BasicDataSource) dataSoure; poolStatus.setDriverName(realDS.getDriverClassName()); // poolStatus.setStartTime(); poolStatus.setProvider(dataSoure.getClass().getName()); poolStatus.setInitConnectionCount(realDS.getInitialSize()); poolStatus.setMaxConnectionCount(realDS.getMaxActive()); poolStatus.setMaxActiveTime(realDS.getMaxWait()); poolStatus.setMaxOpenStatements(realDS.getMaxOpenPreparedStatements()); poolStatus.setSnapshotTime(new Date()); poolStatus.setIdleConnectionCount(realDS.getNumIdle()); poolStatus.setActiveConnectionCount(realDS.getNumActive()); } catch (Exception e) { poolStatus.setErrorTip("??"); } return poolStatus; }
From source file:org.unitils.database.core.DataSourceWrapperFactoryCreateTest.java
@Test public void create() throws Exception { DataSourceWrapper result = dataSourceWrapperFactory.create(databaseConfiguration); assertSame(databaseConfiguration, result.getDatabaseConfiguration()); DataSource dataSource = result.getDataSource(false); BasicDataSource basicDataSource = (BasicDataSource) dataSource; assertTrue(basicDataSource.isAccessToUnderlyingConnectionAllowed()); assertEquals("driver", basicDataSource.getDriverClassName()); assertEquals("user", basicDataSource.getUsername()); assertEquals("pass", basicDataSource.getPassword()); assertEquals("url", basicDataSource.getUrl()); }
From source file:org.unitils.database.DatabaseUnitils.java
/** * This method gets a {@link Connection} from the {@link DataSource} and checks if it is a {@link oracle.jdbc.driver.OracleConnection}. * There is a bug with commons-dbcp 1.4: if you want to create a {@link oracle.sql.BLOB} or a {@link java.sql.Clob} than you must get the inner {@link Connection} but you get another {@link Connection}. * This is fixed in this method./*from w w w .j a v a2 s. com*/ * @param connection * @param dataSource * @return */ public static Connection getGoodConnection(Connection connection, DataSource dataSource) { if (dataSource instanceof BasicDataSource) { BasicDataSource tempDataSource = (BasicDataSource) dataSource; if (tempDataSource.getDriverClassName().toLowerCase().contains("oracle") && connection instanceof DelegatingConnection) { boolean canAccess = tempDataSource.isAccessToUnderlyingConnectionAllowed(); if (!canAccess) { tempDataSource.setAccessToUnderlyingConnectionAllowed(true); } DelegatingConnection tempConnection = (DelegatingConnection) connection; Connection innermostDelegate = tempConnection.getInnermostDelegate(); if (!canAccess) { tempDataSource.setAccessToUnderlyingConnectionAllowed(false); } return innermostDelegate; } } return connection; }
From source file:oscar.util.SqlUtils.java
private static DatabaseTypes getDatabaseType() { BasicDataSource basicDataSource = (BasicDataSource) SpringUtils.getBean("dataSource"); String driverName = basicDataSource.getDriverClassName(); if (driverName.startsWith("com.mysql.")) return (DatabaseTypes.MYSQL); if (driverName.startsWith("org.postgresql.")) return (DatabaseTypes.POSTGRESQL); if (driverName.startsWith("oracle.")) return (DatabaseTypes.ORACLE); else/*from ww w . j a v a 2 s.c o m*/ throw (new IllegalArgumentException("Need a new database driver type added : " + driverName)); }