C3p0连接池
package com.cmft.cmsd.utils;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import com.mchange.v2.c3p0.ComboPooledDataSource;
@Component
public class C3p0Utils {
private static String url;
private static String password;
private static String username;
private static String linkx001;
private static String driver;
private static String MaxPoolSize;
private static String MinPoolSize;
private static String InitialPoolSize;
private static String MaxStatements;
public static void setCpds(ComboPooledDataSource cpds) {
C3p0Utils.cpds = cpds;
}
private static ComboPooledDataSource cpds;
static {
cpds = new ComboPooledDataSource();
try {
ClassLoader classLoader = C3p0Utils.class.getClassLoader();
InputStream is = classLoader.getResourceAsStream("db.properties");
Properties props = new Properties();
props.load(is);
Map<String, String> map = System.getenv();
if (!StringUtils.isAllEmpty(map.get("H_DRIVER"), map.get("H_URL"), map.get("H_USERNAME"),
map.get("H_PASSWORD"), map.get("H_MAXPOOLSIZE"), map.get("H_MINPOOLSIZE"), map.get("H_INITIALPOOLSIZE"),
map.get("H_MAXSTATEMENTS"))) {
cpds.setDriverClass(map.get("H_DRIVER"));
cpds.setJdbcUrl(map.get("H_URL"));
cpds.setUser(map.get("H_USERNAME"));
cpds.setPassword(map.get("H_PASSWORD"));
cpds.setMaxPoolSize(Integer.parseInt(map.get("H_MAXPOOLSIZE")));
cpds.setMinPoolSize(Integer.parseInt(map.get("H_MINPOOLSIZE")));
cpds.setInitialPoolSize(Integer.parseInt(map.get("H_INITIALPOOLSIZE")));
cpds.setMaxStatements(Integer.parseInt(map.get("H_MAXSTATEMENTS")));
} else {
cpds.setDriverClass(props.getProperty("driver"));
cpds.setJdbcUrl(props.getProperty("url"));
cpds.setUser(props.getProperty("username"));
cpds.setPassword(props.getProperty("password"));
cpds.setMaxPoolSize(Integer.parseInt(props.getProperty("MaxPoolSize")));
cpds.setMinPoolSize(Integer.parseInt(props.getProperty("MinPoolSize")));
cpds.setInitialPoolSize(Integer.parseInt(props.getProperty("InitialPoolSize")));
cpds.setMaxStatements(Integer.parseInt(props.getProperty("MaxStatements")));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
Connection conn = null;
try {
conn = cpds.getConnection();
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
Connection connection = getConnection();
System.out.println(connection);
}
}
db.properties
url=jdbc:impala://ip:21051/default;AuthMech=3
password=2
username=3
driver=com.cloudera.impala.jdbc41.Driver
MaxPoolSize=20
MinPoolSize=2
InitialPoolSize=5
MaxStatements=30