Druid源码分析(六)-- 获取连接

10 篇文章 1 订阅

Druid源码分析(六)-- 获取连接

Druid源码分析(六)-- 获取连接

获取连接,执行sql

运行 druid-spring-boot-starter 模块的 com.alibaba.druid.spring.boot.demo.DemoApplication

在 DruidDataSource#getConnection() 打上断点

然后通过postman调用接口
在这里插入图片描述
可以看到,走进程序断点了,userDao.findOne 通过hibernate 去获取连接

// UserServiceImpl.java
public User findById(Long id) {
    return userDao.findOne(id);
}

在这里插入图片描述
获取连接的时候,会再去调一次init 函数,因为之前已经初始化过了,所以会直接return。这就是前几篇文章分析的双重校验+锁起作用的地方,避免了重复初始化。

if (inited) {
    return;
}

然后会根据是否有filter ,执行不通的逻辑,如果没有filter ,就直接获取连接,有的话,会通过责任链的模式执行函数。

我们这里是有监控filter ,所以走的是责任链的模式,和init 的时候类似,最后他还是调用getConnectionDirect()

所以,我们接着看这个函数

public DruidPooledConnection getConnectionDirect(long maxWaitMillis) throws SQLException {
        int notFullTimeoutRetryCnt = 0;
        // 这里用了一个死循环
        for (;;) {
            // handle notFullTimeoutRetry
            DruidPooledConnection poolableConnection;
            try {
                poolableConnection = getConnectionInternal(maxWaitMillis);
                ...

接着往下看getConnectionInternal

private DruidPooledConnection getConnectionInternal(long maxWait) throws SQLException {
   	if (closed) {
   		// 如果数据源关闭了就抛异常
       	connectErrorCountUpdater.incrementAndGet(this);
       	throw new DataSourceClosedException("dataSource already closed at " + new Date(closeTimeMillis));
   }
	...
	// 这里有行代码,这个写法第一次见
   	for (boolean createDirect = false;;) {    
   	// 编译后是这样的
  	while(true) { 
   	...
   	// 最后返回的是DruidPooledConnection ,把DruidConnectionHolder又包装了一层
    DruidPooledConnection poolalbeConnection = new DruidPooledConnection(holder);	    

最后获取完连接,会执行sql,下面是Hibernate 生成的sql

Hibernate: select user0_.id as id1_0_0_, user0_.name as name2_0_0_ from user user0_ where user0_.id=?
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值