首页 > 技术文章 > 创建 连接池

tuxiaoer 2021-03-10 18:38 原文

import com.github.houbb.thread.pool.datasource.PooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: Supermap·F
 * @descripition:
 * @date: created in 9:34 2020/8/20
 * @modify: Copyright (c) Supermap All Rights Reserved.
 */
@Configuration
public class ThirdDataSource {

    @Value("${driver-name}")
    private String config_driverName;

    @Value("${db-config.db-url}")
    private String config_url;

    @Value("${name}")
    private String config_username;

    @Value("${password}")
    private String config_password;

    @Bean
    public PooledDataSource unpooledDataSource(){
        PooledDataSource dataSource = new PooledDataSource();
        dataSource.setDriverClass(config_driverName);
        dataSource.setJdbcUrl(config_url);
        dataSource.setUser(config_username);
        dataSource.setPassword(config_password);
        dataSource.setMinSize(3);
        dataSource.setMaxSize(10);
        dataSource.init();
        return dataSource;
    }

}
如何调用   

注入

@Autowired
   private ThirdDataSource thirdDataSource;


方法内调用:

PooledDataSource pooledDataSource = thirdDataSource.unpooledDataSource();
connection = pooledDataSource.getConnection();
statement = connection.createStatement();
String deleteSql = "delete from t_user";
statement.executeUpdate(deleteSql);

 

推荐阅读