本文整理汇总了Java中org.apache.commons.dbcp.ConnectionFactory类的典型用法代码示例。如果您正苦于以下问题:Java ConnectionFactory类的具体用法?Java ConnectionFactory怎么用?Java ConnectionFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ConnectionFactory类属于org.apache.commons.dbcp包,在下文中一共展示了ConnectionFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: ConnectionPooling
import org.apache.commons.dbcp.ConnectionFactory; //导入依赖的package包/类
/**
* Constructor
*
* Params:
*
*
*/
public ConnectionPooling(String connectionURL, String userName, String password, String driverName) throws ClassNotFoundException, SQLException{
Class.forName(driverName);
Properties props = new Properties();
props.setProperty("user", userName);
props.setProperty("password", password);
ObjectPool connectionPool = new GenericObjectPool(null);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(connectionURL, props);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
connectionPool, null, null, false, true);
Class.forName("org.apache.commons.dbcp.PoolingDriver");
PoolingDriver driver = (PoolingDriver) DriverManager.getDriver(myPoolingDriverName);
driver.registerPool(myPoolName,connectionPool);
}
示例2: buildDataSource
import org.apache.commons.dbcp.ConnectionFactory; //导入依赖的package包/类
public synchronized DataSource buildDataSource(DataSourceConfiguration cfg) {
init();
Pair<GenericObjectPool, DataSource> poolInfo = pools.get(cfg.getId());
if (poolInfo != null) {
return poolInfo.getSecond();
}
GenericObjectPool connectionPool = applicationContext.getBean(GenericObjectPool.class);
DataSource simpleDataSource = buildSimpleDataSource(cfg);
ConnectionFactory connectionFactory = new DataSourceConnectionFactory(simpleDataSource);
// ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(cfg.getUrl().replace(
// NETXILIA_HOME_VAR, path), cfg.getUsername(), cfg.getPassword());
new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);
PoolingDataSource poolingDataSource = new PoolingDataSource(connectionPool);
log.info("Creating a new datasource " + poolingDataSource + " for config:" + cfg.getId());
pools.put(cfg.getId(), new Pair<GenericObjectPool, DataSource>(connectionPool, poolingDataSource));
return poolingDataSource;
}
示例3: buildDataSource
import org.apache.commons.dbcp.ConnectionFactory; //导入依赖的package包/类
public static DataSource buildDataSource(String user, String pass,
String driver, String url) {
DataSource ds;
try {
Class.forName(driver);
} catch (ClassNotFoundException ignore) {
}
GenericObjectPool connectionPool = new GenericObjectPool(null);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
url, user, pass);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
connectionFactory, connectionPool, null, null, false, true);
return new PoolingDataSource(connectionPool);
}
示例4: getDataSource
import org.apache.commons.dbcp.ConnectionFactory; //导入依赖的package包/类
private DataSource getDataSource(LinkDataSource lds){
try{
String driver, url, user, passwd;
String[] access = lds.getAccess().split(",");
driver = access[0];
url = access[1];
user = access[2];
passwd = access[3];
Class.forName(driver);
ObjectPool connectionPool = new GenericObjectPool(null);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(url, user, passwd);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,null,null,false,true);
PoolingDataSource data_source = new PoolingDataSource(connectionPool);
return data_source;
}
catch(Exception e){
}
return null;
}
示例5: setup
import org.apache.commons.dbcp.ConnectionFactory; //导入依赖的package包/类
public void setup( Properties prop ) {
this.prop=prop;
GenericObjectPool.Config conPoolCfg = new GenericObjectPool.Config();
conPoolCfg.maxActive = Integer.parseInt( prop.getProperty( "connectionPoolMaxSize", "15" ) );
conPoolCfg.maxIdle = Integer.parseInt( prop.getProperty( "connectionPoolMaxIdle", "8" ) );
conPoolCfg.maxWait = Integer.parseInt( prop.getProperty( "connectionPoolMaxWait", "60000" ) );
conPoolCfg.minIdle = Integer.parseInt( prop.getProperty( "connectionPoolMinSize", "2" ) );
ObjectPool connectionPool = new GenericObjectPool( null, conPoolCfg );
try {
Class.forName( prop.getProperty( "jdbcDriver" ) );
} catch( ClassNotFoundException e ) {
e.printStackTrace();
throw new RuntimeException();
}
ConnectionFactory connectionFactory = new
DriverManagerConnectionFactory( prop.getProperty( "jdbcUrl" ),
prop.getProperty( "jdbcUser" ),
prop.getProperty( "jdbcPassword" ) );
new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);
PoolingDataSource dataSource = new PoolingDataSource(connectionPool);
ds = dataSource;
}
示例6: StatementClosingPoolableConnectionFactory
import org.apache.commons.dbcp.ConnectionFactory; //导入依赖的package包/类
/**
* Default superclass constructor.
*
* @throws Exception When there is an error in the PoolableConnectionFactory constructor.
*/
public StatementClosingPoolableConnectionFactory(ConnectionFactory factory,
ObjectPool pool,
KeyedObjectPoolFactory stmtPoolFactory,
String validationQuery,
boolean defaultReadOnly,
boolean defaultAutoCommit)
throws Exception {
super(factory, pool, stmtPoolFactory, validationQuery,
defaultReadOnly, defaultAutoCommit);
}
示例7: ReportDatasource
import org.apache.commons.dbcp.ConnectionFactory; //导入依赖的package包/类
/**
* Construct with configuration.
*
* @param configuration - app configuration
*/
public ReportDatasource (final Configuration configuration) {
Optional<String> optConnectionURI = configuration.getJDBCConnectionString();
if (!optConnectionURI.isPresent()) {
dataSource = Optional.empty();
return;
}
String connectionURI = optConnectionURI.get();
ConnectionFactory cf = new DriverManagerConnectionFactory(connectionURI, null);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(cf, new GenericObjectPool(),
null, null, true, false);
GenericObjectPool connectionPool = new GenericObjectPool(poolableConnectionFactory);
dataSource = Optional.of(new PoolingDataSource(connectionPool));
}
示例8: initializeDataSource
import org.apache.commons.dbcp.ConnectionFactory; //导入依赖的package包/类
/**
* @param config
* @throws SQLException
*/
private void initializeDataSource(Config config) {
String user = config.getString(CONFIG_KEY_DB_USER);
String password = config.getString(CONFIG_KEY_DB_PASSWORD);
Properties params = new Properties();
if (isNotEmpty(user) && isNotEmpty(password)) {
params.put("user", user);
params.put("password", password);
}
// ドライバのロード
String driver = config.getString(CONFIG_KEY_DB_DRIVER);
boolean loadSuccess = DbUtils.loadDriver(driver);
if (!loadSuccess) {
String message = "failed to load driver.";
throw new RuntimeException(message);
}
// コネクションをプールするDataSource を作成する
@SuppressWarnings("rawtypes")
GenericObjectPool pool = new GenericObjectPool();
// コネクションプールの設定を行う
int maxActive = config.getInt(CONFIG_KEY_DB_MAX_ACTIVE_CONN, 100);
long maxWait = Long.parseLong(config.getString(CONFIG_KEY_DB_WAIT, "-1"));
pool.setMaxActive(maxActive);
pool.setMaxIdle(maxActive);
pool.setMaxWait(maxWait);
driverUrl = config.getString(CONFIG_KEY_DB_URL);
ConnectionFactory connFactory = new DriverManagerConnectionFactory(driverUrl, params);
new PoolableConnectionFactory(connFactory, pool, null,
null, // validationQuery
false, // defaultReadOnly
false); // defaultAutoCommit
dataSource = new PoolingDataSource(pool);
}
示例9: setUp
import org.apache.commons.dbcp.ConnectionFactory; //导入依赖的package包/类
/**
*
* @return @throws Exception
*/
public DataSource setUp() throws Exception {
/**
* Load JDBC Driver class.
*/
Class.forName(ConnectionPool.DRIVER).newInstance();
/**
* Creates an instance of GenericObjectPool that holds our pool of
* connections object.
*/
connectionPool = new GenericObjectPool();
// set the max number of connections
connectionPool.setMaxActive(connections);
// if the pool is exhausted (i.e., the maximum number of active objects has been reached), the borrowObject() method should simply create a new object anyway
connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW);
/**
* Creates a connection factory object which will be used by the pool to
* create the connection object. We pass the JDBC url info, username
* and password.
*/
ConnectionFactory cf = new DriverManagerConnectionFactory(
ConnectionPool.URL,
ConnectionPool.USERNAME,
ConnectionPool.PASSWORD);
/**
* Creates a PoolableConnectionFactory that will wrap the connection
* object created by the ConnectionFactory to add object pooling
* functionality.
*/
PoolableConnectionFactory pcf
= new PoolableConnectionFactory(cf, connectionPool,
null, null, false, true);
return new PoolingDataSource(connectionPool);
}
示例10: initConnection
import org.apache.commons.dbcp.ConnectionFactory; //导入依赖的package包/类
public final static void initConnection(String address, String user, String password) throws ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
GenericObjectPool connectionPool = new GenericObjectPool(null);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(address, user, password);
KeyedObjectPoolFactory keyed_factory = new StackKeyedObjectPoolFactory();
new PoolableConnectionFactory(connectionFactory, connectionPool, keyed_factory, null, false, true);
ds = new PoolingDataSource(connectionPool);
}
示例11: setUp
import org.apache.commons.dbcp.ConnectionFactory; //导入依赖的package包/类
public DataSource setUp() throws Exception {
Class.forName(DBPool.DRIVER).newInstance(); //Load driver
//Create pool
connectionPool = new GenericObjectPool();
connectionPool.setMaxActive(DBPool.POOL_SIZE);
//Create factor
ConnectionFactory cf = new DriverManagerConnectionFactory(DBPool.URL,DBPool.USER,DBPool.PASSWORD);
//Create PoolableConnectionFactory
PoolableConnectionFactory pcf =
new PoolableConnectionFactory(cf, connectionPool,
null, null, false, true);
return new PoolingDataSource(connectionPool);
}
示例12: getDefaultDataSource
import org.apache.commons.dbcp.ConnectionFactory; //导入依赖的package包/类
@SuppressWarnings({"unchecked", "rawtypes"})
private static DataSource getDefaultDataSource(final String database) {
final GenericObjectPool connectionPool = new GenericObjectPool(null, 5);
final ConnectionFactory connectionFactory = new DriverManagerConnectionFactory("jdbc:mysql://localhost:3306/" + database, "cloud", "cloud");
final PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, connectionPool, null, null, false, true);
return new PoolingDataSource(
/* connectionPool */poolableConnectionFactory.getPool());
}
示例13: DataSourceWorkflowRepositoryFactory
import org.apache.commons.dbcp.ConnectionFactory; //导入依赖的package包/类
/**
* <p>
* Default Constructor
* </p>.
*/
public DataSourceWorkflowRepositoryFactory() throws WorkflowException {
String jdbcUrl, user, pass, driver;
jdbcUrl = System
.getProperty("org.apache.oodt.cas.workflow.repo.datasource.jdbc.url");
user = System
.getProperty("org.apache.oodt.cas.workflow.repo.datasource.jdbc.user");
pass = System
.getProperty("org.apache.oodt.cas.workflow.repo.datasource.jdbc.pass");
driver = System
.getProperty("org.apache.oodt.cas.workflow.repo.datasource.jdbc.driver");
try {
Class.forName(driver);
} catch (ClassNotFoundException e) {
throw new WorkflowException("Cannot load driver: " + driver);
}
GenericObjectPool connectionPool = new GenericObjectPool(null);
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
jdbcUrl, user, pass);
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(
connectionFactory, connectionPool, null, null, false, true);
dataSource = new PoolingDataSource(connectionPool);
}
示例14: LocalXAConnectionFactory
import org.apache.commons.dbcp.ConnectionFactory; //导入依赖的package包/类
/**
* Creates an LocalXAConnectionFactory which uses the specified connection factory to create database
* connections. The connections are enlisted into transactions using the specified transaction manager.
*
* @param transactionManager the transaction manager in which connections will be enlisted
* @param connectionFactory the connection factory from which connections will be retrieved
*/
public LocalXAConnectionFactory(TransactionManager transactionManager, ConnectionFactory connectionFactory) {
if (transactionManager == null) throw new NullPointerException("transactionManager is null");
if (connectionFactory == null) throw new NullPointerException("connectionFactory is null");
this.transactionRegistry = new TransactionRegistry(transactionManager);
this.connectionFactory = connectionFactory;
}
示例15: setUp
import org.apache.commons.dbcp.ConnectionFactory; //导入依赖的package包/类
public void setUp() throws Exception {
super.setUp();
// create a GeronimoTransactionManager for testing
transactionManager = new TransactionManagerImpl();
// create a driver connection factory
Properties properties = new Properties();
properties.setProperty("user", "username");
properties.setProperty("password", "password");
ConnectionFactory connectionFactory = new DriverConnectionFactory(new TesterDriver(), "jdbc:apache:commons:testdriver", properties);
// wrap it with a LocalXAConnectionFactory
XAConnectionFactory xaConnectionFactory = new LocalXAConnectionFactory(transactionManager, connectionFactory);
// create the pool
pool = new GenericObjectPool();
pool.setMaxActive(getMaxActive());
pool.setMaxWait(getMaxWait());
// create the pool object factory
PoolableConnectionFactory factory = new PoolableConnectionFactory(xaConnectionFactory, pool, null, "SELECT DUMMY FROM DUAL", true, true);
pool.setFactory(factory);
// finally create the datasource
ds = new ManagedDataSource(pool, xaConnectionFactory.getTransactionRegistry());
ds.setAccessToUnderlyingConnectionAllowed(true);
}