Java tutorial
/* * Copyright (C) 2010---2014 (wuweixing)<349446658@qq.com> * * This file is part of Wabacus * * Wabacus is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.wabacus.config.database.datasource; import java.sql.Connection; import java.sql.SQLException; import java.util.List; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.dom4j.Element; import com.mchange.v2.c3p0.DataSources; import com.mchange.v2.c3p0.PoolConfig; import com.wabacus.exception.WabacusConfigLoadingException; import com.wabacus.exception.WabacusRuntimeException; import com.wabacus.util.DesEncryptTools; public class C3P0DataSource extends AbsDataSource { private static Log log = LogFactory.getLog(C3P0DataSource.class); private DataSource ds; public Connection getConnection() { try { log.debug("??" + this.getName() + "??"); return ds.getConnection(); } catch (SQLException e) { throw new WabacusRuntimeException("?" + this.getName() + "???", e); } } public DataSource getDataSource() { return this.ds; } public void closePool() { super.closePool(); try { if (this.ds != null) { log.debug("C3P0...................................................."); DataSources.destroy(this.ds); } this.ds = null; } catch (SQLException e) { throw new WabacusRuntimeException("c3p0??", e); } } public void loadConfig(Element eleDataSource) { super.loadConfig(eleDataSource); List lstEleProperties = eleDataSource.elements("property"); if (lstEleProperties == null || lstEleProperties.size() == 0) { throw new WabacusConfigLoadingException( "??" + this.getName() + "?alias?configfile?"); } String driver = null; String url = null; String user = null; String password = null; int minPoolSize = 1; int maxPoolSize = 100; int maxIdleTime = 0; int maxStatements = 0; int acquireIncrement = 1; int idleTestPeriod = 0; Element eleChild; String name; String value; for (int i = 0; i < lstEleProperties.size(); i++) { eleChild = (Element) lstEleProperties.get(i); name = eleChild.attributeValue("name"); value = eleChild.getText(); name = name == null ? "" : name.trim(); value = value == null ? "" : value.trim(); if (value.equals("")) { continue; } if (name.equals("max_size")) { maxPoolSize = Integer.parseInt(value); } else if (name.equals("min_size")) { minPoolSize = Integer.parseInt(value); } else if (name.equals("timeout")) { maxIdleTime = Integer.parseInt(value); } else if (name.equals("max_statements")) { maxStatements = Integer.parseInt(value); } else if (name.equals("idle_test_period")) { idleTestPeriod = Integer.parseInt(value); } else if (name.equals("acquire_increment")) { acquireIncrement = Integer.parseInt(value); } else if (name.equals("driver")) { driver = value; } else if (name.equals("url")) { url = value; } else if (name.equals("user")) { user = value; } else if (name.equals("password")) { password = value; } } if (driver.equals("") || url.equals("") || user.equals("")) { throw new WabacusConfigLoadingException("??" + this.getName() + "????driverurluser?"); } PoolConfig pcfg = new PoolConfig(); pcfg.setInitialPoolSize(minPoolSize); pcfg.setMinPoolSize(minPoolSize); pcfg.setMaxPoolSize(maxPoolSize); pcfg.setAcquireIncrement(acquireIncrement); pcfg.setMaxIdleTime(maxIdleTime); pcfg.setMaxStatements(maxStatements); pcfg.setIdleConnectionTestPeriod(idleTestPeriod); Properties connectionProps = new Properties(); password = password == null ? "" : password.trim(); if (password.startsWith("{3DES}")) { password = password.substring("{3DES}".length()); if (DesEncryptTools.KEY_OBJ == null) { throw new WabacusConfigLoadingException( "?????"); } password = DesEncryptTools.decrypt(password); } connectionProps.setProperty("user", user); connectionProps.setProperty("password", password); try { Class.forName(driver); DataSource unpooled = DataSources.unpooledDataSource(url, connectionProps); this.ds = DataSources.pooledDataSource(unpooled, pcfg); } catch (Exception e) { throw new WabacusConfigLoadingException("??" + this.getName() + "", e); } } protected void finalize() throws Throwable { closePool(); super.finalize(); } }