Skip to content

Commit fdc58c4

Browse files
committed
Signed-off-by: chenhaoxiang <[email protected]>
1 parent aa02af9 commit fdc58c4

37 files changed

+571
-0
lines changed

myDbPoolUse/.classpath

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jdk1.7.0_04"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
6+
<classpathentry kind="lib" path="src/cn/hncu/bin/commons-dbcp2-2.1.1.jar"/>
7+
<classpathentry kind="lib" path="src/cn/hncu/bin/commons-logging-1.2.jar"/>
8+
<classpathentry kind="lib" path="src/cn/hncu/bin/commons-pool2-2.4.2.jar"/>
9+
<classpathentry kind="lib" path="src/cn/hncu/bin/mysql-connector-java-5.1.39-bin.jar"/>
10+
<classpathentry kind="lib" path="src/cn/hncu/bin/c3p0-0.9.1.2.jar"/>
11+
<classpathentry kind="lib" path="src/cn/hncu/bin/c3p0-sources-0.9.1.2.jar"/>
12+
<classpathentry kind="lib" path="src/cn/hncu/bin/commons-dbutils-1.6.jar"/>
13+
<classpathentry kind="lib" path="src/cn/hncu/bin/commons-dbutils-ext.jar"/>
14+
<classpathentry kind="output" path="bin"/>
15+
</classpath>

myDbPoolUse/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>myDbPoolUse</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#Mon Aug 15 15:13:10 CST 2016
2+
eclipse.preferences.version=1
3+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
5+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
6+
org.eclipse.jdt.core.compiler.compliance=1.6
7+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
8+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
9+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
10+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
11+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
12+
org.eclipse.jdt.core.compiler.source=1.6

myDbPoolUse/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BasicDataSource创建DataSource(DBCP连接池配置)
2+
ComboPooledDataSource(C3P0连接池配置)
3+
Apache的DBUtils框架使用

myDbPoolUse/bin/c3p0-config.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<c3p0-config>
2+
<!-- 默认配置,如果没有指定则使用这个配置 -->
3+
<default-config>
4+
<property name="driverClass">com.mysql.jdbc.Driver</property>
5+
<property name="jdbcUrl">
6+
<![CDATA[jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=UTF-8]]>
7+
</property>
8+
<property name="user">root</property>
9+
<property name="password">1234</property>
10+
<!-- 初始化池大小 -->
11+
<property name="initialPoolSize">2</property>
12+
<!-- 最大空闲时间 -->
13+
<property name="maxIdleTime">30</property>
14+
<!-- 最多有多少个连接 -->
15+
<property name="maxPoolSize">10</property>
16+
<!-- 最少几个连接 -->
17+
<property name="minPoolSize">2</property>
18+
<!-- 每次最多可以执行多少个批处理语句 -->
19+
<property name="maxStatements">50</property>
20+
</default-config>
21+
<!-- 命名的配置 -->
22+
<named-config name="demo">
23+
<property name="driverClass">com.mysql.jdbc.Driver</property>
24+
<property name="jdbcUrl"><![CDATA[jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=UTF-8]]></property>
25+
<property name="user">root</property>
26+
<property name="password">1234</property>
27+
<property name="acquireIncrement">5</property><!-- 如果池中数据连接不够时一次增长多少个 -->
28+
<property name="initialPoolSize">100</property>
29+
<property name="minPoolSize">50</property>
30+
<property name="maxPoolSize">1000</property>
31+
<property name="maxStatements">0</property>
32+
<property name="maxStatementsPerConnection">5</property> <!-- he's important, but there's only one of him -->
33+
</named-config>
34+
</c3p0-config>
1.81 KB
Binary file not shown.
1.12 KB
Binary file not shown.
603 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
2.53 KB
Binary file not shown.
1.64 KB
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
##MySQL
2+
driver=com.mysql.jdbc.Driver
3+
url=jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=utf-8
4+
username=root
5+
password=1234
6+
7+
##Oracle
8+
#driver=oracle.jdbc.driver.OracleDriver
9+
#url=jdbc:oracle:thin:@localhost:1521:orcl
10+
#username=scott
11+
#password=tiger
Binary file not shown.
2.23 KB
Binary file not shown.

myDbPoolUse/myDbPoolUse.zip

4.46 MB
Binary file not shown.

