use of org.apache.commons.dbcp.BasicDataSource in project voldemort by voldemort.
the class MysqlGrowth method main.
public static void main(String[] args) throws Exception {
if (args.length != 3) {
System.err.println("USAGE: java MySQLGrowth total_size increment threads");
System.exit(1);
}
final int totalSize = Integer.parseInt(args[0]);
final int increment = Integer.parseInt(args[1]);
final int threads = Integer.parseInt(args[2]);
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUsername("root");
ds.setPassword("");
ds.setUrl("jdbc:mysql://127.0.0.1:3306/test");
final Connection conn = ds.getConnection();
conn.createStatement().execute("truncate table test_table");
final Random rand = new Random();
int iterations = totalSize / increment;
long[] readTimes = new long[iterations];
long[] writeTimes = new long[iterations];
ExecutorService service = Executors.newFixedThreadPool(threads);
for (int i = 0; i < iterations; i++) {
System.out.println("Starting iteration " + i);
List<Future<Object>> results = new ArrayList<Future<Object>>(increment);
long startTime = System.currentTimeMillis();
final int fi = i;
for (int j = 0; j < increment; j++) {
final int fj = j;
results.add(service.submit(new Callable<Object>() {
public Object call() throws Exception {
upsert(conn, Integer.toString(fi * increment + fj), Integer.toString(fi * increment + fj));
return null;
}
}));
}
for (int j = 0; j < increment; j++) results.get(j).get();
writeTimes[i] = System.currentTimeMillis() - startTime;
System.out.println("write: " + (writeTimes[i] / (double) increment));
results.clear();
startTime = System.currentTimeMillis();
for (int j = 0; j < increment; j++) {
results.add(service.submit(new Callable<Object>() {
public Object call() throws Exception {
return select(conn, Integer.toString(rand.nextInt((fi + 1) * increment)));
}
}));
}
for (int j = 0; j < increment; j++) results.get(j).get();
readTimes[i] = (System.currentTimeMillis() - startTime);
System.out.println("read: " + (readTimes[i] / (double) increment));
}
conn.close();
System.out.println();
System.out.println("iteration read write:");
for (int i = 0; i < iterations; i++) {
System.out.print(i);
System.out.print(" " + readTimes[i] / (double) increment);
System.out.println(" " + writeTimes[i] / (double) increment);
}
System.exit(0);
}
use of org.apache.commons.dbcp.BasicDataSource in project Asqatasun by Asqatasun.
the class PersistenceCommonConfig method setUpBasicDataSource.
/**
* @param url
* @param username
* @param password
* @return
*/
public DataSource setUpBasicDataSource(String url, String username, String password) {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName(PersistenceCommonConfig.getDriverClassNameFromUrl(url));
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setUrl(url);
return dataSource;
}
use of org.apache.commons.dbcp.BasicDataSource in project Asqatasun by Asqatasun.
the class AbstractWebDriverTestClass method initDb.
/**
* Initialises the Db connection
*/
private void initDb() {
dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUsername(dbUser);
dataSource.setPassword(dbPassword);
dataSource.setUrl("jdbc:mysql://" + dbUrl + "/" + dbName);
dataSource.setMaxActive(10);
dataSource.setMaxIdle(5);
dataSource.setInitialSize(5);
}
use of org.apache.commons.dbcp.BasicDataSource in project otter by alibaba.
the class DBDataSourceService method createDataSource.
private DataSource createDataSource(String url, String userName, String password, String driverClassName, DataMediaType dataMediaType, String encoding) {
BasicDataSource dbcpDs = new BasicDataSource();
// 初始化连接池时创建的连接数
dbcpDs.setInitialSize(initialSize);
// 连接池允许的最大并发连接数,值为非正数时表示不限制
dbcpDs.setMaxActive(maxActive);
// 连接池中的最大空闲连接数,超过时,多余的空闲连接将会被释放,值为负数时表示不限制
dbcpDs.setMaxIdle(maxIdle);
// 连接池中的最小空闲连接数,低于此数值时将会创建所欠缺的连接,值为0时表示不创建
dbcpDs.setMinIdle(minIdle);
// 以毫秒表示的当连接池中没有可用连接时等待可用连接返回的时间,超时则抛出异常,值为-1时表示无限等待
dbcpDs.setMaxWait(maxWait);
// 是否清除已经超过removeAbandonedTimeout设置的无效连接
dbcpDs.setRemoveAbandoned(true);
// 当清除无效链接时是否在日志中记录清除信息的标志
dbcpDs.setLogAbandoned(true);
// 以秒表示清除无效链接的时限
dbcpDs.setRemoveAbandonedTimeout(removeAbandonedTimeout);
// 确保连接池中没有已破损的连接
dbcpDs.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
// 指定连接被调用时是否经过校验
dbcpDs.setTestOnBorrow(false);
// 指定连接返回到池中时是否经过校验
dbcpDs.setTestOnReturn(false);
// 指定连接进入空闲状态时是否经过空闲对象驱逐进程的校验
dbcpDs.setTestWhileIdle(true);
// 以毫秒表示空闲对象驱逐进程由运行状态进入休眠状态的时长,值为非正数时表示不运行任何空闲对象驱逐进程
dbcpDs.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
// 以毫秒表示连接被空闲对象驱逐进程驱逐前在池中保持空闲状态的最小时间
dbcpDs.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
// 动态的参数
dbcpDs.setDriverClassName(driverClassName);
dbcpDs.setUrl(url);
dbcpDs.setUsername(userName);
dbcpDs.setPassword(password);
if (dataMediaType.isOracle()) {
dbcpDs.addConnectionProperty("restrictGetTables", "true");
dbcpDs.setValidationQuery("select 1 from dual");
} else if (dataMediaType.isMysql()) {
// open the batch mode for mysql since 5.1.8
dbcpDs.addConnectionProperty("useServerPrepStmts", "false");
dbcpDs.addConnectionProperty("rewriteBatchedStatements", "true");
// 将0000-00-00的时间类型返回null
dbcpDs.addConnectionProperty("zeroDateTimeBehavior", "convertToNull");
// 直接返回字符串,不做year转换date处理
dbcpDs.addConnectionProperty("yearIsDateType", "false");
// 返回时间类型的字符串,不做时区处理
dbcpDs.addConnectionProperty("noDatetimeStringSync", "true");
// 允许sqlMode为非严格模式
dbcpDs.addConnectionProperty("jdbcCompliantTruncation", "false");
if (StringUtils.isNotEmpty(encoding)) {
if (StringUtils.equalsIgnoreCase(encoding, "utf8mb4")) {
dbcpDs.addConnectionProperty("characterEncoding", "utf8");
dbcpDs.setConnectionInitSqls(Arrays.asList("set names utf8mb4"));
} else {
dbcpDs.addConnectionProperty("characterEncoding", encoding);
}
}
dbcpDs.setValidationQuery("select 1");
} else {
logger.error("ERROR ## Unknow database type");
}
return dbcpDs;
}
use of org.apache.commons.dbcp.BasicDataSource in project otter by alibaba.
the class MediaPushDataSource method init.
public synchronized void init() {
if (!dataMediaType.isMysql()) {
throw new UnsupportedOperationException("currently only support mysql type");
}
if (delegate != null) {
return;
}
if (dataSourceSupplier == null) {
dataSourceSupplier = MediaDatasourceSupplier.newInstance(dbGroupKey);
dataSourceSupplier.start();
dataSourceSupplier.addSwtichCallback(new DatasourceChangeCallback() {
public void masterChanged(DatasourceInfo newMaster) {
String newUrl = buildMysqlUrl(newMaster.getAddress().getAddress().getHostAddress(), newMaster.getAddress().getPort());
try {
((BasicDataSource) delegate).close();
DataSource newDelegate = doCreateDataSource(newUrl);
delegate = newDelegate;
} catch (SQLException e) {
logger.error("switch master error with url : " + originalUrl, e);
}
}
});
}
DatasourceInfo datasourceInfo = dataSourceSupplier.fetchMaster();
String url = buildMysqlUrl(datasourceInfo.getAddress().getAddress().getHostAddress(), datasourceInfo.getAddress().getPort());
delegate = doCreateDataSource(url);
}
Aggregations