【Java开源代码栏目提醒】:网学会员为广大网友收集整理了,C3p0Configuration.java,希望对大家有所帮助!
/*
* Copyright 2009 rengang
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ibatis.common.jdbc;
import java.util.Map;
import java.util.Properties;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.PoolConfig;
import com.mchange.v2.c3p0.DataSources;
public class C3p0Configuration {
private DataSource dataSource;
public C3p0Configuration(Map properties) {
try {
dataSource = buildDataSource(properties);
} catch (Exception e) {
throw new RuntimeException(
"Error initializing C3p0Configuration. Cause: " + e, e);
}
}
public DataSource getDataSource() {
return dataSource;
}
//鏍规嵁閰嶇疆淇℃伅锛屽垱寤轰竴涓猚3p0鐨凞ataSource
private DataSource buildDataSource(Map map) throws Exception {
DataSource thisDataSource = null;
String jdbcDriver = null;
String jdbcUrl = null;
String username = null;
Properties connectionProps = new Properties();
try {
if (map.containsKey("JDBC.Driver")) {
//瀵归厤缃俊鎭繘琛屽垵濮嬪寲澶勭悊锛屽苟鍒ゆ柇鏄惁鏈夐敊
jdbcDriver = (String) map.get("JDBC.Driver");
try {
Class.forName(jdbcDriver);
} catch (ClassNotFoundException cnfe) {
throw new Exception(
"Could not instantiate C3P0 jdbcDriver", cnfe);
}
if (map.get("JDBC.ConnectionURL") == null) {
throw new Exception("jdbcUrl is null.");
}
if (map.get("JDBC.Username") == null) {
throw new Exception("Username is null.");
}
jdbcUrl = (String) map.get("JDBC.ConnectionURL");
username = (String) map.get("JDBC.Username");
String password = (String) map.get("JDBC.Password");
connectionProps.setProperty("user", username);
connectionProps.setProperty("password", password);
//鍒濆鍖栬繛鎺ユ睜鐨勪俊鎭?
PoolConfig poolConfig = new PoolConfig();
if (notEmpty((String) map.get("Pool.MaximumActiveConnections"))) {
poolConfig.setMaxPoolSize(Integer.valueOf((String) map
.get("Pool.MaximumActiveConnections")));
} else {
poolConfig.setMaxPoolSize(20);
}
if (notEmpty((String) map.get("Pool.MaximumWait"))) {
poolConfig.setMaxIdleTime(Integer.valueOf((String) map
.get("Pool.MaximumWait")));
} else {
poolConfig.setMaxIdleTime(60000);
}
//鑾峰彇涓涓病鏈夎繛鎺ユ睜鐨凞ataSource
DataSource unpooled = DataSources.unpooledDataSource(jdbcUrl,
connectionProps);
// 鑾峰彇涓涓湁杩炴帴姹犵殑DataSource
thisDataSource = DataSources.pooledDataSource(unpooled,
poolConfig);
} else {
throw new Exception("JDBC.Driver is null.");
}
} catch (Exception e) {
throw new Exception("Could not instantiate C3P0 connection pool", e);
}
return thisDataSource;
}
private boolean notEmpty(String s) {
return s != null && s.length() > 0;
}
}
上一篇:
C3p0Application.java
下一篇:
让我掉下眼泪的