自定义连接池
测试类:
@Test
public void testPool() throws SQLException {
Connection conn=null;
PreparedStatement ps=null;
MyDataSource dataSource=null;
try {
dataSource=new MyDataSource();
conn = dataSource.getConnection();
conn.setAutoCommit(false);
String sql="update student set age=age-? where sex=?";
ps = conn.prepareStatement(sql);
ps.setInt(1, 1);
ps.setInt(2, 31);
ps.executeUpdate();
ps.setInt(1, -1);
ps.setInt(2, 2);
ps.executeUpdate();
conn.commit();
} catch (SQLException e) {
e.printStackTrace();
conn.rollback();
}finally {
ps.close();
JDBCUtil.release(conn, ps);
}
}
public class ConnectionWrap implements Connection{
List<Connection> list=null;
Connection conn=null;
public ConnectionWrap(Connection conn,List<Connection> list) {
super();
this.conn=conn;
this.list=list;
}
@Override
public void close() throws SQLException {
list.add(conn);
}
@Override
public PreparedStatement prepareStatement(String sql) throws SQLException {
return conn.prepareStatement(sql);
}
}
接口类:
public class MyDataSource implements DataSource{
List<Connection> list=new ArrayList<Connection>();
public MyDataSource() {
for (int i = 0; i < 10; i++) {
Connection connection=JDBCUtil.getConn();
list.add(connection);
}
}
@Override
public Connection getConnection() throws SQLException {
if (list.size()==0) {
for (int i = 0; i <3; i++) {
Connection connection=JDBCUtil.getConn();
list.add(connection);
}
}
Connection conn = list.remove(0);
Connection connection=new ConnectionWrap(conn, list);
return connection;
}
}
第三方连接池
DBCP
代码形式
public void testDBCP01(){
Connection conn = null;
PreparedStatement ps = null;
try {
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost/bank");
dataSource.setUsername("root");
dataSource.setPassword("root");
conn = dataSource.getConnection();
String sql = "insert into account values(null , ? , ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, "admin");
ps.setInt(2, 1000);
ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally {
JDBCUtil.release(conn, ps);
}
}
使用配置文件形式


配置文件:
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/java
username=root
password=zengqiang
#
initialSize=10
#最大连接数量
maxActive=50
#
maxIdle=20
#
minIdle=5
#
maxWait=60000
#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;]
#注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。
connectionProperties=useUnicode=true;characterEncoding=gbk
#指定由连接池所创建的连接的自动提交(auto-commit)状态。
defaultAutoCommit=true
#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。
#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation=READ_UNCOMMITTED
@Test
public void testdbcp() {
Connection connection=null;
PreparedStatement pStatement=null;
try {
BasicDataSourceFactory bsf=new BasicDataSourceFactory();
Properties properties= new Properties();
properties.load(new FileInputStream("src//dbcpconfig.properties"));
DataSource bSource=bsf.createDataSource(properties);
connection=bSource.getConnection();
String sql="select * from student";
pStatement=connection.prepareStatement(sql);
ResultSet executeQuery = pStatement.executeQuery();
while (executeQuery.next()) {
System.out.println(executeQuery.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtil.release(connection, pStatement);
}
}
C3P0
代码形式
Connection conn = null;
PreparedStatement ps = null;
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost/bank");
dataSource.setUser("root");
dataSource.setPassword("root");
conn = dataSource.getConnection();
String sql = "insert into account values(null , ? , ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, "admi234n");
ps.setInt(2, 103200);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtil.release(conn, ps);
}
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost/java</property>
<property name="user">root</property>
<property name="password">zengqiang</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</default-config>
<named-config name="oracle">
<property name="acquireIncrement">50</property>
<property name="initialPoolSize">100</property>
<property name="minPoolSize">50</property>
<property name="maxPoolSize">1000</property>
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property>
<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>
@Test
public void testc3p0() {
Connection connection=null;
PreparedStatement pStatement=null;
try {
ComboPooledDataSource dataSource=new ComboPooledDataSource();
connection=dataSource.getConnection();
String sql="select * from student";
pStatement=connection.prepareStatement(sql);
ResultSet executeQuery = pStatement.executeQuery();
while (executeQuery.next()) {
System.out.println(executeQuery.getString("name"));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
JDBCUtil.release(connection, pStatement);
}
}
DBUtil

@Test
public void testdbutil() throws SQLException {
ComboPooledDataSource dataSource=new ComboPooledDataSource();
QueryRunner queryRunner=new QueryRunner(dataSource);
Student student= queryRunner.query("SELECT * FROM student WHERE sex=?",new ResultSetHandler<Student>(){
@Override
public Student handle(ResultSet arg0) throws SQLException {
Student student=new Student();
while (arg0.next()) {
String name=arg0.getString("name");
int age=arg0.getInt("age");
int sex=arg0.getInt("sex");
student.setAge(age);
student.setName(name);
student.setSex(sex);
}
return student;
}
}, 2);
System.out.println(student);
Student s1= queryRunner.query("SELECT * FROM student WHERE sex=?",new BeanHandler<Student>(Student.class), 2);
}