当前位置:Gxlcms > 数据库问题 > 数据库连接池技术

数据库连接池技术

时间:2021-07-01 10:21:17 帮助过:4人阅读

//方法一,通过字符串来设置连接参数
public
class JDBCUtil1 { private static BasicDataSource ds = null; private static String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8"; private static String user="root"; private static String password="123123"; private static String driverClass="com.mysql.jdbc.Driver"; static{ ds = new BasicDataSource(); //最基本的链接配置 ds.setUrl(url); ds.setUsername(user); ds.setPassword(password); ds.setDriverClassName(driverClass); //初始化连接数 ds.setInitialSize(5); //最大连接数 ds.setMaxActive(10); //最大等待时间 ds.setMaxWait(3000); } public static Connection getConnection(){ try { return ds.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return null; } }
//方法二通过配置文件读取连接参数
public
class JDBCUtil { private static BasicDataSource ds = null; static{ //properties中的key不能乱写 //通过BasicDataSource的set方法来得到key的写法,去掉set,首字母小写即可。 Properties prop = new Properties();
     //读取配置文件,配置文件在下面  InputStream inputStream
= JDBCUtil.class.getResourceAsStream("/db.properties"); try { prop.load(inputStream); ds = (BasicDataSource) BasicDataSourceFactory.createDataSource(prop); } catch (Exception e) { e.printStackTrace(); } } public static Connection getConnection(){ try { return ds.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return null; } }

配置文件名db.properties

url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
username=root
password=123123
driverClassName=com.mysql.jdbc.Driver
initialSize=5
maxActive=10
maxWait=3000

C3P0配置数据库连接池,需要导入c3p0-0.9.5.2.jar和mchange-commons-java-0.2.11.jar

配置方式一:

public class JDBCUtil1 {
    
    private static ComboPooledDataSource ds = null;
    
    private static String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8";
    private static String user="root";
    private static String password="123123";
    private static String driverClass="com.mysql.jdbc.Driver";
    
    static{
        ds = new ComboPooledDataSource();
        //基本配置
        ds.setJdbcUrl(url);
        ds.setUser(user);
        ds.setPassword(password);
        try {
            ds.setDriverClass(driverClass);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        
        ds.setInitialPoolSize(5);
        ds.setMaxPoolSize(10);
        //设置超时时间
        ds.setCheckoutTimeout(3000);
    }
    
    public static Connection getConnection(){
        try {
            return ds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    

}

配置方式二:通过读取配置文件c3p0-config.xml,配置文件必须命名唯一,只能叫c3p0-config.xml

配置文件信息

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <!-- 默认配置 -->
    <default-config>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
        <property name="user">root</property>
        <property name="password">123123</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="initialPoolSize">5</property>
        <property name="maxPoolSize">10</property>
        <property name="checkoutTimeout">3000</property>
    </default-config>
    
    <!--  -->
    <named-config name="intergalactoApp">
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
        <property name="user">root</property>
        <property name="password">123123</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="initialPoolSize">10</property>
        <property name="maxPoolSize">20</property>
        <property name="checkoutTimeout">5000</property>
    </named-config>
</c3p0-config>

代码示范

public class JDBCUtil {
    private static ComboPooledDataSource ds = null;
    static {
        // 读取的是default-config
        // ds = new ComboPooledDataSource();
        // 读取的是<named-config name="intergalactoApp">
        ds = new ComboPooledDataSource("intergalactoApp");
    }

    public static Connection getConnection() {
        try {
            return ds.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}

 

数据库连接池技术

标签:tac   配置   put   cti   new   change   down   方式   ons   

人气教程排行