JAVA 连接池 BoneCP 测试(2)- with spring 3

来源:互联网 发布:软件sai下载 编辑:程序博客网 时间:2024/05/12 09:42

额外JAR包:

org.springframework.expression-3.0.1.RELEASE.jar

org.springframework.asm-3.0.1.RELEASE.jar

org.springframework.beans-3.0.1.RELEASE.jar

 

p:statementCacheSize="100"

<bean id="masterDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close"

p:driverClass="com.mysql.jdbc.Driver" 

p:jdbcUrl="jdbc:mysql://localhost/pesdog?autoReconnectForPools=true&amp;autoReconnect=true&amp;characterEncoding=utf-8" 

p:username="root" p:password="" 

p:idleConnectionTestPeriod="60" p:idleMaxAge="240"

p:maxConnectionsPerPartition="30" p:minConnectionsPerPartition="10"

p:partitionCount="3" p:acquireIncrement="5" 

p:releaseHelperThreads="3"

/>

 

<context:annotation-config/><bean id="foo" class="test.Foo">

</bean>


 

 

public static void main(String[] args){

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

Foo foo = (Foo)context.getBean("foo");

try{

foo.test();

}catch(Exception ex){

ex.printStackTrace();

}

}

 

@Component

public class Foo {

//@Autowired

private DataSource masterDataSource;

 

public DataSource getMasterDataSource() {

return masterDataSource;

}

 

@Required

@Autowired

public void setMasterDataSource(DataSource masterDataSource) {

this.masterDataSource = masterDataSource;

}

public void test() throws Exception{

Connection cn = this.masterDataSource.getConnection();

Statement stmt = cn.createStatement();

String sql = "select username,loginid from sys_user";

System.out.println(sql);

ResultSet rs = stmt.executeQuery(sql);

while(rs.next()){

System.out.println(rs.getString("username"));

System.out.println(rs.getString("loginid"));

}

}

}