本文整理汇总了Java中com.mchange.v2.c3p0.PooledDataSource类的典型用法代码示例。如果您正苦于以下问题:Java PooledDataSource类的具体用法?Java PooledDataSource怎么用?Java PooledDataSource使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PooledDataSource类属于com.mchange.v2.c3p0包,在下文中一共展示了PooledDataSource类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: pooled
import com.mchange.v2.c3p0.PooledDataSource; //导入依赖的package包/类
private static PooledDataSource pooled(String hostname, int port, String username, String password, String database, String driver, int maxPoolsize) {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setJdbcUrl(format("jdbc:mysql://%s:%d/%s?rewriteBatchedStatements=true",
hostname,
port,
database));
dataSource.setUser(username);
dataSource.setPassword(password);
dataSource.setIdleConnectionTestPeriod(60 * 5);
dataSource.setMinPoolSize(3);
dataSource.setInitialPoolSize(3);
dataSource.setAcquireIncrement(1);
dataSource.setMaxPoolSize(maxPoolsize);
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
return dataSource;
}
示例2: getPooledDataSource
import com.mchange.v2.c3p0.PooledDataSource; //导入依赖的package包/类
private static PooledDataSource getPooledDataSource(Properties properties, String configPrefix, String host_propertyname, int maxPoolSize) {
String prefix = configPrefix;
if (properties.getProperty(prefix + host_propertyname) == null) {
prefix = "db." + prefix + ".";
if (properties.getProperty(prefix + host_propertyname) == null) {
throw new IllegalArgumentException("No " + configPrefix + host_propertyname + " property available to configure data source");
}
}
return pooled(
properties.getProperty(prefix + host_propertyname),
Integer.parseInt(properties.getProperty(prefix + "port")),
properties.getProperty(prefix + "username"),
properties.getProperty(prefix + "password"),
properties.getProperty(prefix + "database"),
properties.getProperty(prefix + "driver"),
maxPoolSize
);
}
示例3: DataSourceMonitor
import com.mchange.v2.c3p0.PooledDataSource; //导入依赖的package包/类
public DataSourceMonitor() {
this.dataSources = new LinkedList<>();
this.executor = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryBuilder().setNameFormat("DataSourceMonitor").build());
executor.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
for (PooledDataSource dataSource : dataSources) {
try {
reportDataSource(Kernel.getMetricsService(), dataSource);
} catch (Exception e) {
log.error(ExceptionToString.format(e));
}
}
}
}, 10, getMonitoringFrequency(), TimeUnit.SECONDS);
}
示例4: reportDataSource
import com.mchange.v2.c3p0.PooledDataSource; //导入依赖的package包/类
public void reportDataSource(MetricsService metricsService, PooledDataSource dataSource) {
log.trace(String.format("will report on data source %s", dataSource.getDataSourceName()));
String dataSourceName = dataSource.getDataSourceName();
try {
// connections
metricsService.recordGaugeValue(parameterName(dataSourceName, "connections.numTotal"), (long) dataSource.getNumConnectionsDefaultUser());
metricsService.recordGaugeValue(parameterName(dataSourceName, "connections.numBusy"), (long) dataSource.getNumBusyConnectionsDefaultUser());
metricsService.recordGaugeValue(parameterName(dataSourceName, "connections.numIdle"), (long) dataSource.getNumIdleConnectionsDefaultUser());
metricsService.recordGaugeValue(parameterName(dataSourceName, "connections.numUnclosedOrphaned"),
(long) dataSource.getNumUnclosedOrphanedConnectionsDefaultUser());
// statements
metricsService.recordGaugeValue(parameterName(dataSourceName, "statements.numTotal"), (long) dataSource.getStatementCacheNumStatementsDefaultUser());
metricsService.recordGaugeValue(parameterName(dataSourceName, "statements.numCheckedOut"), (long) dataSource.getStatementCacheNumCheckedOutDefaultUser());
metricsService.recordGaugeValue(parameterName(dataSourceName, "statements.numWithCached"),
(long) dataSource.getStatementCacheNumConnectionsWithCachedStatementsDefaultUser());
metricsService.recordGaugeValue(parameterName(dataSourceName, "numFailedCheckouts"), dataSource.getNumFailedCheckoutsDefaultUser());
metricsService.recordGaugeValue(parameterName(dataSourceName, "numFailedCheckins"), dataSource.getNumFailedCheckinsDefaultUser());
} catch (SQLException e) {
log.error(ExceptionToString.format(e));
}
}
示例5: getPooledConnection
import com.mchange.v2.c3p0.PooledDataSource; //导入依赖的package包/类
private Connection getPooledConnection(PooledDataSource ds, String aUsername, String aPassword) throws SQLException {
Connection retCon;
try {
retCon = ds.getConnection();
} catch (Exception e) {
if (ds.sampleLastAcquisitionFailureStackTrace(aUsername, aPassword) != null) {
throw new SQLException(ds.sampleLastAcquisitionFailureStackTrace(aUsername, aPassword));
} else if (ds.sampleLastCheckinFailureStackTrace(aUsername, aPassword) != null) {
throw new SQLException(ds.sampleLastCheckinFailureStackTrace(aUsername, aPassword));
} else if (ds.sampleLastCheckoutFailureStackTrace(aUsername, aPassword) != null) {
throw new SQLException(ds.sampleLastCheckoutFailureStackTrace(aUsername, aPassword));
} else if (ds.sampleLastConnectionTestFailureStackTrace(aUsername, aPassword) != null) {
throw new SQLException(ds.sampleLastConnectionTestFailureStackTrace(aUsername, aPassword));
} else if (ds.sampleLastIdleTestFailureStackTrace(aUsername, aPassword) != null) {
throw new SQLException(ds.sampleLastIdleTestFailureStackTrace(aUsername, aPassword));
} else {
throw new SQLException(e);
}
}
return retCon;
}
示例6: testCleanDataSources
import com.mchange.v2.c3p0.PooledDataSource; //导入依赖的package包/类
/**
* Test cleanDataSources().
*
* @throws java.sql.SQLException
*/
@Test
public void testCleanDataSources() throws SQLException {
DBConnectionManager dbcManagerSpy = getDBConnectionManagerSpyWithPooling();
PooledDataSource dataSourceMock = mock(PooledDataSource.class);
Hashtable<String, Hashtable<String, DataSource>> dbmsPoolTable = getHashTableObject1(dataSourceMock);
Hashtable<String, PooledDataSourceProvider> providerTable = new Hashtable<>();
PooledDataSourceProvider dataSourceProviderMock = mock(PooledDataSourceProvider.class);
doNothing().when(dataSourceProviderMock).closePooledDataSource(any(DataSource.class));
providerTable.put(C3P0PooledDataSourceProvider.C3P0_DATASOURCE_PROVIDER_NAME, dataSourceProviderMock);
dbcManagerSpy.providerTable = providerTable;
dbcManagerSpy.dbmsPoolTable = dbmsPoolTable;
dbcManagerSpy.cleanDataSources();
verify(dataSourceMock, times(1)).getNumConnectionsAllUsers();
verify(dataSourceProviderMock, times(1)).closePooledDataSource(any(DataSource.class));
assertTrue(dbcManagerSpy.dbmsPoolTable.isEmpty());
}
示例7: testShutDownDbmsPools
import com.mchange.v2.c3p0.PooledDataSource; //导入依赖的package包/类
/**
* Test shutdownDbmsPools().
*
* @throws java.sql.SQLException
*/
@Test
public void testShutDownDbmsPools() throws SQLException {
DBConnectionManager dbcManagerSpy = getDBConnectionManagerSpyWithPooling();
PooledDataSource dataSourceMock = mock(PooledDataSource.class);
dbcManagerSpy.dbmsPoolTable = getHashTableObject1(dataSourceMock);
Hashtable<String, PooledDataSourceProvider> providerTable = new Hashtable<>();
PooledDataSourceProvider dataSourceProviderMock = mock(PooledDataSourceProvider.class);
doNothing().when(dataSourceProviderMock).closePooledDataSource(any(DataSource.class));
providerTable.put(C3P0PooledDataSourceProvider.C3P0_DATASOURCE_PROVIDER_NAME, dataSourceProviderMock);
dbcManagerSpy.providerTable = providerTable;
Connection connMock = mock(Connection.class);
doReturn(connMock).when(dbcManagerSpy).getPooledConnection(any(DBType.class)
, any(String.class), any(String.class), any(String.class));
dbcManagerSpy.getConnection(DBType.DB2, EMPTY_STRING, DB_URL, DHARMA_USER, DHARMA_PASSWORD, getPoolingProperties());
dbcManagerSpy.shutdownDbmsPools();
verify(dataSourceProviderMock, times(1)).closePooledDataSource(any(DataSource.class));
assertNull(dbcManagerSpy.dbmsPoolTable);
}
示例8: testGetPooledConnectionWithNoDataSource
import com.mchange.v2.c3p0.PooledDataSource; //导入依赖的package包/类
/**
* Test method getPooledConnection(...) with no DataSource.
*
* @throws Exception
*/
@Test
public void testGetPooledConnectionWithNoDataSource() throws Exception {
DBConnectionManager dbcManagerSpy = getDBConnectionManagerSpyWithPooling();
PooledDataSource dataSourceMock = mock(PooledDataSource.class);
Connection connMock = mock(Connection.class);
doReturn(connMock).when(dataSourceMock).getConnection();
dbcManagerSpy.dbmsPoolTable = getHashTableObject2(dataSourceMock, connMock);
doReturn(dataSourceMock).when(dbcManagerSpy).createDataSource(any(DBType.class)
, anyString(), anyString(), anyString(), any(Hashtable.class));
PowerMockito.mockStatic(TripleDES.class);
Mockito.when(TripleDES.encryptPassword(any(String.class))).thenReturn(ENCRYPTED_PASS);
assertEquals(connMock, dbcManagerSpy.getPooledConnection(DBType.MYSQL, DB_URL, DHARMA_USER, DHARMA_PASSWORD));
verify(dataSourceMock, times(1)).getConnection();
verify(dbcManagerSpy.createDataSource(any(DBType.class)
, anyString(), anyString(), anyString(), any(Hashtable.class)), times(1));
}
示例9: closePolledDataSources
import com.mchange.v2.c3p0.PooledDataSource; //导入依赖的package包/类
/**
* Close all pooled datasources created by C3P0.
*/
public static void closePolledDataSources() {
for (PooledDataSource dataSource:(Set<PooledDataSource>)C3P0Registry.allPooledDataSources()) {
try {
dataSource.close();
DataSources.destroy(dataSource);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
示例10: createDataSource
import com.mchange.v2.c3p0.PooledDataSource; //导入依赖的package包/类
protected PooledDataSource createDataSource() {
logger.info("Creating the data source with the configuration {}", configuration);
if (configuration == null) {
logger.error("Database configuration is not set");
throw new MinnalException("Database configuration is not set");
}
ComboPooledDataSource dataSource = new ComboPooledDataSource();
try {
Class.forName(configuration.getDriverClass());
dataSource.setJdbcUrl(configuration.getUrl());
dataSource.setUser(configuration.getUsername());
dataSource.setPassword(configuration.getPassword());
dataSource.setIdleConnectionTestPeriod(configuration.getIdleConnectionTestPeriod());
dataSource.setInitialPoolSize(configuration.getMinSize());
dataSource.setMaxPoolSize(configuration.getMaxSize());
} catch (Exception e) {
logger.error("Failed while creating the data source", e);
throw new MinnalException("Failed while configuring the data source", e);
}
return dataSource;
}
示例11: pooledEventSource
import com.mchange.v2.c3p0.PooledDataSource; //导入依赖的package包/类
private static LegacyPooledMysqlEventSource pooledEventSource(PooledDataSource dataSource, String tableName, StreamId pretendStreamId, String name, int batchSize) {
try {
new LegacyMysqlEventStoreSetup(dataSource::getConnection, tableName).lazyCreate();
} catch (Exception e) {
LoggerFactory.getLogger(LegacyMysqlEventSource.class).warn("Failed to ensure ES scheme is created", e);
}
return new LegacyPooledMysqlEventSource(dataSource, tableName, pretendStreamId, batchSize, name);
}
示例12: pooledMasterDbEventSource
import com.mchange.v2.c3p0.PooledDataSource; //导入依赖的package包/类
public static PooledMysqlEventSource pooledMasterDbEventSource(PooledDataSource dataSource, String tableName, String name, int batchSize) {
try {
new BasicMysqlEventStoreSetup(dataSource::getConnection, tableName).lazyCreate();
} catch (Exception e) {
LoggerFactory.getLogger(BasicMysqlEventSource.class).warn("Failed to ensure ES scheme is created", e);
}
return new PooledMysqlEventSource(dataSource, tableName, batchSize, name);
}
示例13: pooledMasterDb
import com.mchange.v2.c3p0.PooledDataSource; //导入依赖的package包/类
public static PooledDataSource pooledMasterDb(Config config, int maxPoolSize) {
return pooled(
config.getString("hostname"),
config.getInt("port"),
config.getString("username"),
config.getString("password"),
config.getString("database"),
config.getString("driver"),
maxPoolSize
);
}
示例14: pooledReadOnlyDb
import com.mchange.v2.c3p0.PooledDataSource; //导入依赖的package包/类
public static PooledDataSource pooledReadOnlyDb(Config config, int maxPoolSize) {
return pooled(
config.getString("read_only_cluster"),
config.getInt("port"),
config.getString("username"),
config.getString("password"),
config.getString("database"),
config.getString("driver"),
maxPoolSize
);
}
示例15: proccessDataSource
import com.mchange.v2.c3p0.PooledDataSource; //导入依赖的package包/类
private PooledDataSource proccessDataSource(String tenantIdentifier) {
System.out.println("connection is null, start process " + tenantIdentifier);
DataSourceWrapper sourceWrapper = DataSourceRegistery.getInstance().get(tenantIdentifier);
if (sourceWrapper == null) {
System.out.println("datasource is null for tenantid in map " + tenantIdentifier);
sourceWrapper = restTemplate.getForObject("http://localhost:8888/database-management/tenant/{tenantId}", DataSourceWrapper.class, tenantIdentifier);
System.out.println("get datasurce in database management " + tenantIdentifier);
sourceWrapper.setTenantId(tenantIdentifier);
DataSourceRegistery.getInstance().put(sourceWrapper.getTenantId(), sourceWrapper);
System.out.println("put datasource in map " + tenantIdentifier);
}
System.out.println("create connection for tenantId " + tenantIdentifier);
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource(sourceWrapper.getTenantId());
comboPooledDataSource.setJdbcUrl(sourceWrapper.getUrl());
comboPooledDataSource.setUser(sourceWrapper.getUsername());
comboPooledDataSource.setPassword(sourceWrapper.getPassword());
comboPooledDataSource.setInitialPoolSize(sourceWrapper.getInitialPoolSize());
comboPooledDataSource.setMinPoolSize(sourceWrapper.getMinPoolSize());
comboPooledDataSource.setMaxPoolSize(sourceWrapper.getMaxPoolSize());
comboPooledDataSource.setMaxConnectionAge(sourceWrapper.getPoolMaxConnectionAge());
try {
comboPooledDataSource.setDriverClass(sourceWrapper.getDriver());
} catch (PropertyVetoException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("finish process for tenantId " + tenantIdentifier);
return comboPooledDataSource;
}