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

<p style="text-align: center"><span style="font-size: 16px">mybatis与数据库访问相关的配置以及设计


<p style="text-align: left"><span style="font-size: 16px">mybatis不管如何NB,总是要与数据库进行打交道。通过提问的方式,逐步深入

                                                       

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

      流程上的区别

                                                

     职责上区别

                                  

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

      非池化类:

                      

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

Connection doGetConnection(Properties properties) =

    

    池化类:

    

    池化工作分配  :

                

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

Connection getConnection()

<div class="cnblogs_code">

 (conn == ) { 
            </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;只有有连接还回来,再走这里</span>
    <span style="color: #0000ff"&gt;if</span> (!<span style="color: #000000"&gt;state.idleConnections.isEmpty()) {
      </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; Pool has available connection</span>
      conn = state.idleConnections.remove(0<span style="color: #000000"&gt;);
      </span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; (log.isDebugEnabled()) {
        log.debug(</span>"Checked out connection " + conn.getRealHashCode() + " from pool."<span style="color: #000000"&gt;);
      }
    } </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt; {

    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;有两种可能:1种,都在使用中,池子没满,再新建
      </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; Pool does not have available connection</span>
      <span style="color: #0000ff"&gt;if</span> (state.activeConnections.size() <<span style="color: #000000"&gt; poolMaximumActiveConnections) {
        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; Can create new connection

        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;新建连接</span>
        conn = <span style="color: #0000ff"&gt;new</span> PooledConnection(dataSource.getConnection(),<span style="color: #0000ff"&gt;this</span><span style="color: #000000"&gt;);
        </span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; (log.isDebugEnabled()) {
          log.debug(</span>"Created connection " + conn.getRealHashCode() + "."<span style="color: #000000"&gt;);
        }
      } </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt; {
        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; Cannot create new connection

        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;找一个最老的,用的ArrayList,老的在ArrayList数组的前面</span>
        PooledConnection oldestActiveConnection = state.activeConnections.get(0<span style="color: #000000"&gt;);
        </span><span style="color: #0000ff"&gt;long</span> longestCheckoutTime =<span style="color: #000000"&gt; oldestActiveConnection.getCheckoutTime();

        </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;借出超时,不让他做了,直接rollback。。。暴力</span>
        <span style="color: #0000ff"&gt;if</span> (longestCheckoutTime ><span style="color: #000000"&gt; poolMaximumCheckoutTime) {
          </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; Can claim overdue connection</span>
          state.claimedOverdueConnectionCount++<span style="color: #000000"&gt;;
          state.accumulatedCheckoutTimeOfOverdueConnections </span>+=<span style="color: #000000"&gt; longestCheckoutTime;
          state.accumulatedCheckoutTime </span>+=<span style="color: #000000"&gt; longestCheckoutTime;
          state.activeConnections.remove(oldestActiveConnection);
          </span><span style="color: #0000ff"&gt;if</span> (!<span style="color: #000000"&gt;oldestActiveConnection.getRealConnection().getAutoCommit()) {
            </span><span style="color: #0000ff"&gt;try</span><span style="color: #000000"&gt; {
              oldestActiveConnection.getRealConnection().rollback();
            } </span><span style="color: #0000ff"&gt;catch</span><span style="color: #000000"&gt; (SQLException e) {
              </span><span style="color: #008000"&gt;/*</span><span style="color: #008000"&gt;
                 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.
               </span><span style="color: #008000"&gt;*/</span><span style="color: #000000"&gt;
              log.debug(</span>"Bad connection. Could not roll back"<span style="color: #000000"&gt;);
            }  
          }

          </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;拿回来后,不再放到原有的PooledConnection,新建立一个。从新开始.老的REAL connection还被oldestActiveConnection引用,不会内存溢出?</span>
          conn = <span style="color: #0000ff"&gt;new</span> PooledConnection(oldestActiveConnection.getRealConnection(),<span style="color: #0000ff"&gt;this</span><span style="color: #000000"&gt;);
          conn.setCreatedTimestamp(oldestActiveConnection.getCreatedTimestamp());
          conn.setLastUsedTimestamp(oldestActiveConnection.getLastUsedTimestamp());

          oldestActiveConnection.invalidate();
          </span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; (log.isDebugEnabled()) {
            log.debug(</span>"Claimed overdue connection " + conn.getRealHashCode() + "."<span style="color: #000000"&gt;);
          }
        } </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt; {
          </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; Must wait

            </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;大家都在用着,你只能等着了。</span>
          <span style="color: #0000ff"&gt;try</span><span style="color: #000000"&gt; {
            </span><span style="color: #0000ff"&gt;if</span> (!<span style="color: #000000"&gt;countedWait) {
              state.hadToWaitCount</span>++<span style="color: #000000"&gt;;
              countedWait </span>= <span style="color: #0000ff"&gt;true</span><span style="color: #000000"&gt;;
            }
            </span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; (log.isDebugEnabled()) {
              log.debug(</span>"Waiting as long as " + poolTimeToWait + " milliseconds for connection."<span style="color: #000000"&gt;);
            }
            </span><span style="color: #0000ff"&gt;long</span> wt =<span style="color: #000000"&gt; System.currentTimeMillis();
            state.wait(poolTimeToWait);
            state.accumulatedWaitTime </span>+= System.currentTimeMillis() -<span style="color: #000000"&gt; wt;
          } </span><span style="color: #0000ff"&gt;catch</span><span style="color: #000000"&gt; (InterruptedException e) {
            </span><span style="color: #0000ff"&gt;break</span><span style="color: #000000"&gt;;
          }
        }
      }
    }
    </span><span style="color: #0000ff"&gt;if</span> (conn != <span style="color: #0000ff"&gt;null</span><span style="color: #000000"&gt;) {
      </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt; ping to server and check the connection is valid or not</span>
      <span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; (conn.isValid()) {
        </span><span style="color: #0000ff"&gt;if</span> (!<span style="color: #000000"&gt;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</span>++<span style="color: #000000"&gt;;
        state.accumulatedRequestTime </span>+= System.currentTimeMillis() -<span style="color: #000000"&gt; t;
      } </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt; {
        </span><span style="color: #0000ff"&gt;if</span><span style="color: #000000"&gt; (log.isDebugEnabled()) {
          log.debug(</span>"A bad connection (" + conn.getRealHashCode() + ") was returned from the pool,getting another connection."<span style="color: #000000"&gt;);
        }
        state.badConnectionCount</span>++<span style="color: #000000"&gt;;
        localBadConnectionCount</span>++<span style="color: #000000"&gt;;
        conn </span>= <span style="color: #0000ff"&gt;null</span><span style="color: #000000"&gt;;

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

}</span></pre>

<p style="text-align: left">        


<p style="text-align: left">    

  

相关文章

1.pom.xml引入依赖 &lt;dependency&gt; &lt;gro...
&lt;?xml version=&quot;1.0&quot; encoding=&a...
准备工作 ① 创建数据库&amp;数据表 ## 创建数据库 CREA...
MyBatis逆向工程是指根据数据库表结构自动生成对应的实体类、...
MyBatis获取参数值的两种方式:${}和#{} ${}的本质就是字符串...
resultMap作用是处理数据表中字段与java实体类中属性的映射关...