日期:2014-05-16 浏览次数:20596 次
import org.apache.commons.dbcp.BasicDataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; import java.sql.SQLException; import java.sql.Connection; import java.util.Properties; public class ConnectionSource { private static BasicDataSource dataSource = null; public ConnectionSource() { } public static void init() { if (dataSource != null) { try { dataSource.close(); } catch (Exception e) { // } dataSource = null; } try { Properties p = new Properties(); p.setProperty("driverClassName", "oracle.jdbc.driver.OracleDriver"); p.setProperty("url", "jdbc:oracle:thin:@192.168.0.1:1521:testDB"); p.setProperty("password", "scott"); p.setProperty("username", "tiger"); p.setProperty("maxActive", "30"); p.setProperty("maxIdle", "10"); p.setProperty("maxWait", "1000"); p.setProperty("removeAbandoned", "false"); p.setProperty("removeAbandonedTimeout", "120"); p.setProperty("testOnBorrow", "true"); p.setProperty("logAbandoned", "true"); dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(p); } catch (Exception e) { // } } public static synchronized Connection getConnection() throws SQLException { if (dataSource == null) { init(); } Connection conn = null; if (dataSource != null) { conn = dataSource.getConnection(); } return conn; } }
<!-- ===== 配置Spring数据源 ========== --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="org.sqlite.JDBC" /> <property name="url" value="jdbc:sqlite:search.db" /> <property name="maxIdle" value="10"/> <property name="maxActive" value="100"/> <property name="maxWait" value="10000"/> <property name="validationQuery" value="select 1"/> <property name="testOnBorrow" value="false"/> <property name="testWhileIdle" value="true"/> <property name="timeBetweenEvictionRunsMillis" value="1200000"/> <property name="minEvictableIdleTimeMillis" value="1800000"/> <property name="numTestsPerEvictionRun" value="5"/> <property name="defaultAutoCommit" value="true"/> </bean>