List of usage examples for javax.sql DataSource getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:com.jolbox.bonecp.spring.DynamicDataSourceProxy.java
/** Switch to a new DataSource using the given configuration. * @param newConfig BoneCP DataSource to use. * @throws SQLException/*from w ww.j av a2 s . com*/ */ public void switchDataSource(BoneCPConfig newConfig) throws SQLException { logger.info("Switch to new datasource requested. New Config: " + newConfig); DataSource oldDS = getTargetDataSource(); if (!(oldDS instanceof BoneCPDataSource)) { throw new SQLException("Unknown datasource type! Was expecting BoneCPDataSource but received " + oldDS.getClass() + ". Not switching datasource!"); } BoneCPDataSource newDS = new BoneCPDataSource(newConfig); newDS.getConnection().close(); // initialize a connection (+ throw it away) to force the datasource to initialize the pool // force application to start using the new one setTargetDataSource(newDS); logger.info("Shutting down old datasource slowly. Old Config: " + oldDS); // tell the old datasource to terminate. This terminates the pool lazily so existing checked out connections can still be used. ((BoneCPDataSource) oldDS).close(); }
From source file:com.example.dbflute.spring.dbflute.allcommon.DBFluteInitializer.java
/** * Constructor, which initializes various components. * @param dataSource The instance of data source. (NotNull) *///from w w w.j av a 2 s. c o m public DBFluteInitializer(javax.sql.DataSource dataSource) { if (dataSource == null) { String msg = "The argument 'dataSource' should not be null!"; throw new IllegalArgumentException(msg); } _dataSourceFqcn = dataSource.getClass().getName(); announce(); prologue(); standBy(); }
From source file:org.hsweb.web.datasource.dynamic.DynamicXaDataSourceImpl.java
public XADataSource getActiveXADataSource() { DataSource activeDs = getActiveDataSource(); XADataSource xaDataSource;/* w ww .j a v a2s . co m*/ if (activeDs instanceof XADataSource) xaDataSource = ((XADataSource) activeDs); else if (activeDs instanceof AtomikosDataSourceBean) { xaDataSource = ((AtomikosDataSourceBean) activeDs).getXaDataSource(); } else { throw new UnsupportedOperationException(activeDs.getClass() + " is not XADataSource"); } return xaDataSource; }
From source file:cz.jirutka.spring.data.jdbc.sql.SqlGeneratorFactory.java
/** * @param dataSource The DataSource for which to find compatible * SQL Generator./*from w ww. ja va 2 s . c o m*/ * @return An SQL Generator compatible with the given {@code dataSource}. * @throws DataAccessResourceFailureException if exception is thrown when * trying to obtain Connection or MetaData from the * {@code dataSource}. * @throws IllegalStateException if no compatible SQL Generator is found. */ public SqlGenerator getGenerator(DataSource dataSource) { if (cache.containsKey(dataSource)) { return cache.get(dataSource); } DatabaseMetaData metaData; try { metaData = dataSource.getConnection().getMetaData(); } catch (SQLException ex) { throw new DataAccessResourceFailureException("Failed to retrieve database metadata", ex); } for (SqlGenerator generator : generators) { try { if (generator.isCompatible(metaData)) { LOG.info("Using SQL Generator {} for dataSource {}", generator.getClass().getName(), dataSource.getClass()); cache.put(dataSource, generator); return generator; } } catch (SQLException ex) { LOG.warn("Exception occurred when invoking isCompatible() on {}", generator.getClass().getSimpleName(), ex); } } // This should not happen, because registry should always contain one // "default" generator that returns true for every DatabaseMetaData. throw new IllegalStateException("No compatible SQL Generator found."); }
From source file:de.iteratec.iteraplan.persistence.RoutingDataSource.java
/** * Adds a data source of type {@link CachableBasicDataSource} to the map of * {@link #setTargetDataSources(Map) target data sources}. * /*w ww .j a v a2s . c o m*/ * @param key * The lookup key the data source is mapped to. * @param value * The data source. */ public void addCachableBasicDataSource(Object key, DataSource value) { if (!(value instanceof CachableBasicDataSource)) { throw new IllegalArgumentException("The parameter 'value' must be of type 'CachableDataSource'."); } int maximumCacheSize = IteraplanProperties .getIntProperty(IteraplanProperties.MAXIMUM_SIMULTANEOUS_DATASOURCES); if (maximumCacheSize <= 0) { throw new IllegalArgumentException("The cache size property must have a value greater than zero."); } // This code block needs to be synchronized because only ONE thread at a time must be allowed // to change the shared map of target data sources. Otherwise race conditions could occur. // All other accesses to targetDataSources can remain without explicit synchronization, because they // operate on ConcurrentHashMap. synchronized (this) { if (targetDataSources.size() >= maximumCacheSize) { logger.info("Cache exceeded: Trying to remove least recently accessed data source."); long timestampToRemove = System.currentTimeMillis(); Object lookupKeyToRemove = null; DataSource dataSourceToRemove = null; for (Map.Entry<Object, DataSource> entry : this.targetDataSources.entrySet()) { Object lookupKey = resolveSpecifiedLookupKey(entry.getKey()); DataSource dataSource = resolveSpecifiedDataSource(entry.getValue()); // The MASTER data source must not be removed from the map. if (lookupKey.equals(Constants.MASTER_DATA_SOURCE)) { continue; } if (!(dataSource instanceof CachableBasicDataSource)) { throw new IllegalStateException( "Target data source is of type '" + dataSource.getClass().getSimpleName() + "', but must be of type 'CachableDataSource'"); } long timestamp = ((CachableBasicDataSource) dataSource).getTimestamp(); if (timestamp < timestampToRemove) { timestampToRemove = timestamp; lookupKeyToRemove = lookupKey; dataSourceToRemove = dataSource; logger.trace("The currently oldest timestamp is " + timestampToRemove + " for the data source with the lookup key " + lookupKeyToRemove); } } logger.info("Removing data source '" + lookupKeyToRemove + "' with timestamp " + timestampToRemove); if (lookupKeyToRemove != null) { targetDataSources.remove(lookupKeyToRemove); } if (dataSourceToRemove != null) { destroyDataSource(dataSourceToRemove); } } } logger.info("Adding key '" + key + "' to the map of data sources."); targetDataSources.put(key, value); updateDataSourceRouter(); }
From source file:com.alibaba.druid.benckmark.pool.PoolPerformanceTest.java
@Test public void test_tomcat_jdbc() throws Exception { org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource(); // dataSource.(10); dataSource.setMaxIdle(maxPoolSize);//from ww w. j a va 2 s .c o m dataSource.setMinIdle(minPoolSize); dataSource.setMaxActive(maxPoolSize); dataSource.setDriverClassName(driverClass); dataSource.setUrl(jdbcUrl); // dataSource.setPoolPreparedStatements(true); // dataSource.setMaxOpenPreparedStatements(100); dataSource.setUsername(user); dataSource.setPassword(password); System.out.println(dataSource.getClass().getSimpleName()); for (int i = 0; i < loopCount; ++i) { p0(dataSource, "tomcat-jdbc", threadCount); } dataSource.close(); System.out.println(); }
From source file:com.gopivotal.cloudfoundry.test.core.DataSourceUtils.java
public String getUrl(DataSource dataSource) { if (isClass(dataSource, "com.jolbox.bonecp.BoneCPDataSource")) { return invokeMethod(dataSource, "getJdbcUrl"); } else if (isClass(dataSource, "org.apache.commons.dbcp.BasicDataSource")) { return invokeMethod(dataSource, "getUrl"); } else if (isClass(dataSource, "org.apache.tomcat.dbcp.dbcp.BasicDataSource")) { return invokeMethod(dataSource, "getUrl"); } else if (isClass(dataSource, "org.apache.tomcat.jdbc.pool.DataSource")) { return invokeMethod(dataSource, "getUrl"); } else if (isClass(dataSource, "org.springframework.jdbc.datasource.embedded" + ".EmbeddedDatabaseFactory$EmbeddedDataSourceProxy")) { return getUrl(getDataSource(dataSource)); } else if (isClass(dataSource, "org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy")) { return getUrl(getTargetDataSource(dataSource)); } else if (isClass(dataSource, "org.springframework.jdbc.datasource.SimpleDriverDataSource")) { return invokeMethod(dataSource, "getUrl"); } else if (isClass(dataSource, "org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy")) { return getUrl(getTargetDataSource(dataSource)); }//from w w w. j a v a2 s .c o m return String.format("Unable to determine URL for DataSource of type %s", dataSource.getClass().getName()); }
From source file:com.nebhale.demo.web.DataSourceController.java
private String getUrl(DataSource dataSource) { if (isClass(dataSource, "com.jolbox.bonecp.BoneCPDataSource")) { return invokeMethod(dataSource, "getJdbcUrl"); } else if (isClass(dataSource, "org.apache.commons.dbcp.BasicDataSource")) { return invokeMethod(dataSource, "getUrl"); } else if (isClass(dataSource, "org.apache.tomcat.dbcp.dbcp.BasicDataSource")) { return invokeMethod(dataSource, "getUrl"); } else if (isClass(dataSource, "org.apache.tomcat.jdbc.pool.DataSource")) { return invokeMethod(dataSource, "getUrl"); } else if (isClass(dataSource, "org.springframework.jdbc.datasource.embedded" + ".EmbeddedDatabaseFactory$EmbeddedDataSourceProxy")) { return getUrl(getDataSource(dataSource)); } else if (isClass(dataSource, "org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy")) { return getUrl(getTargetDataSource(dataSource)); } else if (isClass(dataSource, "org.springframework.jdbc.datasource.SimpleDriverDataSource")) { return invokeMethod(dataSource, "getUrl"); } else if (isClass(dataSource, "org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy")) { return getUrl(getTargetDataSource(dataSource)); }/* w w w. j a v a2 s .co m*/ return String.format("Unable to determine URL for DataSource of type %s", dataSource.getClass().getName()); }
From source file:com.liferay.portal.dao.jdbc.DataSourceFactoryImpl.java
public DataSource initDataSource(Properties properties) throws Exception { Properties defaultProperties = PropsUtil.getProperties("jdbc.default.", true); PropertiesUtil.merge(defaultProperties, properties); properties = defaultProperties;//w w w . j av a2 s . c o m String jndiName = properties.getProperty("jndi.name"); if (Validator.isNotNull(jndiName)) { try { return (DataSource) JNDIUtil.lookup(new InitialContext(), jndiName); } catch (Exception e) { _log.error("Unable to lookup " + jndiName, e); } } DataSource dataSource = null; String liferayPoolProvider = PropsValues.JDBC_DEFAULT_LIFERAY_POOL_PROVIDER; if (liferayPoolProvider.equalsIgnoreCase("c3p0") || liferayPoolProvider.equalsIgnoreCase("c3po")) { if (_log.isDebugEnabled()) { _log.debug("Initializing C3P0 data source"); } dataSource = initDataSourceC3PO(properties); } else if (liferayPoolProvider.equalsIgnoreCase("dbcp")) { if (_log.isDebugEnabled()) { _log.debug("Initializing DBCP data source"); } dataSource = initDataSourceDBCP(properties); } else if (liferayPoolProvider.equalsIgnoreCase("primrose")) { if (_log.isDebugEnabled()) { _log.debug("Initializing Primrose data source"); } dataSource = initDataSourcePrimrose(properties); } else { if (_log.isDebugEnabled()) { _log.debug("Initializing Tomcat data source"); } dataSource = initDataSourceTomcat(properties); } if (_log.isDebugEnabled()) { _log.debug("Created data source " + dataSource.getClass().getName()); SortedProperties sortedProperties = new SortedProperties(properties); sortedProperties.list(System.out); } return dataSource; }
From source file:com.jolbox.benchmark.BenchmarkTests.java
/** * /*from ww w . j a v a2s . c o m*/ * * @param workdelay * @param doPreparedStatement * @param poolType * @return result * @throws PropertyVetoException * @throws InterruptedException * @throws SQLException * @throws NoSuchMethodException * @throws InvocationTargetException * @throws IllegalAccessException * @throws SecurityException * @throws IllegalArgumentException */ private long[] multiThreadTest(int workdelay, boolean doPreparedStatement, ConnectionPoolType poolType) throws PropertyVetoException, InterruptedException, SQLException, IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException { int numCycles = 10; long[] tempResults = new long[numCycles]; long[] poolResults = new long[threads]; DataSource ds = null; switch (poolType) { case BONECP_1_PARTITIONS: ds = multiThreadedBoneCP(doPreparedStatement, 1); break; case BONECP_2_PARTITIONS: ds = multiThreadedBoneCP(doPreparedStatement, 2); break; case BONECP_4_PARTITIONS: ds = multiThreadedBoneCP(doPreparedStatement, 4); break; case BONECP_5_PARTITIONS: ds = multiThreadedBoneCP(doPreparedStatement, 5); break; case BONECP_10_PARTITIONS: ds = multiThreadedBoneCP(doPreparedStatement, 10); break; case C3P0: ds = multiThreadedC3P0(doPreparedStatement); break; case PROXOOL: ds = multiThreadedProxool(doPreparedStatement); break; case DBCP: ds = multiThreadedDBCP(doPreparedStatement); break; case TOMCAT_JDBC: ds = multiThreadedTomcatJDBC(doPreparedStatement); break; case DBPOOL: ds = multiThreadedDBPool(doPreparedStatement); break; default: break; } for (int threadCount = 1; threadCount <= threads; threadCount = threadCount + stepping) { for (int cycle = 0; cycle < numCycles; cycle++) { if (ds == null) { continue; } tempResults[cycle] = (long) (startThreadTest(threadCount, ds, workdelay, doPreparedStatement) / (1.0 * threadCount)); } long min = Long.MAX_VALUE; for (int i = 0; i < numCycles; i++) { min = Math.min(min, tempResults[i]); } // String result = poolType+", "+threadCount + ", "+min; poolResults[threadCount] = min; // System.out.println(result); // results.add(result); } if (ds != null) { try { ds.getClass().getMethod("close").invoke(ds); } catch (NoSuchMethodException e) { ds.getClass().getMethod("release").invoke(ds); } } return poolResults; }