List of usage examples for org.apache.commons.dbcp BasicDataSource BasicDataSource
BasicDataSource
From source file:org.apache.synapse.config.xml.AbstractDBMediatorFactory.java
/** * Create a custom DataSource using the specified properties and Apache DBCP * @param pool the toplevel 'pool' element that holds DataSource information * @param mediator the mediator to store properties for serialization * @return a DataSource created using specified properties */// w w w . j a v a 2 s . c om private DataSource createCustomDataSource(OMElement pool, AbstractDBMediator mediator) { BasicDataSource ds = new BasicDataSource(); // load the minimum required properties ds.setDriverClassName(getValue(pool, DRIVER_Q)); ds.setUsername(getValue(pool, USER_Q)); ds.setPassword(getValue(pool, PASS_Q)); ds.setUrl(getValue(pool, URL_Q)); //save loaded properties for later mediator.addDataSourceProperty(DRIVER_Q, getValue(pool, DRIVER_Q)); mediator.addDataSourceProperty(URL_Q, getValue(pool, URL_Q)); mediator.addDataSourceProperty(USER_Q, getValue(pool, USER_Q)); mediator.addDataSourceProperty(PASS_Q, getValue(pool, PASS_Q)); Iterator props = pool.getChildrenWithName(PROP_Q); while (props.hasNext()) { OMElement prop = (OMElement) props.next(); String name = prop.getAttribute(ATT_NAME).getAttributeValue(); String value = prop.getAttribute(ATT_VALUE).getAttributeValue(); // save property for later mediator.addDataSourceProperty(name, value); if ("autocommit".equals(name)) { if ("true".equals(value)) { ds.setDefaultAutoCommit(true); } else if ("false".equals(value)) { ds.setDefaultAutoCommit(false); } } else if ("isolation".equals(name)) { try { if ("Connection.TRANSACTION_NONE".equals(value)) { ds.setDefaultTransactionIsolation(Connection.TRANSACTION_NONE); } else if ("Connection.TRANSACTION_READ_COMMITTED".equals(value)) { ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED); } else if ("Connection.TRANSACTION_READ_UNCOMMITTED".equals(value)) { ds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); } else if ("Connection.TRANSACTION_REPEATABLE_READ".equals(value)) { ds.setDefaultTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); } else if ("Connection.TRANSACTION_SERIALIZABLE".equals(value)) { ds.setDefaultTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); } } catch (NumberFormatException ignore) { } } else if ("initialsize".equals(name)) { try { ds.setInitialSize(Integer.parseInt(value)); } catch (NumberFormatException ignore) { } } else if ("maxactive".equals(name)) { try { ds.setMaxActive(Integer.parseInt(value)); } catch (NumberFormatException ignore) { } } else if ("maxidle".equals(name)) { try { ds.setMaxIdle(Integer.parseInt(value)); } catch (NumberFormatException ignore) { } } else if ("maxopenstatements".equals(name)) { try { ds.setMaxOpenPreparedStatements(Integer.parseInt(value)); } catch (NumberFormatException ignore) { } } else if ("maxwait".equals(name)) { try { ds.setMaxWait(Long.parseLong(value)); } catch (NumberFormatException ignore) { } } else if ("minidle".equals(name)) { try { ds.setMinIdle(Integer.parseInt(value)); } catch (NumberFormatException ignore) { } } else if ("poolstatements".equals(name)) { if ("true".equals(value)) { ds.setPoolPreparedStatements(true); } else if ("false".equals(value)) { ds.setPoolPreparedStatements(false); } } else if ("testonborrow".equals(name)) { if ("true".equals(value)) { ds.setTestOnBorrow(true); } else if ("false".equals(value)) { ds.setTestOnBorrow(false); } } else if ("testonreturn".equals(name)) { if ("true".equals(value)) { ds.setTestOnReturn(true); } else if ("false".equals(value)) { ds.setTestOnReturn(false); } } else if ("testwhileidle".equals(name)) { if ("true".equals(value)) { ds.setTestWhileIdle(true); } else if ("false".equals(value)) { ds.setTestWhileIdle(false); } } else if ("validationquery".equals(name)) { ds.setValidationQuery(value); } } return ds; }
From source file:org.apache.taverna.configuration.database.impl.DatabaseManagerImpl.java
private void setupDataSource() { setDerbyPaths();//from ww w . j ava2 s .com dataSource = new BasicDataSource(); dataSource.setDriverClassName(databaseConfiguration.getDriverClassName()); System.setProperty("hibernate.dialect", databaseConfiguration.getHibernateDialect()); dataSource.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED); dataSource.setMaxActive(databaseConfiguration.getPoolMaxActive()); dataSource.setMinIdle(databaseConfiguration.getPoolMinIdle()); dataSource.setMaxIdle(databaseConfiguration.getPoolMaxIdle()); dataSource.setDefaultAutoCommit(true); dataSource.setInitialSize(databaseConfiguration.getPoolMinIdle()); //Derby blows up if the username of password is empty (even an empty string thats not null). if (databaseConfiguration.getUsername() != null && databaseConfiguration.getUsername().length() >= 1) dataSource.setUsername(databaseConfiguration.getUsername()); if (databaseConfiguration.getPassword() != null && databaseConfiguration.getPassword().length() >= 1) dataSource.setPassword(databaseConfiguration.getPassword()); dataSource.setUrl(databaseConfiguration.getJDBCUri()); }
From source file:org.apache.taverna.provenance.api.ProvenanceAccess.java
/** * Initialises a named JNDI DataSource if not already set up externally. The * DataSource is named jdbc/taverna/* ww w .j a va 2 s.c o m*/ * * @param driverClassName * - the classname for the driver to be used. * @param jdbcUrl * - the jdbc connection url * @param username * - the username, if required (otherwise null) * @param password * - the password, if required (oteherwise null) * @param minIdle * - if the driver supports multiple connections, then the * minumum number of idle connections in the pool * @param maxIdle * - if the driver supports multiple connections, then the * maximum number of idle connections in the pool * @param maxActive * - if the driver supports multiple connections, then the * minumum number of connections in the pool */ public static void initDataSource(String driverClassName, String jdbcUrl, String username, String password, int minIdle, int maxIdle, int maxActive) { System.setProperty(INITIAL_CONTEXT_FACTORY, "org.osjava.sj.memory.MemoryContextFactory"); System.setProperty("org.osjava.sj.jndi.shared", "true"); BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(driverClassName); ds.setDefaultTransactionIsolation(TRANSACTION_READ_UNCOMMITTED); ds.setMaxActive(maxActive); ds.setMinIdle(minIdle); ds.setMaxIdle(maxIdle); ds.setDefaultAutoCommit(true); if (username != null) ds.setUsername(username); if (password != null) ds.setPassword(password); ds.setUrl(jdbcUrl); try { new InitialContext().rebind("jdbc/taverna", ds); } catch (NamingException ex) { logger.error("Problem rebinding the jdbc context", ex); } }
From source file:org.apache.torque.JndiConfigurationTest.java
/** * Creates a Data Source from the Torque configuration without using Torque. * @return a SharedPoolDataSource source. * @throws Exception if we cannot create a Data source. *//* w w w . j av a 2s.co m*/ protected BasicDataSource getDataSource() throws Exception { Configuration torqueConfiguration = getTorqueConfiguraton(); String defaultDatabase = getDefaultDatabase(torqueConfiguration); Configuration dsfactoryConfiguration = torqueConfiguration .subset(Torque.TORQUE_KEY + "." + DataSourceFactory.DSFACTORY_KEY + "." + defaultDatabase + "." + AbstractDataSourceFactory.CONNECTION_KEY); BasicDataSource dataSource = new BasicDataSource(); for (Iterator i = dsfactoryConfiguration.getKeys(); i.hasNext();) { String key = (String) i.next(); String stringValue = dsfactoryConfiguration.getString(key); if ("user".equals(key)) { // setUser() in SharedPoolDataSouce corresponds to // setUsername() in BasicDataSourceFactory key = "username"; } else if ("driver".equals(key)) { // setDriver() in SharedPoolDataSouce corresponds to // setDriverClassName() in BasicDataSourceFactory key = "driverClassName"; } Class propertyType = PropertyUtils.getPropertyType(dataSource, key); Object value = ConvertUtils.convert(stringValue, propertyType); PropertyUtils.setSimpleProperty(dataSource, key, value); } return dataSource; }
From source file:org.apache.wookie.server.Start.java
private static void configureServer() throws Exception { // create embedded jetty instance logger.info("Configuring Jetty server"); server = new Server(port); // configure embedded jetty to handle wookie web application WebAppContext context = new WebAppContext(); context.setServer(server);/*from w ww . jav a2s .c om*/ context.setContextPath("/wookie"); context.setWar("build/webapp/wookie"); // enable and configure JNDI container resources context.setConfigurationClasses(new String[] { "org.mortbay.jetty.webapp.WebInfConfiguration", "org.mortbay.jetty.plus.webapp.EnvConfiguration", "org.mortbay.jetty.plus.webapp.Configuration", "org.mortbay.jetty.webapp.JettyWebXmlConfiguration", "org.mortbay.jetty.webapp.TagLibConfiguration" }); if (persistenceManagerType.equals(PERSISTENCE_MANAGER_TYPE_JPA)) { logger.info("Configuring JPA persistence manager"); // setup derby database directory and logging properties if (dbType.equals("derby") && dbUri.startsWith("jdbc:derby:")) { int dbUriArgsIndex = dbUri.indexOf(";", 11); if (dbUriArgsIndex == -1) { dbUriArgsIndex = dbUri.length(); } String databasePath = dbUri.substring(11, dbUriArgsIndex); int databaseDirIndex = databasePath.lastIndexOf(File.separatorChar); if ((databaseDirIndex == -1) && (File.separatorChar != '/')) { databaseDirIndex = databasePath.lastIndexOf('/'); } if (databaseDirIndex != -1) { String databaseDir = databasePath.substring(0, databaseDirIndex); File databaseDirFile = new File(databaseDir); if (!databaseDirFile.exists()) { databaseDirFile.mkdirs(); } String derbyLog = databaseDirFile.getAbsolutePath() + File.separator + "derby.log"; System.setProperty("derby.stream.error.file", derbyLog); } } // Setup a database connection resource using DBCP BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(dbDriver); dataSource.setUrl(dbUri); dataSource.setUsername(dbUser); dataSource.setPassword(dbPassword); dataSource.setMaxActive(80); dataSource.setMaxIdle(80); dataSource.setInitialSize(5); dataSource.setMaxOpenPreparedStatements(0); // Set up connection pool GenericObjectPool pool = new GenericObjectPool(); // setup factory and pooling DataSource DataSourceConnectionFactory factory = new DataSourceConnectionFactory(dataSource); @SuppressWarnings("unused") PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(factory, pool, null, null, false, true); DataSource poolingDataSource = new PoolingDataSource(pool); new Resource(JPAPersistenceManager.WIDGET_DATABASE_JNDI_DATASOURCE_NAME, poolingDataSource); } else if (persistenceManagerType.equals(PERSISTENCE_MANAGER_TYPE_JCR)) { logger.info("Configuring JCR persistence manager"); // setup repository directory and derby logging properties File repositoryDirFile = new File("widgetRepository"); if (!repositoryDirFile.exists()) { repositoryDirFile.mkdirs(); } String derbyLog = repositoryDirFile.getAbsolutePath() + File.separator + "derby.log"; System.setProperty("derby.stream.error.file", derbyLog); // setup Jackrabbit JCR repository JNDI resource String repositoryConfig = repositoryDirFile.getAbsolutePath() + File.separator + "repository.xml"; Repository repository = new TransientRepository(repositoryConfig, repositoryDirFile.getAbsolutePath()); new Resource(JCRPersistenceManager.WIDGET_REPOSITORY_JNDI_REPOSITORY_NAME, repository); } // configure embedded jetty web application handler server.addHandler(context); // configure embedded jetty authentication realm HashUserRealm authedRealm = new HashUserRealm("Authentication Required", "etc/jetty-realm.properties"); server.setUserRealms(new UserRealm[] { authedRealm }); logger.info("Configured Jetty server"); }
From source file:org.apigw.monitoring.config.PersistenceConfig.java
@Bean public DataSource dataSource() { log.debug("Setting up datasource for with drive: {}, url: {}, username: {}", driver, url, username); BasicDataSource bdc = new BasicDataSource(); bdc.setUsername(username);/*ww w .j av a2 s. c o m*/ bdc.setPassword(password); bdc.setUrl(url); bdc.setDriverClassName(driver); bdc.setValidationQuery(validationQuery); return bdc; }
From source file:org.asimba.wa.integrationtest.server.AsimbaWaDerbyDb.java
private AsimbaWaDerbyDb() { String driverClassName = RunConfiguration.getInstance().getProperty("db.driverClass"); String url = RunConfiguration.getInstance().getProperty("db.connectionString"); String username = RunConfiguration.getInstance().getProperty("db.username"); String password = RunConfiguration.getInstance().getProperty("db.password"); BasicDataSource ds = null;//from ww w.ja v a 2 s. c o m ds = new BasicDataSource(); ds.setDriverClassName(driverClassName); ds.setUrl(url); ds.setUsername(username); ds.setPassword(password); _datasource = ds; }
From source file:org.asqatasun.tgol.test.scenario.AbstractWebDriverTestClass.java
/** * Initialises the Db connection //ww w . ja v a2 s. com */ private void initDb() { dataSource = new BasicDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUsername(dbUser); dataSource.setPassword(dbPassword); dataSource.setUrl("jdbc:mysql://" + dbUrl + "/" + dbName); dataSource.setMaxActive(10); dataSource.setMaxIdle(5); dataSource.setInitialSize(5); }
From source file:org.athrun.android.framework.agent.common.DBCommandRunner.java
private String connect(String command) throws Exception { if (connection != null) { connection.close();/*from w ww .ja v a 2 s .c o m*/ } String namePass = command.split("@")[0]; String name = namePass.split(":")[0]; String password = namePass.split(":")[1]; String connectionStr = command.substring(namePass.length() + 1); System.out.println("+++" + connectionStr); String driverClassName = connectionStr.split(",")[0]; String connectionUrl = connectionStr.split(",")[1]; ds = new BasicDataSource(); System.out.print(driverClassName + "--" + connectionUrl + "--" + name + "--" + password); ds.setDriverClassName(driverClassName); ds.setUrl(connectionUrl); ds.setUsername(name); ds.setPassword(password); connection = ds.getConnection(); return null; }
From source file:org.blocks4j.reconf.client.setup.DatabaseManager.java
private BasicDataSource createDataSource(DatabaseURL arg) { BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName(arg.getDriverClassName()); ds.setUrl(arg.buildRuntimeURL());//from www . j a va 2s. c o m ds.setUsername(arg.getLogin()); ds.setPassword(arg.getPass()); ds.setMaxActive(30); ds.setMinIdle(5); ds.setTestOnBorrow(true); ds.setMaxWait(5000); return ds; }