日期:2014-05-16 浏览次数:20477 次
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.rowset.CachedRowSet;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.sun.rowset.CachedRowSetImpl;
import com.zhongwei.connectionpool.ConnectionPool;
public class ConnectionManager
{
private static ConnectionManager instance;
private static ComboPooledDataSource dataSource;
private ConnectionManager() throws IOException, SQLException,
PropertyVetoException
{
Properties p = new Properties();
p.load(this.getClass().getResourceAsStream("c3p0.properties"));
dataSource = new ComboPooledDataSource();
dataSource.setProperties(p);
System.out.println(p.getProperty("password"));
dataSource.setUser(p.getProperty("user"));
dataSource.setPassword(p.getProperty("password"));
dataSource.setJdbcUrl(p.getProperty("jdbcUrl"));
dataSource.setDriverClass(p.getProperty("driverClass"));
dataSource.setInitialPoolSize(Integer.valueOf(p.getProperty("initialPoolSize")));
dataSource.setMinPoolSize(Integer.valueOf(p.getProperty("minPoolSize")));
dataSource.setMaxPoolSize(Integer.valueOf(p.getProperty("maxPoolSize")));
dataSource.setMaxStatements(Integer.valueOf(p.getProperty("maxStatements")));
dataSource.setMaxIdleTime(Integer.valueOf(p.getProperty("maxIdleTime")));
dataSource.setPreferredTestQuery(p.getProperty("preferredTestQuery"));
dataSource.setIdleConnectionTestPeriod(Integer.valueOf(p.getProperty("idleConnectionTestPeriod")));
dataSource.setTestConnectionOnCheckout(Boolean.valueOf(p.getProperty("testConnectionOnCheckout")));
dataSource.setTestConnectionOnCheckin(Boolean.valueOf(p.getProperty("testConnectionOnCheckin")));
dataSource.setAcquireIncrement(Integer.valueOf(p.getProperty("acquireIncrement")));
dataSource.setAcquireRetryAttempts(Integer.valueOf(p.getProperty("acquireRetryAttempts")));
dataSource.setAcquireRetryDelay(Integer.valueOf(p.getProperty("acquireRetryDelay")));
dataSource.setAutomaticTestTable(p.getProperty("automaticTestTable"));
dataSource.setCheckoutTimeout(Integer.valueOf(p.getProperty("checkoutTimeout")));
dataSource.setAutoCommitOnClose(Boolean.valueOf(p.getProperty("autoCommitOnClose")));
}
public static final ConnectionManager getInstance()
{
if (instance == null)
{
try
{
instance = new ConnectionManager();
}
catch (Exception e)
{
e.printStackTrace();
}
}
return instance;
}
public synchronized final Connection getConnection()
{
Connection connection = null;
try
{
connection = dataSource.getConnection();
}
catch (Exception e)
{
e.printStackTrace();
}
return connection;
}
public static void main(String[] args) throws Exception
{
System.out.println("开始使用连接池...");
PreparedStatement pst = null;
for(int i=0;i<10;i++)
{
long beginTime = System.currentTimeMillis();
ConnectionManager inManager = ConnectionManager.getInstance();
Connection conn = inManager.getConnection();
try