spring-整合c3p0连接池

pom.xml


    4.0.0
    com.gq
    spring-ioc-v2
    0.0.1-SNAPSHOT
    
        
            org.springframework
            spring-context
            4.3.9.RELEASE
        
        
            junit
            junit
            4.12
        
        
        
            mysql
            mysql-connector-java
            5.1.40
        
        
            c3p0
            c3p0
            0.9.1.2
        
    
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.5.1
                
                    1.8
                    1.8
                    UTF-8
                
            
        
    

/spring-ioc-v2/src/main/resources/config.properties

jdbcDriver=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///test
jdbcUsername=root
jdbcPassword=123456

/spring-ioc-v2/src/main/resources/spring-configs.xml



    
    
    
    
    
    
    
    
    
        
        
        
        
    

package test;

import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestBase {
    protected ClassPathXmlApplicationContext ctx;
    @Before
    public void init(){
        ctx=new ClassPathXmlApplicationContext("spring-configs.xml");
    }
    @After
    public void close(){
        ctx.close();
    }
}
package test;

import java.sql.Connection;
import java.sql.SQLException;

import org.junit.Assert;
import org.junit.Test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class TestDataSource02 extends TestBase{
    @Test
    public void testC3PDataSource() throws SQLException{
        //获取bean对象
        ComboPooledDataSource ds = ctx.getBean("c3p0",ComboPooledDataSource.class);
        //用接口接收对象,测试对象值是否为空
        Assert.assertNotEquals(null, ds);
        //通过bean对象获取与数据库的连接
        Connection conn = ds.getConnection();
        System.out.println(conn);
    }
}

运行结果

九月 04, 2018 2:48:25 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6ed3ef1: startup date [Tue Sep 04 14:48:25 CST 2018]; root of context hierarchy
九月 04, 2018 2:48:25 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-configs.xml]
OpenDataSource.OpenDataSource()-2
OpenDataSource.init()
九月 04, 2018 2:48:26 下午 com.mchange.v2.log.MLog 
信息: MLog clients using java 1.4+ standard logging.
九月 04, 2018 2:48:26 下午 com.mchange.v2.c3p0.C3P0Registry banner
信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
九月 04, 2018 2:48:26 下午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hge19c9x19bjvx5ae8xt|46238e3f, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hge19c9x19bjvx5ae8xt|46238e3f, idleConnectionTestPeriod -> 0, initialPoolSize -> 3, jdbcUrl -> jdbc:mysql:///test, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 0, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 15, maxStatements -> 0, maxStatementsPerConnection -> 0, minPoolSize -> 3, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
com.mchange.v2.c3p0.impl.NewProxyConnection@38c5cc4c
九月 04, 2018 2:48:26 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@6ed3ef1: startup date [Tue Sep 04 14:48:25 CST 2018]; root of context hierarchy
OpenDataSource.destory()

你可能感兴趣的:(spring-整合c3p0连接池)