本文整理汇总了Java中com.mchange.v2.c3p0.DataSources.pooledDataSource方法的典型用法代码示例。如果您正苦于以下问题:Java DataSources.pooledDataSource方法的具体用法?Java DataSources.pooledDataSource怎么用?Java DataSources.pooledDataSource使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.mchange.v2.c3p0.DataSources
的用法示例。
在下文中一共展示了DataSources.pooledDataSource方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testOpenPooledDataSource
import com.mchange.v2.c3p0.DataSources; //导入方法依赖的package包/类
/**
* Test openPoolesDataSource(...) method.
*
* @throws Exception
*/
@Test
public void testOpenPooledDataSource() throws Exception {
PowerMockito.mockStatic(DataSources.class);
DataSource unPooledDSMock = mock(DataSource.class);
DataSource retPooledDSMock = mock(DataSource.class);
PowerMockito.doReturn(unPooledDSMock).when(DataSources.class, "unpooledDataSource"
, anyString(), anyString(), anyString());
PowerMockito.doReturn(retPooledDSMock).when(DataSources.class, "pooledDataSource"
, any(DataSource.class), anyMap());
assertEquals(retPooledDSMock, provider.openPooledDataSource(DBConnectionManager.DBType.MYSQL
, "url", USERNAME, "password"));
//Call PowerMockito.verifyStatic() to start verifying behavior
PowerMockito.verifyStatic();
//Use EasyMock-like semantic to verify behavior:
DataSources.unpooledDataSource(anyString(), anyString(), anyString());
DataSources.pooledDataSource(any(DataSource.class), anyMap());
}
示例2: createDataSource
import com.mchange.v2.c3p0.DataSources; //导入方法依赖的package包/类
public static DataSource createDataSource(Properties poolProperties) throws SQLException {
assertNotNull(poolProperties);
if (poolProperties.containsKey(PROP_URL) == false) {
throw new SQLException("Could not find field: " + PROP_URL);
}
DataSource ds = null;
String jdbcUrl = (String) poolProperties.get(PROP_URL);
try {
ds = DataSources.unpooledDataSource(jdbcUrl, poolProperties);
return DataSources.pooledDataSource(ds);
} catch (Exception e) {
throw new SQLException(e);
}
}
示例3: DatabaseHelper
import com.mchange.v2.c3p0.DataSources; //导入方法依赖的package包/类
/**
* @param rawProperties
* @throws SQLException
*/
public DatabaseHelper(final Properties rawProperties) throws SQLException {
final Properties database = new Properties(rawProperties);
try {
Class.forName(database.getProperty("driver"));
} catch (final ClassNotFoundException e) {
throw new SQLException("Database driver is not available", e);
}
final String url = database.getProperty("url");
final String username = database.getProperty("user");
final String password = database.getProperty("password");
final DataSource source = DataSources.unpooledDataSource(url, username, password);
final DataSource polled = DataSources.pooledDataSource(source);
polled.getConnection().close();
this.connectionProvider = polled;
}
示例4: getDataSource
import com.mchange.v2.c3p0.DataSources; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@Override
// 2.1.0
// protected DataSource getDataSource() {
public DataSource getDataSource() {
////
// 2.1.0
// // url
// String url = properties.getProperty("url");
// if (url == null)
// logger.error("C3p0.<init>: property url == null");
////
try {
// 2.1.0
// DataSource unpooledDataSource = DataSources.unpooledDataSource(url, properties);
// logger.debug(() -> "C3p0.getDataSource: unpooledDataSource = " + unpooledDataSource);
DataSource unpooledDataSource = DataSources.unpooledDataSource(jdbcProperties.getProperty("url"), jdbcProperties);
DataSource dataSource = DataSources.pooledDataSource(unpooledDataSource);
// 2.1.0
// logger.debug(() -> "C3p0.getDataSource: dataSource = " + dataSource);
////
return dataSource;
}
catch (SQLException e) {
// 2.1.0
// logger.error("C3p0.<init>: " + e, e);
throw new RuntimeException("jdbcProperties: " + jdbcProperties, e);
}
// 2.1.0
// return null;
////
}
示例5: prepareTestInstance
import com.mchange.v2.c3p0.DataSources; //导入方法依赖的package包/类
@Override
public void prepareTestInstance(final TestContext testContext) throws Exception {
System.err.println(String.format("TemporaryDatabaseExecutionListener.prepareTestInstance(%s)", testContext));
final JUnitTemporaryDatabase jtd = findAnnotation(testContext);
if (jtd == null) {
return;
}
m_database = m_databases.remove();
final PooledDataSource pooledDataSource = (PooledDataSource)DataSources.pooledDataSource(m_database);
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try { pooledDataSource.close(); }
catch (final Throwable t) { LogUtils.debugf(this, t, "failed to close pooled data source"); }
}
});
final LazyConnectionDataSourceProxy proxy = new LazyConnectionDataSourceProxy(pooledDataSource);
DataSourceFactory.setInstance(proxy);
testContext.setAttribute("org.opennms.netmgt.dao.db.TemporaryDatabaseExecutionListener.pooledDataSource", pooledDataSource);
System.err.println(String.format("TemporaryDatabaseExecutionListener.prepareTestInstance(%s) prepared db %s", testContext, m_database.toString()));
System.err.println("Temporary Database Name: " + m_database.getTestDatabase());
}
示例6: shouldUseConnectionFromPool
import com.mchange.v2.c3p0.DataSources; //导入方法依赖的package包/类
@Test
public void shouldUseConnectionFromPool() throws PropertyVetoException, SQLException, ClassNotFoundException {
Class.forName(driver());
DataSource dataSourceUnpooled = DataSources.unpooledDataSource(url(), user(), password());
DataSource dataSourcePooled = DataSources.pooledDataSource(dataSourceUnpooled); //init the connection pool
Base.open(dataSourcePooled); //get connection from pool
Person.deleteAll(); //clean DB before test
Person.createIt("name", "Matt", "last_name", "Diamont", "dob", "1962-01-01");
a(Person.findAll().size()).shouldBeEqual(1);
Person.deleteAll();//clean DB after test
Base.close();// really connection goes back to pool
DataSources.destroy(dataSourcePooled);//shut down the pool
}
示例7: configure
import com.mchange.v2.c3p0.DataSources; //导入方法依赖的package包/类
public void configure(Properties props) throws HibernateException {
String jdbcDriverClass = props.getProperty(Environment.DRIVER);
String jdbcUrl = props.getProperty(Environment.URL);
Properties connectionProps = ConnectionProviderFactory.getConnectionProperties(props);
log.info("C3P0 using driver: " + jdbcDriverClass + " at URL: " + jdbcUrl);
log.info("Connection properties: " + connectionProps);
if (jdbcDriverClass==null) {
log.warn("No JDBC Driver class was specified by property " + Environment.DRIVER);
}
else {
try {
Class.forName(jdbcDriverClass);
}
catch (ClassNotFoundException cnfe) {
String msg = "JDBC Driver class not found: " + jdbcDriverClass;
log.fatal(msg);
throw new HibernateException(msg);
}
}
try {
int minPoolSize = PropertiesHelper.getInt(Environment.C3P0_MIN_SIZE, props, 1);
int maxPoolSize = PropertiesHelper.getInt(Environment.C3P0_MAX_SIZE, props, 100);
int maxIdleTime = PropertiesHelper.getInt(Environment.C3P0_TIMEOUT, props, 0);
int maxStatements = PropertiesHelper.getInt(Environment.C3P0_MAX_STATEMENTS, props, 0);
int acquireIncrement = PropertiesHelper.getInt(Environment.C3P0_ACQUIRE_INCREMENT, props, 1);
int idleTestPeriod = PropertiesHelper.getInt(Environment.C3P0_IDLE_TEST_PERIOD, props, 0);
boolean validateConnection = PropertiesHelper.getBoolean(Environment.C3P0_VALIDATE_CONNECTION, props);
PoolConfig pcfg = new PoolConfig();
pcfg.setInitialPoolSize(minPoolSize);
pcfg.setMinPoolSize(minPoolSize);
pcfg.setMaxPoolSize(maxPoolSize);
pcfg.setAcquireIncrement(acquireIncrement);
pcfg.setMaxIdleTime(maxIdleTime);
pcfg.setMaxStatements(maxStatements);
pcfg.setTestConnectionOnCheckout(validateConnection);
pcfg.setIdleConnectionTestPeriod(idleTestPeriod);
/*DataSource unpooled = DataSources.unpooledDataSource(
jdbcUrl, props.getProperty(Environment.USER), props.getProperty(Environment.PASS)
);*/
DataSource unpooled = DataSources.unpooledDataSource(jdbcUrl, connectionProps);
ds = DataSources.pooledDataSource(unpooled, pcfg);
}
catch (Exception e) {
log.fatal("could not instantiate C3P0 connection pool", e);
throw new HibernateException("Could not instantiate C3P0 connection pool", e);
}
String i = props.getProperty(Environment.ISOLATION);
if (i==null) {
isolation=null;
}
else {
isolation = new Integer(i);
log.info( "JDBC isolation level: " + Environment.isolationLevelToString( isolation.intValue() ) );
}
}
示例8: openPooledDataSource
import com.mchange.v2.c3p0.DataSources; //导入方法依赖的package包/类
/**
* get the pooled datasource from c3p0 pool
*
* @param aDbType a supported database type.
* @param aDbUrl a connection url
* @param aUsername a username for the database
* @param aPassword a password for the database connection
* @return a DataSource a pooled data source
* @throws SQLException
*/
public DataSource openPooledDataSource(DBType aDbType, String aDbUrl, String aUsername, String aPassword) throws SQLException {
final DataSource unPooledDS = DataSources.unpooledDataSource(aDbUrl, aUsername, aPassword);
//override the default properties with ours
final Map<String, String> props = this.getPoolingProperties(aDbType);
return DataSources.pooledDataSource(unPooledDS, props);
}