数据库的连接池总结

数据库的连接池

  • 首先这些有的需要jar包的都需要导入jar包比如说c3p0以及dbcp等我将以一下的方式来介绍他们的使用和简单的原理。他们这些连接池的实现都是基于实现java.sql包下的DataSource的接口来实现的,我们要明确 我们要明确数据库的连接池是分配管理数据的,可以重复使用一个数据库的资源,而不是不断重新建立

    1. 普通的连接池
      2.对于数据库的普通连接

    ‘ public static Connection connction() throws ClassNotFoundException, SQLException {
    //为接口附上实现类

    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:orcl", "scott", "123456");
    
    return connection;
    

    }

    public static void close(Connection connection, Statement statement, ResultSet resultSet) {

    if (connection != null) {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    
    }
    if (statement != null) {
        try {
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    
    }
    if (resultSet != null) {
        try {
            resultSet.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    
    }
    

    }’


  1. 利用连接池
    将信息分装在properties文件中,程序程序中调
1
2
3
4
5
6
7
8
9
10
11


‘driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
username=scott
password=123456
initSize=10
maxIdle=8
minIdle=5
maxActive=20
maxWait=5000’

最后通过在程序里取得代码的代码块的进行获取poperties的值,付给datesource的实现类所需要的信息即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
'static {
Properties properties = new Properties();
try {
properties.load(jdbcutil.class.getClassLoader().getResourceAsStream("jdbc.properties"));
System.out.println(jdbcutil.class.getClassLoader().getResource("jdbc.properties"));
dataSource = new BasicDataSource();
String driver = properties.getProperty("driver");
String url = properties.getProperty("url");
System.out.println(url);
String username = properties.getProperty("username");
String password = properties.getProperty("password");
String initSize = properties.getProperty("initSize");
String maxIdel = properties.getProperty("maxIdel");
String minIdel = properties.getProperty("minIdel");
String maxActive = properties.getProperty("maxActive");
String maxWait = properties.getProperty("maxWait");
dataSource = new BasicDataSource();
if (driver != null && !"".equals(driver)) {
System.out.println(driver);
dataSource.setDriverClassName(driver);
}
if (url != null && !"".equals(url)) {
dataSource.setUrl(url);
}
if (username != null && !"".equals(username)) {
dataSource.setUsername(username);
}
if (password != null && !"".equals(password)) {
dataSource.setPassword(password);
}
if (initSize != null && !"".equals(initSize)) {
dataSource.setInitialSize(Integer.parseInt(initSize));
}
if (maxIdel != null && !"".equals(maxIdel)) {
dataSource.setMaxIdle(Integer.parseInt(maxIdel));
}
if (minIdel != null && !"".equals(minIdel)) {
dataSource.setMinIdle(Integer.parseInt(minIdel));
}
if (maxWait != null && !"".equals(maxWait)) {
dataSource.setMaxWait(Integer.parseInt(maxWait));
}
if (maxActive != null && !"".equals(maxActive)) {
dataSource.setMaxActive(Integer.parseInt(maxActive));
}

} catch (IOException e) {
e.printStackTrace();
}

}`

  1. 另外就是通过c3p0的配置文件c3p0 的config.xml的文件中获得的参数只需呀在程序中DateSource d=new c3p0的实现类,通过观察我们可以得知其实在c3p0的内部就是通过代理的模式来实现的。主要dateresource 是一个接口,如果我们采用装饰者模式,我们需要实现接口中的所有的方法,这样不免有些麻烦发,而代理仅仅将对象以及方法和方法中的参数传递进去就可以了。
    1
    2
    3
    static {
    dataSource = new ComboPooledDataSource("mvcapp");
    }'

JNDI 因为sun公司的开发的,所以兼容性相对要好,而且支持异步连接,并且支持高并发的应用环境


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
在tomcat的config配置文件中:配置环境:
1.Tomcat-->conf--->context.xml
添加:

maxActive="100" maxIdle="30" maxWait="10000" username="root" password="ning"
driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/csdn"/>
2 . 在we.xml的文件中进行配置

mysql database connection
jdbc/csdn
javax.sql.DataSource
Container

3.数据库进行连接
Context context=new InitialContext();
DataSource dataSource= (DataSource)context.lookup("java:comp/env/jdbc/csdn");
Connection conn=dataSource.getConnection();
out.print(conn);

总结:其实这几种的,你叫简单的是c3p0封装的比较好,只需要new出来对象就可以直接调用方法获取connection,这就是对数据库的连接池的一个小小的总结吧。

你可能感兴趣的:(数据库的连接池总结)