数据库连接池原理

每次请求都要建立一次数据库连接,费时还要分配内存资源,使用完后得断开连接。也不能控制被创建的连接对象数,系统资源分配内存大小无法把控,频繁进行数据库连接会占用很多数据库资源,网站响应速度下降。如果连接过多,可能导致内存泄漏,服务器崩溃。数据库连接池提高了数据库连接的利用率,减小了内存吞吐的开销。

Java应用程序访问数据库过程

1.加载数据库驱动程序
2.建立数据库连接
3.访问数据库,执行sql语句
4.断开数据库连接

数据库连接池原理

数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”,预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完后再放回去。通过设定连接池最大连接数来防止系统无尽的与数据库连接。通过连接池的管理机制监控数据库的连接的数量,使用情况,为系统开发,测试及性能调整提供依据。

数据库连接池远不止这4步,需要考虑的问题很多,这里只是简单说明其原理

1.新建一个类实现DataSource接口
2.在构造器中一次性创建5个连接,将连接保存到LinkedList中
3.从LinkedList中返回一个连接
4.将连接放回连接池中
public class MyDataSource implements DataSource {

	private LinkedList<Connection> dataSources = new LinkedList<Connection>();

	public MyDataSource() {
		for (int i = 0; i < 5; i++) {
			try {
				DriverManager.registerDriver(new Driver());
				Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "666666");
				dataSources.add(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public Connection getConnection() throws SQLException {
		final Connection conn = dataSources.removeFirst();
		return conn;
	}

	public void releaseConnection(Connection conn) {
		dataSources.add(conn);
	}

	public PrintWriter getLogWriter() throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	public void setLogWriter(PrintWriter out) throws SQLException {
		// TODO Auto-generated method stub

	}

	public void setLoginTimeout(int seconds) throws SQLException {
		// TODO Auto-generated method stub

	}

	public int getLoginTimeout() throws SQLException {
		// TODO Auto-generated method stub
		return 0;
	}

	public Logger getParentLogger() throws SQLFeatureNotSupportedException {
		// TODO Auto-generated method stub
		return null;
	}

	public <T> T unwrap(Class<T> iface) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean isWrapperFor(Class<?> iface) throws SQLException {
		// TODO Auto-generated method stub
		return false;
	}

	public Connection getConnection(String username, String password) throws SQLException {
		// TODO Auto-generated method stub
		return null;
	}

}

测试

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {
	
	public static void main(String[] args) throws SQLException {
		FindAllUsers();
	}

	public static void FindAllUsers() throws SQLException {
		MyDataSource dataSource = new MyDataSource();
		Connection conn = null;
		Statement state = null;
		ResultSet result = null;
		try {
			conn = dataSource.getConnection();
			state = conn.createStatement();
			result = state.executeQuery("select * from user_info");
			while (result.next()) {
				System.out.println(result.getString("username"));
			}
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			result.close();
			state.close();
			dataSource.releaseConnection(conn);
		}
	}

}

结果
test数据库中user_info表中数据
在这里插入图片描述
控制台输出
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值