C3p0数据库连接池的创建方法

来源:互联网 发布:淘宝小号查询 编辑:程序博客网 时间:2024/05/08 04:37
方法一:


?
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
52
53
54
55
package C3P0;
import java.sql.Connection;
import java.sql.SQLException;
import java.beans.PropertyVetoException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DBPool{      
   privatestatic DBPool dbPool;      
   privateComboPooledDataSource dataSource;    
 
   static{      
           dbPool =new DBPool();      
   }      
    
   publicDBPool(){      
           try{      
                 dataSource =new ComboPooledDataSource();      
                 dataSource.setUser("id");      
                 dataSource.setPassword("pw");      
                 dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/test?
autoReconnect=true&useUnicode=true&characterEncoding=GB2312");
                 dataSource.setDriverClass("com.mysql.jdbc.Driver");
                 dataSource.setInitialPoolSize(2);
                 dataSource.setMinPoolSize(1);
                 dataSource.setMaxPoolSize(10);
                 dataSource.setMaxStatements(50);
                 dataSource.setMaxIdleTime(60);      
           }catch (PropertyVetoException e) {      
               thrownew RuntimeException(e);      
           }      
   }      
 
   publicfinal static DBPool getInstance(){      
           returndbPool;      
   }      
 
   publicfinal Connection getConnection(){      
           try{      
               returndataSource.getConnection();      
           }  catch (SQLException e)   {      
               thrownew RuntimeException("无法从数据源获取连接",e);      
           }      
   }    
    
   publicstatic void main(String[] args) throws SQLException {
        Connection con =null;
        try{
        con = DBPool.getInstance().getConnection();
        }catch (Exception e){
        }finally {
        if(con != null)
        con.close();
        }
        }
 
}


方法二:

?
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
c3p0方法:
  配置文件:c3p0-config.xml
  <?xml version="1.0"encoding="UTF-8"?>
<c3p0-config>
<named-config name="userApp">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="acquireIncrement">5</property>
<property name="initialPoolSize">10</property>
<property name="minPoolSize">10</property>
<property name="maxPoolSize">20</property><!-- intergalactoApp adopts a different approach to
configuring statement caching -->
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property>
<!-- he's important, but there's only one of him -->
<user-overrides user="master-of-the-universe">
<property name="acquireIncrement">1</property>
<property name="initialPoolSize">1</property>
<property name="minPoolSize">1</property>
<property name="maxPoolSize">5</property>
<property name="maxStatementsPerConnection">50</property>
</user-overrides>
</named-config>
</c3p0-config>
连接数据库:
  packagecn.langzi.jdbc.c3p0;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DbConnection {
privatestatic DataSource dataSource;
static{
dataSource = new ComboPooledDataSource("userApp");
}
public static Connection getConnectioon() throwsSQLException{
return dataSource.getConnection();
}
}
方法三:


?
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
public final class ConnectionManager {
 privatestatic ConnectionManager instance;
 
 publicComboPooledDataSource ds;
 privatestatic String c3p0Properties ="c3p0.properties";
 
 privateConnectionManager() throwsException {
  Properties p =new Properties();
  p.load(this.getClass().getResourceAsStream(c3p0Properties));
  ds =new ComboPooledDataSource();
  ds.setUser(p.getProperty("user"));
  ds.setPassword(p.getProperty("user"));
  ds.setJdbcUrl(p.getProperty("user"));
  ds.setDriverClass(p.getProperty("user"));
  ds.setInitialPoolSize(Integer.parseInt(p.getProperty("initialPoolSize")));
  ds.setMinPoolSize(Integer.parseInt(p.getProperty("minPoolSize")));
  ds.setMaxPoolSize(Integer.parseInt(p.getProperty("maxPoolSize")));
  ds.setMaxStatements(Integer.parseInt(p.getProperty("maxStatements")));
  ds.setMaxIdleTime(Integer.parseInt(p.getProperty("maxIdleTime")));
 }
 
 publicstatic final ConnectionManager getInstance() {
  if(instance == null) {
   try{
    instance =new ConnectionManager();
   }catch (Exception e) {
    e.printStackTrace();
   }
  }
  returninstance;
 }
 
 publicsynchronized finalConnection getConnection() {
  try{
   returnds.getConnection();
  }catch (SQLException e) {
   e.printStackTrace();
  }
  returnnull;
 }
 
 protectedvoid finalize() throws Throwable {
  DataSources.destroy(ds);// 关闭datasource
  super.finalize();
 }
}

0 0
原创粉丝点击