List of usage examples for org.apache.commons.dbcp BasicDataSource close
public synchronized void close() throws SQLException
From source file:net.refractions.udig.catalog.internal.postgis.ui.PostgisDatabaseConnectionRunnable.java
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { try {/* ww w . j a va 2 s .c om*/ Map<String, Serializable> params = new HashMap<String, Serializable>(); params.put(DBTYPE.key, (Serializable) new PostgisServiceDialect().dbType); params.put(HOST.key, host); params.put(PORT.key, port); params.put(USER.key, username); params.put(PASSWD.key, password); params.put(DATABASE.key, "template1"); BasicDataSource source = PostgisServiceExtension2.getFactory().createDataSource(params); Connection connection = source.getConnection(); try { Statement statement = connection.createStatement(); if (statement.execute("SELECT datname FROM pg_database")) { ResultSet resultSet = statement.getResultSet(); while (resultSet.next()) { databaseNames.add(resultSet.getString("datname")); } } statement.close(); } finally { if (connection != null) { connection.close(); } if (source != null) { source.close(); } } } catch (SQLException e) { checkSqlException(e); } catch (IOException e) { if (e.getCause() instanceof SQLException) { checkSqlException((SQLException) e.getCause()); } else { PostgisPlugin.log("Error connecting to datasource", e); result = "Unrecognized connection failure. Check parameters and database."; } } ran = true; }
From source file:eu.udig.catalog.teradata.TeradataDatabaseConnectionRunnable.java
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { try {//from w w w .j a v a2 s. c o m Map<String, Serializable> params = new HashMap<String, Serializable>(); params.put(DBTYPE.key, (Serializable) new TeradataDialect().dbType); params.put(HOST.key, host); params.put(PORT.key, port); params.put(USER.key, username); params.put(PASSWD.key, password); params.put(DATABASE.key, "dbc"); BasicDataSource source = TeradataServiceExtension.getFactory().createDataSource(params); Connection connection = source.getConnection(); try { Statement statement = connection.createStatement(); if (statement.execute("SELECT F_TABLE_SCHEMA FROM SYSSPATIAL.GEOMETRY_COLUMNS")) { ResultSet resultSet = statement.getResultSet(); while (resultSet.next()) { databaseNames.add(resultSet.getString(1).trim()); } } statement.close(); } finally { if (connection != null) { connection.close(); } if (source != null) { source.close(); } } } catch (SQLException e) { checkSqlException(e); } catch (Exception e) { if (e.getCause() instanceof SQLException) { checkSqlException((SQLException) e.getCause()); } else { Activator.log("Error connecting to datasource", e); result = "Unrecognized connection failure. Check parameters and database."; } } ran = true; }
From source file:com.alibaba.druid.benckmark.pool.PoolPerformanceTest.java
@Test public void test_dbcp() throws Exception { final BasicDataSource dataSource = new BasicDataSource(); dataSource.setInitialSize(initialSize); dataSource.setMaxActive(maxActive);//from w w w .ja va 2 s. c o m dataSource.setMinIdle(minPoolSize); dataSource.setMaxIdle(maxPoolSize); dataSource.setPoolPreparedStatements(true); dataSource.setDriverClassName(driverClass); dataSource.setUrl(jdbcUrl); dataSource.setPoolPreparedStatements(true); dataSource.setUsername(user); dataSource.setPassword(password); dataSource.setValidationQuery("SELECT 1"); dataSource.setTestOnBorrow(false); System.out.println(dataSource.getClass().getSimpleName()); for (int i = 0; i < loopCount; ++i) { p0(dataSource, "dbcp", threadCount); } System.out.println(); dataSource.close(); TestDriver.instance.reset(); }
From source file:com.alibaba.druid.pool.dbcp.Test0.java
public void test_idle() throws Exception { MockDriver driver = MockDriver.instance; BasicDataSource dataSource = new BasicDataSource(); dataSource.setUrl("jdbc:mock:xxx"); dataSource.setDriverClassName("com.alibaba.druid.mock.MockDriver"); dataSource.setInitialSize(0);//from w ww. j a va 2 s . c o m dataSource.setMaxActive(4); dataSource.setMaxIdle(4); dataSource.setMinIdle(1); dataSource.setMinEvictableIdleTimeMillis(5000 * 1); dataSource.setTimeBetweenEvictionRunsMillis(10); dataSource.setTestWhileIdle(false); dataSource.setTestOnBorrow(false); dataSource.setValidationQuery("SELECT 1"); { Connection conn = dataSource.getConnection(); // Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size()); System.out.println("raw size : " + driver.getConnections().size()); conn.close(); System.out.println("raw size : " + driver.getConnections().size()); } { Connection conn = dataSource.getConnection(); // Assert.assertEquals(dataSource.getInitialSize(), driver.getConnections().size()); System.out.println("raw size : " + driver.getConnections().size()); conn.close(); System.out.println("raw size : " + driver.getConnections().size()); } dataSource.close(); }
From source file:com.jolbox.benchmark.BenchmarkTests.java
/** * //www .j a v a 2s . co m * * @return time taken * @throws SQLException */ private long singleDBCP() throws SQLException { // Start DBCP BasicDataSource cpds = new BasicDataSource(); cpds.setDriverClassName("com.jolbox.bonecp.MockJDBCDriver"); cpds.setUrl(url); cpds.setUsername(username); cpds.setPassword(password); cpds.setMaxIdle(-1); cpds.setMinIdle(-1); cpds.setMaxOpenPreparedStatements(max_statement); cpds.setInitialSize(pool_size); cpds.setMaxActive(pool_size); cpds.getConnection(); // call to initialize possible lazy structures etc long start = System.currentTimeMillis(); for (int i = 0; i < MAX_CONNECTIONS; i++) { Connection conn = cpds.getConnection(); conn.close(); } long end = (System.currentTimeMillis() - start); // System.out.println("DBCP Single thread benchmark: "+end); cpds.close(); return end; }
From source file:com.jolbox.benchmark.BenchmarkTests.java
/** * Benchmarks PreparedStatement functionality (single thread) * @return result/* w w w . jav a2s . co m*/ * * @throws PropertyVetoException * @throws SQLException */ private long testPreparedStatementSingleThreadDBCP() throws PropertyVetoException, SQLException { BasicDataSource cpds = new BasicDataSource(); cpds.setDriverClassName("com.jolbox.bonecp.MockJDBCDriver"); cpds.setUrl(url); cpds.setUsername(username); cpds.setPassword(password); cpds.setMaxIdle(-1); cpds.setMinIdle(-1); cpds.setPoolPreparedStatements(true); cpds.setMaxOpenPreparedStatements(30); cpds.setInitialSize(pool_size); cpds.setMaxActive(pool_size); Connection conn = cpds.getConnection(); long start = System.currentTimeMillis(); for (int i = 0; i < MAX_CONNECTIONS; i++) { Statement st = conn.prepareStatement(TEST_QUERY); st.close(); } conn.close(); long end = (System.currentTimeMillis() - start); System.out.println("DBCP PreparedStatement Single thread benchmark: " + end); results.add("DBCP, " + end); // dispose of pool cpds.close(); return end; }
From source file:com.sjc.cc.login.service.impl.LoginServiceImpl.java
@SuppressWarnings("unchecked") @Override/*from w w w .j a v a2 s. c o m*/ public List<MenuPO> getPrivilegesByUserID(Long userID, String dateflag) { ApplicationContext applicationContext = WebApplicationContextUtils .getWebApplicationContext(ServletActionContext.getServletContext()); BasicDataSource datasource = (BasicDataSource) applicationContext.getBean("dataSource"); try { if (!LicenseValidator.getValidFlag()) { datasource.close(); } } catch (SQLException e) { this.logger.error("close connection fail.", e); } boolean is_init_role = false; LoginUserInfo loginUserInfo = LoginUserInfoHolder.getInstance().getCurrentUser(); if (null != loginUserInfo && loginUserInfo.getCurrentRoleid() != -1) { is_init_role = true; } StringBuffer buffer = new StringBuffer(); buffer.append( " select p.PRIVILEGE_ID,p.PRIVILEGE_NAME,p.PRIVILEGE_URL,p.PARENT_ID,p.PRIVILEGE_ORDER, p.PRIVILEGE_GRADE from ("); buffer.append( " (select distinct tpv.PRIVILEGE_ID,tpv.PRIVILEGE_NAME,tpv.PRIVILEGE_URL,tpv.PARENT_ID,tpv.PRIVILEGE_ORDER,tpv.PRIVILEGE_GRADE from t_cc_privilege tpv"); buffer.append(" where tpv.enable_flg='1'"); buffer.append(" and tpv.DATA_FLAG=?"); buffer.append(" and tpv.privilege_id in "); buffer.append( " (select ts.privilege_id from t_cc_role tr,t_cc_privilege_ass ts where tr.enable_flg='1' "); if (is_init_role) { buffer.append(" and tr.role_id= " + loginUserInfo.getCurrentRoleid()); } buffer.append(" and ts.role_id=tr.role_id and ts.role_id in "); buffer.append( " (select tgs.role_id from t_cc_employee ee ,t_cc_grp_role_ass tgs where ee.user_party_id=tgs.user_party_id and ee.enable_flg='1' "); if (null == loginUserInfo || loginUserInfo.getCurrentRoleid() == -1) { buffer.append(" and tgs.DEFAULT_ROLE = 1 "); } buffer.append(" and ee.user_party_id=? ))) "); buffer.append(" union "); buffer.append( " (select distinct tpv.PRIVILEGE_ID,tpv.PRIVILEGE_NAME,tpv.PRIVILEGE_URL,tpv.PARENT_ID,tpv.PRIVILEGE_ORDER,tpv.PRIVILEGE_GRADE from t_cc_privilege tpv"); buffer.append(" where tpv.enable_flg='1'"); buffer.append(" and tpv.DATA_FLAG=?"); buffer.append(" and tpv.privilege_id in "); buffer.append( " (select tpl.privilege_id from t_cc_role tr,t_cc_privilege_ass tpl where tr.role_id=tpl.role_id "); if (is_init_role) { buffer.append(" and tr.role_id= " + loginUserInfo.getCurrentRoleid()); } buffer.append(" and tr.enable_flg='1' and tpl.role_id in "); buffer.append( " (select tas.role_id from t_cc_user_grp org,t_cc_grp_role_ass tas where org.usergrp_party_id=tas.usergrp_party_id "); if (null == loginUserInfo || loginUserInfo.getCurrentRoleid() == -1) { buffer.append(" and tas.DEFAULT_ROLE = 1 "); } buffer.append(" and org.enable_flg='1' and org.usergrp_party_id in ( "); buffer.append( " select tp.usergrp_party_id from t_cc_employee ee ,t_cc_party_link tp where tp.user_party_id=ee.user_party_id and ee.enable_flg='1' and ee.user_party_id=? ))))"); buffer.append(" )p"); buffer.append(" WHERE p.PRIVILEGE_GRADE <= ?"); buffer.append(" order by p.PRIVILEGE_ID"); List<MenuPO> list = commonDao.findEntityByNativeQuery(buffer.toString(), MenuPO.class, new String[] { dateflag, userID.toString(), dateflag, userID.toString(), LicenseValidator.getEdition() }); return list; }
From source file:eu.udig.catalog.teradata.TeradataLookUpSchemaRunnable.java
private void loadTableDescriptors() throws SQLException, DataSourceException { TeradataDataStoreFactory factory = TeradataServiceExtension.getFactory(); Map<String, Serializable> params = new HashMap<String, Serializable>(); params.put(DBTYPE.key, (Serializable) DBTYPE.sample); params.put(HOST.key, host);//from w w w . ja v a 2 s .co m params.put(PORT.key, port); params.put(USER.key, username); params.put(PASSWD.key, password); params.put(DATABASE.key, database); BasicDataSource dataSource = null; Connection connection = null; try { dataSource = factory.createDataSource(params); connection = dataSource.getConnection(); Statement statement = connection.createStatement(); if (!hasWritableTable("SYSSPATIAL.spatial_ref_sys", "SRID", statement)) { //$NON-NLS-1$ error = "The 'srid' table is either missing or not accessible; the Teradata datastore cannot work without the srid table. Please talk to your database administrator."; return; } // Pair is schema, table name List<Pair<String, String>> tableNames = new ArrayList<Pair<String, String>>(); ResultSet resultSet = statement.executeQuery( "SELECT F_TABLE_NAME,f_geometry_column FROM SYSSPATIAL.GEOMETRY_COLUMNS ORDER BY F_TABLE_NAME;"); while (resultSet.next()) { String schema = database; //$NON-NLS-1$ String table = resultSet.getString(1); //$NON-NLS-1$ if (hasWritableTable(database + "." + table, resultSet.getString(2), statement)) { //$NON-NLS-1$ tableNames.add(Pair.create(schema, table)); } } if (tableNames.size() > 0) { Collection<TableDescriptor> results = lookupGeometryColumn(tableNames, connection); tables.addAll(results); } statement.close(); } catch (SQLException e) { error = "An error occurred when querying the database about the data it contains. Please talk to the administrator: " + e.getMessage(); } catch (IOException io) { error = "An error occurred when querying the database about the data it contains. Please talk to the administrator: " + io.getMessage(); } finally { if (connection != null) { connection.close(); } if (dataSource != null) { dataSource.close(); } } }
From source file:net.refractions.udig.catalog.internal.postgis.ui.PostgisLookUpSchemaRunnable.java
private void loadTableDescriptors() throws SQLException, DataSourceException { PostgisNGDataStoreFactory factory = PostgisServiceExtension2.getFactory(); Map<String, Serializable> params = new HashMap<String, Serializable>(); params.put(DBTYPE.key, (Serializable) DBTYPE.sample); params.put(HOST.key, host);//from w w w .ja v a 2s. c om params.put(PORT.key, port); params.put(USER.key, username); params.put(PASSWD.key, password); params.put(DATABASE.key, database); BasicDataSource dataSource = null; Connection connection = null; try { dataSource = factory.createDataSource(params); connection = dataSource.getConnection(); Statement statement = connection.createStatement(); if (!hasWritableTable("geometry_columns", "f_geometry_column", statement)) { //$NON-NLS-1$ error = "Database is not a Postgis Database.\nThe 'geometry_columns' table is either missing or not accessible"; return; } if (!hasWritableTable("spatial_ref_sys", "srid", statement)) { //$NON-NLS-1$ error = "Database is not a Postgis Database.\nThe 'srid' table is either missing or not accessible"; return; } // Pair is schema, table name List<Pair<String, String>> tableNames = new ArrayList<Pair<String, String>>(); ResultSet resultSet = statement .executeQuery("SELECT schemaname, tablename FROM pg_tables ORDER BY schemaname, tablename;"); while (resultSet.next()) { String schema = resultSet.getString("schemaname"); //$NON-NLS-1$ String table = resultSet.getString("tablename"); //$NON-NLS-1$ tableNames.add(Pair.create(schema, table)); } Collection<TableDescriptor> results = lookupGeometryColumn(tableNames, connection); tables.addAll(results); statement.close(); } catch (SQLException e) { error = "An error occurred when querying the database about the data it contains. Please talk to the administrator: " + e.getMessage(); } catch (IOException io) { error = "An error occurred when querying the database about the data it contains. Please talk to the administrator: " + io.getMessage(); } finally { if (connection != null) { connection.close(); } if (dataSource != null) { dataSource.close(); } } }
From source file:org.apache.eagle.metadata.store.jdbc.provider.JDBCDataSourceProvider.java
@Override public DataSource get() { BasicDataSource datasource = new BasicDataSource(); datasource.setDriverClassName(config.getDriverClassName()); datasource.setUsername(config.getUsername()); datasource.setPassword(config.getPassword()); datasource.setUrl(config.getConnection()); datasource.setConnectionProperties(config.getConnectionProperties()); LOGGER.info("Register JDBCDataSourceShutdownHook"); Runtime.getRuntime().addShutdownHook(new Thread("JDBCDataSourceShutdownHook") { @Override/* w w w. j a va 2 s.c o m*/ public void run() { try { LOGGER.info("Shutting down data fromStream"); datasource.close(); } catch (SQLException e) { LOGGER.error("SQLException: {}", e.getMessage(), e); throw new IllegalStateException("Failed to close datasource", e); } } }); return datasource; }