package com.power.mysql;
|
|
import java.beans.PropertyVetoException;
|
import java.sql.Connection;
|
import java.sql.SQLException;
|
|
import com.mchange.v2.c3p0.ComboPooledDataSource;
|
|
public class MysqlConnPool {
|
private ComboPooledDataSource mysql_ds = new ComboPooledDataSource();
|
private int mSqlPort = 5306;
|
|
public MysqlConnPool(String server_ip, int port, int conncount_max)
|
{
|
try {
|
init(server_ip, port, conncount_max);
|
} catch (PropertyVetoException e) {
|
e.printStackTrace();
|
}
|
}
|
public void init(String server_ip, int port, int conncount_max) throws PropertyVetoException
|
{
|
mSqlPort = port;
|
mysql_ds.setDriverClass("com.mysql.jdbc.Driver");
|
mysql_ds.setJdbcUrl("jdbc:mysql://" + server_ip + ":" + mSqlPort);
|
mysql_ds.setUser("root");
|
mysql_ds.setPassword("lmx8688139");
|
mysql_ds.setMaxPoolSize(conncount_max);
|
mysql_ds.setMinPoolSize(2);
|
mysql_ds.setIdleConnectionTestPeriod(300);
|
}
|
public Connection getConn()
|
{
|
Connection con = null;
|
try {
|
con = mysql_ds.getConnection();
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
return con;
|
}
|
|
public int getSqlConnPort() {
|
return mSqlPort;
|
}
|
|
public int getNumBusyConnections() {
|
int cnt = 0;
|
try {
|
cnt = mysql_ds.getNumBusyConnections();
|
} catch (SQLException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
return cnt;
|
}
|
|
public int getNumIdleConnections() {
|
int cnt = 0;
|
try {
|
cnt = mysql_ds.getNumIdleConnections();
|
} catch (SQLException e) {
|
// TODO Auto-generated catch block
|
e.printStackTrace();
|
}
|
return cnt;
|
}
|
|
public int getMaxPoolSize() {
|
return (mysql_ds.getMaxPoolSize());
|
}
|
|
public void setMaxPoolSize(int size) {
|
mysql_ds.setMaxPoolSize(size);
|
}
|
}
|
|