myDbPoolUse/src/c3p0-config.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<c3p0-config>
2+
<!-- 默认配置,如果没有指定则使用这个配置 -->
3+
<default-config>
4+
<property name="driverClass">com.mysql.jdbc.Driver</property>
5+
<property name="jdbcUrl">
6+
<![CDATA[jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=UTF-8]]>
7+
</property>
8+
<property name="user">root</property>
9+
<property name="password">1234</property>
10+
<!-- 初始化池大小 -->
11+
<property name="initialPoolSize">2</property>
12+
<!-- 最大空闲时间 -->
13+
<property name="maxIdleTime">30</property>
14+
<!-- 最多有多少个连接 -->
15+
<property name="maxPoolSize">10</property>
16+
<!-- 最少几个连接 -->
17+
<property name="minPoolSize">2</property>
18+
<!-- 每次最多可以执行多少个批处理语句 -->
19+
<property name="maxStatements">50</property>
20+
</default-config>
21+
<!-- 命名的配置 -->
22+
<named-config name="demo">
23+
<property name="driverClass">com.mysql.jdbc.Driver</property>
24+
<property name="jdbcUrl"><![CDATA[jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=UTF-8]]></property>
25+
<property name="user">root</property>
26+
<property name="password">1234</property>
27+
<property name="acquireIncrement">5</property><!-- 如果池中数据连接不够时一次增长多少个 -->
28+
<property name="initialPoolSize">100</property>
29+
<property name="minPoolSize">50</property>
30+
<property name="maxPoolSize">1000</property>
31+
<property name="maxStatements">0</property>
32+
<property name="maxStatementsPerConnection">5</property> <!-- he's important, but there's only one of him -->
33+
</named-config>
34+
</c3p0-config>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cn.hncu.C3p0;
2+
3+
import java.beans.PropertyVetoException;
4+
import java.sql.Connection;
5+
import java.sql.SQLException;
6+
7+
import org.junit.Test;
8+
9+
import com.mchange.v2.c3p0.ComboPooledDataSource;
10+
11+
public class C3p0Demo {
12+
13+
@Test
14+
// 纯Java方法使用c3p0
15+
public void C3p0Demo() throws PropertyVetoException, SQLException {
16+
ComboPooledDataSource pool = new ComboPooledDataSource();
17+
pool.setUser("root");// 用户姓名
18+
pool.setPassword("1234");// 用户密码
19+
pool.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=utf-8");// MySQL数据库连接url
20+
pool.setDriverClass("com.mysql.jdbc.Driver");
21+
22+
// pool可以调用set方法进行Connection池的配置
23+
24+
// 连接关闭之后,内存会被释放,下次取时会重新开(内存地址不共用)
25+
for (int i = 0; i < 20; i++) {
26+
Connection con = pool.getConnection();
27+
System.out.println(i + ":" + con);
28+
if (i % 2 == 0) {
29+
con.close();
30+
}
31+
}
32+
}
33+
34+
@Test
35+
// 演示采用配置文件的方式使用c3p0
36+
public void C3p0PropertyDemo() throws SQLException {
37+
ComboPooledDataSource pool = new ComboPooledDataSource();//空参,自动到classpath目录下面加载“c3p0-config.xml”配置文件---配置文件的存储位置和名称必须是这样,且使用“默认配置”
38+
//ComboPooledDataSource pool = new ComboPooledDataSource("demo");//加载“c3p0-config.xml”文件中定义的“demo”这个配置元素
39+
for(int i=0;i<25;i++){
40+
Connection con = pool.getConnection();
41+
System.out.println(i+":"+ con.hashCode());
42+
}
43+
}
44+
45+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cn.hncu.C3p0;
2+
3+
import java.sql.Connection;
4+
import java.sql.SQLException;
5+
6+
import javax.sql.DataSource;
7+
8+
import com.mchange.v2.c3p0.ComboPooledDataSource;
9+
10+
public class C3p0Pool {
11+
//我们的这个包装,只是为了把c3p0池做成让每个线程(客户端)获得的是同一个连接,方便做b/s框架下的事务
12+
private static DataSource pool;
13+
private static ThreadLocal<Connection> t = new ThreadLocal<Connection>();
14+
static{
15+
pool = new ComboPooledDataSource("demo");
16+
//这里的参数视你的配置文件而定,也可以不写,用默认的配置
17+
}
18+
19+
public static DataSource getDataSource(){
20+
return pool;
21+
}
22+
23+
public static Connection getConnection() throws SQLException{
24+
Connection con = t.get();
25+
if(con==null){
26+
con=pool.getConnection();
27+
t.set(con);
28+
}
29+
return con;
30+
}
31+
}
603 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cn.hncu.dbcp;
2+
3+
import java.sql.Connection;
4+
import java.sql.SQLException;
5+
import java.util.Properties;
6+
7+
import javax.sql.DataSource;
8+
9+
import org.apache.commons.dbcp2.BasicDataSource;
10+
import org.apache.commons.dbcp2.BasicDataSourceFactory;
11+
import org.junit.Test;
12+
13+
public class DbcpPoolDemo {
14+
15+
// 纯Java方式设置参数,使用dbcp池
16+
@Test
17+
public void testDbcp() {
18+
BasicDataSource pool = new BasicDataSource();// 连接池
19+
pool.setUsername("root");
20+
pool.setPassword("1234");
21+
pool.setDriverClassName("com.mysql.jdbc.Driver");
22+
pool.setUrl("jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=utf-8");
23+
24+
System.out.println(pool.getMaxIdle());// 最大空闲时间。如果一个用户获取一个连接,不用,多长时间会被强行收回
25+
System.out.println(pool.getMaxWaitMillis());// 在抛出异常之前,池等待连接被回收的最长时间(当没有可用连接时)。设置为-1表示无限等待。
26+
System.out.println(pool.getInitialSize());// 初始化时有几个Connection
27+
System.out.println(pool.getMaxTotal());// 最多能有多少个Connection
28+
29+
System.out.println("----------------");
30+
// pool.setMaxTotal(20);//可以我们自己设置池的相关参数,如最大连接数
31+
32+
// 从它的池中获取连接
33+
for (int i = 0; i < 20; i++) {
34+
Connection con = null;
35+
try {
36+
con = pool.getConnection();
37+
} catch (SQLException e) {
38+
e.printStackTrace();
39+
}
40+
System.out.println(i + ":" + con.hashCode());
41+
}
42+
}
43+
44+
// 通过配置文件方式设置参数,使用dbcp池
45+
@Test
46+
public void testPropertyFile() throws Exception {
47+
Properties p = new Properties();
48+
p.load(DbcpPoolDemo.class.getResourceAsStream("dbcp.properties"));// 配置文件和当前类的class放在一起
49+
// p.load(DbcpPoolDemo.class.getClassLoader().getResourceAsStream("dbcp.properties"));//配置文件要放在src(bin)的根目录---classpath的根
50+
DataSource pool = BasicDataSourceFactory.createDataSource(p);
51+
// 从它的池中获取连接
52+
for (int i = 0; i < 20; i++) {
53+
Connection con = pool.getConnection();
54+
System.out.println(con.hashCode());
55+
if (i % 2 == 0) {
56+
con.close();
57+
}
58+
}
59+
}
60+
61+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package cn.hncu.dbcp;
2+
3+
import java.io.IOException;
4+
import java.sql.Connection;
5+
import java.sql.SQLException;
6+
import java.util.Properties;
7+
8+
import javax.sql.DataSource;
9+
10+
import org.apache.commons.dbcp2.BasicDataSourceFactory;
11+
12+
public class DbcpUtil {
13+
private static DataSource pool;
14+
private static ThreadLocal<Connection> t = new ThreadLocal<Connection>();
15+
static{
16+
17+
try {
18+
//读取配置文件
19+
Properties p = new Properties();
20+
p.load(DbcpUtil.class.getResourceAsStream("dbcp.properties"));// 配置文件和当前类的class放在一起
21+
pool = BasicDataSourceFactory.createDataSource(p);
22+
} catch (IOException e) {
23+
e.printStackTrace();
24+
} catch (Exception e) {
25+
e.printStackTrace();
26+
}
27+
}
28+
29+
//返回DataSource--池
30+
public static DataSource getDataSource(){
31+
return pool;
32+
}
33+
34+
public static Connection getConnection() throws SQLException{
35+
//从本地线程管理对象t中拿
36+
Connection con = t.get();
37+
if(con==null){
38+
con=pool.getConnection();
39+
//放入t中
40+
t.set(con);
41+
}
42+
return con;
43+
}
44+
45+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
##MySQL
2+
driver=com.mysql.jdbc.Driver
3+
url=jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=utf-8
4+
username=root
5+
password=1234
6+
7+
##Oracle
8+
#driver=oracle.jdbc.driver.OracleDriver
9+
#url=jdbc:oracle:thin:@localhost:1521:orcl
10+
#username=scott
11+
#password=tiger

0 commit comments

Comments
 (0)