List of usage examples for org.apache.commons.dbcp BasicDataSource setUsername
public synchronized void setUsername(String username)
Sets the #username .
Note: this method currently has no effect once the pool has been initialized.
From source file:net.firejack.platform.model.config.hibernate.HibernateFactoryBean.java
private void buildHibernateTransactionManager() { for (Map.Entry<String, Database> entry : databases.entrySet()) { String name = entry.getKey(); Database database = entry.getValue(); BasicDataSource dataSource = null; try {/*from w w w. j a va 2 s .co m*/ dataSource = context.getBean(name + HIBERNATE_DATA_SOURCE_SUFFIX, BasicDataSource.class); } catch (BeansException e) { logger.info("Manual Hibernate DataSource: " + name + HIBERNATE_DATA_SOURCE_SUFFIX + " not found use default"); } if (dataSource == null) { dataSource = defaultHibernate.getBean(HIBERNATE_DEFAULT_PREFIX + HIBERNATE_DATA_SOURCE_SUFFIX, BasicDataSource.class); dataSource.setDriverClassName(database.type.getDriver()); dataSource.setUrl(database.getUrl()); dataSource.setUsername(database.getUsername()); dataSource.setPassword(database.getPassword()); dataSource.setValidationQuery(database.getType().getValidate()); context.getBeanFactory().registerSingleton(name + HIBERNATE_DATA_SOURCE_SUFFIX, dataSource); } Properties properties = null; try { properties = context.getBean(name + HIBERNATE_PROPERTIES_SUFFIX, Properties.class); } catch (BeansException e) { logger.info("Manual Hibernate properties: " + name + HIBERNATE_PROPERTIES_SUFFIX + " not found use default"); } if (properties == null) { properties = defaultHibernate.getBean(HIBERNATE_DEFAULT_PREFIX + HIBERNATE_PROPERTIES_SUFFIX, Properties.class); properties.put(Environment.DIALECT, database.getType().getDialect()); context.getBeanFactory().registerSingleton(name + HIBERNATE_PROPERTIES_SUFFIX, properties); } SessionFactory sessionFactory = null; try { sessionFactory = context.getBean(name + HIBERNATE_SESSION_FACTORY_SUFFIX, SessionFactory.class); } catch (BeansException e) { logger.info("Manual Hibernate Session Factory: " + name + HIBERNATE_SESSION_FACTORY_SUFFIX + " not found use default"); } if (sessionFactory == null) { try { AnnotationSessionFactoryBean annotationSessionFactoryBean = new AnnotationSessionFactoryBean(); annotationSessionFactoryBean.setAnnotatedClasses(new Class[] { BaseEntityModel.class }); annotationSessionFactoryBean.setDataSource(dataSource); annotationSessionFactoryBean.setHibernateProperties(properties); annotationSessionFactoryBean.setPackagesToScan(database.getScanPackages()); annotationSessionFactoryBean.setNamingStrategy(new OpenFlameNamingStrategy(database.getType())); if (database.getType() == DatabaseName.MySQL || database.getType() == DatabaseName.MSSQL) { annotationSessionFactoryBean.setLobHandler(new DefaultLobHandler()); } else if (database.getType() == DatabaseName.Oracle) { annotationSessionFactoryBean.setLobHandler(new OracleLobHandler()); } disposableBeans.add(annotationSessionFactoryBean); annotationSessionFactoryBean.afterPropertiesSet(); sessionFactory = annotationSessionFactoryBean.getObject(); } catch (Exception e) { logger.error(e, e); } } HibernateTemplate template = new HibernateTemplate(); template.setSessionFactory(sessionFactory); context.getBeanFactory().registerSingleton(name + HIBERNATE_TEMPLATE_PREFIX, template); database.setHibernateSupport(template); logger.info("Initialize Hibernate support for stories: " + database); String beanName = name + HIBERNATE_TRANSACTION_MANAGER_SUFFIX; ConstructorArgumentValues values = new ConstructorArgumentValues(); values.addGenericArgumentValue(sessionFactory); RootBeanDefinition definition = new RootBeanDefinition(HibernateTransactionManager.class); definition.setConstructorArgumentValues(values); registers.put(beanName, definition); Collection<HibernateSupport> stores = database.getStores(); for (HibernateSupport store : stores) { targets.put(((Advised) store).getTargetClass(), beanName); } } }
From source file:com.googlecode.wmbutil.jdbc.DataSourceLocator.java
/** * Looks up a named data source/*from w w w .ja v a 2 s . c om*/ * * @param dataSourceName * The name of the data source to look for * @return The data source * @throws RuntimeException * If the configuration can not be loaded or the data source * definition is missing */ public synchronized DataSource lookup(String dataSourceName) { BasicDataSource ds; if (dataSources.containsKey(dataSourceName)) { ds = (BasicDataSource) dataSources.get(dataSourceName); if (ds == null) { // we failed to create it earlier throw new RuntimeException("Failed to create data source"); } } else { ds = new BasicDataSource(); try { String driver = config.getProperty("jdbc." + dataSourceName + ".class"); if (driver == null || driver.trim().length() == 0) { throw new RuntimeException("Lookup connections configuration file must contain a jdbc." + dataSourceName + ".class value"); } else { ds.setDriverClassName(driver); } String url = config.getProperty("jdbc." + dataSourceName + ".url"); if (url == null || url.trim().length() == 0) { throw new RuntimeException("Lookup connections configuration file must contain a jdbc." + dataSourceName + ".url value"); } else { ds.setUrl(url); } String username = config.getProperty("jdbc." + dataSourceName + ".username"); if (username == null || username.trim().length() == 0) { throw new RuntimeException("Lookup connections configuration file must contain a jdbc." + dataSourceName + ".username value"); } else { ds.setUsername(username); } ds.setPassword(config.getProperty("jdbc." + dataSourceName + ".password")); ds.setDefaultReadOnly(true); dataSources.put(dataSourceName, ds); } catch (RuntimeException e) { // mark failed to create dataSources.put(dataSourceName, null); throw e; } } return ds; }
From source file:cz.muni.fi.pv168.MainForm.java
private DataSource prepareDataSource() { /*/*from w w w. j a va 2s .c o m*/ * BasicDataSource ds = new BasicDataSource(); Properties properties = * new Properties(); FileInputStream in = null; try { in = new * FileInputStream("database.properties"); } catch * (FileNotFoundException ex) { * Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, "File * Not Found", ex); } try { properties.load(in); } catch (IOException * ex) { Logger.getLogger(MainForm.class.getName()).log(Level.SEVERE, * "Can Not Read Properties File", ex); } String drivers = * properties.getProperty("jdbc.drivers"); if (null == drivers) { * System.setProperty("jdbs.drivers", drivers); } String url = * properties.getProperty("jdbc.url"); String username = * properties.getProperty("jdbc.username"); String password = * properties.getProperty("jdbc.password"); ds.setUrl(url); * ds.setUsername(username); ds.setPassword(password); return ds; */ ResourceBundle databaseProperties = ResourceBundle.getBundle("cz.muni.fi.pv168.database"); String url = databaseProperties.getString("jdbc.url"); String username = databaseProperties.getString("jdbc.username"); String password = databaseProperties.getString("jdbc.password"); BasicDataSource ds = new BasicDataSource(); ds.setUrl(url); ds.setUsername(username); ds.setPassword(password); try { DBUtils.tryCreateTables(ds); } catch (SQLException ex) { JOptionPane.showMessageDialog(jMenu1, localization.getString("db_connection_failure"), localization.getString("error"), JOptionPane.ERROR_MESSAGE); } carManager.setDataSource(ds); customerManager.setDataSource(ds); rentManager.setDataSource(ds); return ds; }
From source file:com.alibaba.otter.manager.biz.common.DataSourceCreator.java
private DataSource createDataSource(String url, String userName, String password, String driverClassName, DataMediaType dataMediaType, String encoding) { BasicDataSource dbcpDs = new BasicDataSource(); dbcpDs.setInitialSize(initialSize);// ? dbcpDs.setMaxActive(maxActive);// ????? dbcpDs.setMaxIdle(maxIdle);// ?? dbcpDs.setMinIdle(minIdle);// ?0? dbcpDs.setMaxWait(maxWait);// ??-1? dbcpDs.setRemoveAbandoned(true);// ??removeAbandonedTimeout dbcpDs.setLogAbandoned(true);// ?? dbcpDs.setRemoveAbandonedTimeout(removeAbandonedTimeout); // ? dbcpDs.setNumTestsPerEvictionRun(numTestsPerEvictionRun);// ?? dbcpDs.setTestOnBorrow(false);// ?? dbcpDs.setTestOnReturn(false);// ?? dbcpDs.setTestWhileIdle(true);// ???? dbcpDs.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // ???????? dbcpDs.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); // ??????? // ??//w ww . ja v a 2s .c o m dbcpDs.setDriverClassName(driverClassName); dbcpDs.setUrl(url); dbcpDs.setUsername(userName); dbcpDs.setPassword(password); if (dataMediaType.isOracle()) { dbcpDs.addConnectionProperty("restrictGetTables", "true"); // dbcpDs.setValidationQuery("select 1 from dual"); } else if (dataMediaType.isMysql()) { // open the batch mode for mysql since 5.1.8 dbcpDs.addConnectionProperty("useServerPrepStmts", "false"); dbcpDs.addConnectionProperty("rewriteBatchedStatements", "true"); dbcpDs.addConnectionProperty("zeroDateTimeBehavior", "convertToNull");// 0000-00-00null dbcpDs.addConnectionProperty("yearIsDateType", "false");// ??year?date? dbcpDs.addConnectionProperty("noDatetimeStringSync", "true");// ,??? if (StringUtils.isNotEmpty(encoding)) { if (StringUtils.equalsIgnoreCase(encoding, "utf8mb4")) { dbcpDs.addConnectionProperty("characterEncoding", "utf8"); dbcpDs.setConnectionInitSqls(Arrays.asList("set names utf8mb4")); } else { dbcpDs.addConnectionProperty("characterEncoding", encoding); } } // dbcpDs.setValidationQuery("select 1"); } else { logger.error("ERROR ## Unknow database type"); } return dbcpDs; }
From source file:com.alibaba.otter.node.etl.common.datasource.impl.DBDataSourceService.java
private DataSource createDataSource(String url, String userName, String password, String driverClassName, DataMediaType dataMediaType, String encoding) { BasicDataSource dbcpDs = new BasicDataSource(); dbcpDs.setInitialSize(initialSize);// ? dbcpDs.setMaxActive(maxActive);// ????? dbcpDs.setMaxIdle(maxIdle);// ?? dbcpDs.setMinIdle(minIdle);// ?0? dbcpDs.setMaxWait(maxWait);// ??-1? dbcpDs.setRemoveAbandoned(true);// ??removeAbandonedTimeout dbcpDs.setLogAbandoned(true);// ?? dbcpDs.setRemoveAbandonedTimeout(removeAbandonedTimeout); // ? dbcpDs.setNumTestsPerEvictionRun(numTestsPerEvictionRun);// ?? dbcpDs.setTestOnBorrow(false);// ?? dbcpDs.setTestOnReturn(false);// ?? dbcpDs.setTestWhileIdle(true);// ???? dbcpDs.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // ???????? dbcpDs.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); // ??????? // ??// www . ja v a 2 s. c om dbcpDs.setDriverClassName(driverClassName); dbcpDs.setUrl(url); dbcpDs.setUsername(userName); dbcpDs.setPassword(password); if (dataMediaType.isOracle()) { dbcpDs.addConnectionProperty("restrictGetTables", "true"); dbcpDs.setValidationQuery("select 1 from dual"); } else if (dataMediaType.isMysql()) { // open the batch mode for mysql since 5.1.8 dbcpDs.addConnectionProperty("useServerPrepStmts", "false"); dbcpDs.addConnectionProperty("rewriteBatchedStatements", "true"); dbcpDs.addConnectionProperty("zeroDateTimeBehavior", "convertToNull");// 0000-00-00null dbcpDs.addConnectionProperty("yearIsDateType", "false");// ??year?date? dbcpDs.addConnectionProperty("noDatetimeStringSync", "true");// ,??? if (StringUtils.isNotEmpty(encoding)) { if (StringUtils.equalsIgnoreCase(encoding, "utf8mb4")) { dbcpDs.addConnectionProperty("characterEncoding", "utf8"); dbcpDs.setConnectionInitSqls(Arrays.asList("set names utf8mb4")); } else { dbcpDs.addConnectionProperty("characterEncoding", encoding); } } dbcpDs.setValidationQuery("select 1"); } else { logger.error("ERROR ## Unknow database type"); } return dbcpDs; }
From source file:com.alibaba.otter.common.push.datasource.media.MediaPushDataSource.java
protected DataSource doCreateDataSource(String url) { BasicDataSource dbcpDs = new BasicDataSource(); dbcpDs.setInitialSize(initialSize);// ? dbcpDs.setMaxActive(maxActive);// ????? dbcpDs.setMaxIdle(maxIdle);// ?? dbcpDs.setMinIdle(minIdle);// ?0? dbcpDs.setMaxWait(maxWait);// ??-1? dbcpDs.setRemoveAbandoned(true);// ??removeAbandonedTimeout dbcpDs.setLogAbandoned(true);// ?? dbcpDs.setRemoveAbandonedTimeout(removeAbandonedTimeout); // ? dbcpDs.setNumTestsPerEvictionRun(numTestsPerEvictionRun);// ?? dbcpDs.setTestOnBorrow(false);// ?? dbcpDs.setTestOnReturn(false);// ?? dbcpDs.setTestWhileIdle(true);// ???? dbcpDs.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis); // ???????? dbcpDs.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis); // ??????? // ??//from w w w.jav a 2 s . c o m dbcpDs.setDriverClassName(driverClassName); dbcpDs.setUrl(url); dbcpDs.setUsername(userName); dbcpDs.setPassword(password); if (dataMediaType.isOracle()) { dbcpDs.addConnectionProperty("restrictGetTables", "true"); dbcpDs.setValidationQuery("select 1 from dual"); } else if (dataMediaType.isMysql()) { // open the batch mode for mysql since 5.1.8 dbcpDs.addConnectionProperty("useServerPrepStmts", "false"); dbcpDs.addConnectionProperty("rewriteBatchedStatements", "true"); dbcpDs.addConnectionProperty("zeroDateTimeBehavior", "convertToNull");// 0000-00-00null dbcpDs.addConnectionProperty("yearIsDateType", "false");// ??year?date? if (StringUtils.isNotEmpty(encoding)) { dbcpDs.addConnectionProperty("characterEncoding", encoding); } dbcpDs.setValidationQuery("select 1"); } else { logger.error("ERROR ## Unknow database type"); } return dbcpDs; }
From source file:nl.surfnet.coin.db.AbstractInMemoryDatabaseTest.java
/** * We use an in-memory database - no need for Spring in this one - and * populate it with the sql statements in test-data-eb.sql * /*from ww w . ja v a 2 s. c o m*/ * @throws Exception * unexpected */ @Before public void before() throws Exception { BasicDataSource dataSource = new BasicDataSource(); dataSource.setPassword(""); dataSource.setUsername("sa"); String url = getDataSourceUrl(); dataSource.setUrl(url); dataSource.setDriverClassName("org.hsqldb.jdbcDriver"); jdbcTemplate = new JdbcTemplate(dataSource); ClassPathResource resource = new ClassPathResource(getMockDataContentFilename()); logger.debug("Loading database content from " + resource); if (resource.exists()) { final String sql = IOUtils.toString(resource.getInputStream()); final String[] split = sql.split(";"); for (String s : split) { if (!StringUtils.hasText(s)) { continue; } jdbcTemplate.execute(s + ';'); } } }
From source file:nl.surfnet.coin.db.MigrationsTest.java
private BasicDataSource hsqldbDataSource() { BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName("org.hsqldb.jdbcDriver"); ds.setUrl("jdbc:hsqldb:mem:api"); ds.setUsername("sa"); ds.setPassword(""); return ds;// w w w. j a v a2 s . com }
From source file:nl.surfnet.coin.db.MigrationsTest.java
private BasicDataSource mysqlDataSource() { BasicDataSource ds = new BasicDataSource(); ds.setDriverClassName("com.mysql.jdbc.Driver"); ds.setUrl("jdbc:mysql://localhost:3306/apitest"); ds.setUsername("root"); ds.setPassword(""); return ds;//from w ww .j a v a 2 s. c o m }
From source file:nl.trivento.albero.repositories.DatabaseRepository.java
private DataSource createBasicDataSource(Map<String, String> parameters) throws ConfigurationException { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(parameters.get(DRIVER)); dataSource.setUrl(parameters.get(URL)); dataSource.setUsername(parameters.get(USER)); dataSource.setPassword(parameters.get(PASSWORD)); return dataSource; }