Mybatis   发布时间:2019-10-08  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了mybatis与数据库访问相关的配置以及设计大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

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


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

transactionManager source {driver}" {url}" {usernamE}" password"{passworD}" source

source中获取

看代码前:

                           

title="mybatis与数据库访问相关的配置以及设计" alt="mybatis与数据库访问相关的配置以及设计" src="https://cn.js-code.com/res/2019/02-07/10/c1a51ba04392a5f2f63cf4aed949de31.png" >

source

                                                       

title="mybatis与数据库访问相关的配置以及设计" alt="mybatis与数据库访问相关的配置以及设计" src="https://cn.js-code.com/res/2019/02-07/10/4017d3e69004596f4e6ce7cac05f7fad.png" >

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

      流程上的区别

                                                

title="mybatis与数据库访问相关的配置以及设计" alt="mybatis与数据库访问相关的配置以及设计" src="https://cn.js-code.com/res/2019/02-07/10/a492ecdbb0abf6a1df3381517883459f.png" >

     职责上区别

                                  

title="mybatis与数据库访问相关的配置以及设计" alt="mybatis与数据库访问相关的配置以及设计" src="https://cn.js-code.com/res/2019/02-07/10/86f9bd56b88f493bd4bdcbbefe52c5e4.png" >

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

      非池化类:

                      

title="mybatis与数据库访问相关的配置以及设计" alt="mybatis与数据库访问相关的配置以及设计" src="https://cn.js-code.com/res/2019/02-07/10/2252575e3b969fd7d41e543ca895772b.png" >

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

Connection doGetConnection(Properties properties) { initializeDriver(); Connection connection =RMANager.getConnection(url,properties); configureConnection(connection);

    

    池化类:

    

title="mybatis与数据库访问相关的配置以及设计" alt="mybatis与数据库访问相关的配置以及设计" src="https://cn.js-code.com/res/2019/02-07/10/010c3f60f5b88bBACcdd73c903cd1959.png" >

    池化工作分配  :

                

title="mybatis与数据库访问相关的配置以及设计" alt="mybatis与数据库访问相关的配置以及设计" src="https://cn.js-code.com/res/2019/02-07/10/654a117f6c7bee160985ad108acf7458.png" >

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

Connection getConnection() { source.getUsername(),datasource.getpassword()).getProxyConnection(); }

<div class="cnblogs_code">

 (conn == ) { 
      E) {
      </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">    

  

大佬总结

以上是大佬教程为你收集整理的mybatis与数据库访问相关的配置以及设计全部内容,希望文章能够帮你解决mybatis与数据库访问相关的配置以及设计所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: