mybatis与数据库访问相关的配置以及设计

Posted marios

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis与数据库访问相关的配置以及设计相关的知识,希望对你有一定的参考价值。

mybatis与数据库访问相关的配置以及设计

mybatis不管如何NB,总是要与数据库进行打交道。通过提问的方式,逐步深入

  • 我们常用的MyBatis配置中哪些是与数据库相关?
  1. 数据源配置:
1         <environment id="development">
2             <transactionManager type="JDBC"/>
3             <dataSource type="POOLED">
4                 <property name="driver" value="${driver}"/>
5                 <property name="url" value="${url}"/>
6                 <property name="username" value="${username}"/>
7                 <property name="password" value="${password}"/>
8             </dataSource>
9         </environment>

看到这个配置文件,第一个直觉会想到由谁读取配置文件,谁有读取了配置信息?先忽略这个疑问,跳过。直接看下面的问题

  1.配置数据源信息后,由谁来创建、管理数据源?

    根据JDBC驱动中约束的接口,Connection需要DataSource中获取

    如果自己设计,是否直接可以由工厂返回Connection?有什么好处,有什么坏处? //TODO

         没看代码前:

                            技术分享图片

        实际Mybatis设计,没有直接返回Connection,而返回了dataSource

                                                       技术分享图片

    2.对于有连接池的数据源,和无连接池的数据源,我们自己会如何设计?

      流程上的区别

                                                技术分享图片

     职责上区别

 

                                  技术分享图片

    现在解开谜底:看实际Mybatis设计如何?

      非池化类:

 

                      技术分享图片

看下最关键的,获得数据库连接,和我们自己写的没啥区别。简单粗暴

  private Connection doGetConnection(Properties properties) throws SQLException {
    initializeDriver();
    Connection connection = DriverManager.getConnection(url, properties);
    configureConnection(connection);
    return connection;
  }

    

    池化类:

    技术分享图片

 

    池化工作分配  :

  
              技术分享图片

 

 

  至此,MYBABTIS对于数据源的创建以及管理结束!看下代码,池化获得连接的代码

  @Override
  public Connection getConnection() throws SQLException {
    return popConnection(dataSource.getUsername(), dataSource.getPassword()).getProxyConnection();
  }

 

 

while (conn == null) { //够用就行,拿到一个就返回
      synchronized (state) {

          //只有有连接还回来,再走这里
        if (!state.idleConnections.isEmpty()) {
          // Pool has available connection
          conn = state.idleConnections.remove(0);
          if (log.isDebugEnabled()) {
            log.debug("Checked out connection " + conn.getRealHashCode() + " from pool.");
          }
        } else {

        //有两种可能:1种,都在使用中,池子没满,再新建
          // Pool does not have available connection
          if (state.activeConnections.size() < poolMaximumActiveConnections) {
            // Can create new connection

            //新建连接
            conn = new PooledConnection(dataSource.getConnection(), this);
            if (log.isDebugEnabled()) {
              log.debug("Created connection " + conn.getRealHashCode() + ".");
            }
          } else {
            // Cannot create new connection

            //找一个最老的,用的ArrayList,老的在ArrayList数组的前面
            PooledConnection oldestActiveConnection = state.activeConnections.get(0);
            long longestCheckoutTime = oldestActiveConnection.getCheckoutTime();

            //借出超时,不让他做了,直接rollback。。。暴力
            if (longestCheckoutTime > poolMaximumCheckoutTime) {
              // Can claim overdue connection
              state.claimedOverdueConnectionCount++;
              state.accumulatedCheckoutTimeOfOverdueConnections += longestCheckoutTime;
              state.accumulatedCheckoutTime += longestCheckoutTime;
              state.activeConnections.remove(oldestActiveConnection);
              if (!oldestActiveConnection.getRealConnection().getAutoCommit()) {
                try {
                  oldestActiveConnection.getRealConnection().rollback();
                } catch (SQLException e) {
                  /*
                     Just log a message for debug and continue to execute the following
                     statement like nothing happend.
                     Wrap the bad connection with a new PooledConnection, this will help
                     to not intterupt current executing thread and give current thread a
                     chance to join the next competion for another valid/good database
                     connection. At the end of this loop, bad {@link @conn} will be set as null.
                   */
                  log.debug("Bad connection. Could not roll back");
                }  
              }

              //拿回来后,不再放到原有的PooledConnection,新建立一个。从新开始.老的REAL connection还被oldestActiveConnection引用,不会内存溢出?
              conn = new PooledConnection(oldestActiveConnection.getRealConnection(), this);
              conn.setCreatedTimestamp(oldestActiveConnection.getCreatedTimestamp());
              conn.setLastUsedTimestamp(oldestActiveConnection.getLastUsedTimestamp());
              
              oldestActiveConnection.invalidate();
              if (log.isDebugEnabled()) {
                log.debug("Claimed overdue connection " + conn.getRealHashCode() + ".");
              }
            } else {
              // Must wait

                //大家都在用着,你只能等着了。
              try {
                if (!countedWait) {
                  state.hadToWaitCount++;
                  countedWait = true;
                }
                if (log.isDebugEnabled()) {
                  log.debug("Waiting as long as " + poolTimeToWait + " milliseconds for connection.");
                }
                long wt = System.currentTimeMillis();
                state.wait(poolTimeToWait);
                state.accumulatedWaitTime += System.currentTimeMillis() - wt;
              } catch (InterruptedException e) {
                break;
              }
            }
          }
        }
        if (conn != null) {
          // ping to server and check the connection is valid or not
          if (conn.isValid()) {
            if (!conn.getRealConnection().getAutoCommit()) {
              conn.getRealConnection().rollback();
            }
            conn.setConnectionTypeCode(assembleConnectionTypeCode(dataSource.getUrl(), username, password));
            conn.setCheckoutTimestamp(System.currentTimeMillis());
            conn.setLastUsedTimestamp(System.currentTimeMillis());
            state.activeConnections.add(conn);
            state.requestCount++;
            state.accumulatedRequestTime += System.currentTimeMillis() - t;
          } else {
            if (log.isDebugEnabled()) {
              log.debug("A bad connection (" + conn.getRealHashCode() + ") was returned from the pool, getting another connection.");
            }
            state.badConnectionCount++;
            localBadConnectionCount++;
            conn = null;

            //如果累计有这些个链接失效了,则报个异常.
            if (localBadConnectionCount > (poolMaximumIdleConnections + poolMaximumLocalBadConnectionTolerance)) {
              if (log.isDebugEnabled()) {
                log.debug("PooledDataSource: Could not get a good connection to the database.");
              }
              throw new SQLException("PooledDataSource: Could not get a good connection to the database.");
            }
          }
        }
      }

    }

 

 

 

 

 

        

    

  

 


以上是关于mybatis与数据库访问相关的配置以及设计的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis与Spring整合

MyBatis的相关api以及配置解析

MyBatis框架及ORM

MyBatis与Spring的整合

SpringBoot数据访问 SpringBoot整合Mybatis

阶段一-01.万丈高楼,地基首要-第2章 单体架构设计与准备工作-2-14 数据层HikariCP与MyBatis整合