日期:2014-05-19 浏览次数:20792 次
public final class ConnectManager{ private static ConnectManager connect = null; private ComboPooledDataSource ds = null; private final ReentrantLock lock; private ConnectManager() { lock = new ReentrantLock(); ds = new ComboPooledDataSource(); try { ds.setDriverClass(GetProperInfo.getProperName(DRIVERCLASS)); } catch (PropertyVetoException e) { e.printStackTrace(); } ds.setJdbcUrl(GetProperInfo.getProperName(JDBC_URL)); ds.setUser(GetProperInfo.getProperName(JDBC_USER)); ds.setPassword(GetProperInfo.getProperName(JDBC_PASSWORD)); ds.setIdleConnectionTestPeriod(GetProperInfo .getIntProper(CONNECTIONTESTPERIOD)); // <!--每60秒检查所有连接池中的空闲连接。Default: // 0 --> ds.setInitialPoolSize(GetProperInfo.getIntProper(INITIALPOOLSIZE)); // <!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: // 3 // --> ds.setMaxIdleTime(GetProperInfo.getIntProper(MAXIDLETIME));// <!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: // 0 --> ds.setMaxPoolSize(GetProperInfo.getIntProper(MAXPOOLSIZE));// !--连接池中保留的最大连接数。Default: // 15 --> System.out.println("database info:"); System.out .println("driver=" + GetProperInfo.getProperName(DRIVERCLASS)); System.out.println("url=" + GetProperInfo.getProperName(JDBC_URL)); } public static synchronized ConnectManager getInstance() { if (connect == null) { connect = new ConnectManager(); } return connect; } public final Connection getConn() throws SQLException { final ReentrantLock lock = this.lock; lock.lock(); try { return ds.getConnection(); } finally { lock.unlock(); } } @Override protected void finalize() throws Throwable { DataSources.destroy(ds); // 关闭datasource super.finalize(); } }
------解决方案--------------------
2)我来回答一下。
一般C3P0不会单独使用的,而在Spring中配置,所以没事,不需要知道配置文件叫什么名字。
<context:property-placeholder location="classpath:c3p0_2.properties" /> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driverClass}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <property name="minPoolSize" value="${c3p0.minPoolSize}" /> <property name="maxPoolSize" value="${c3p0.maxPoolSize}" /> <p