大佬教程收集整理的这篇文章主要介绍了mybatis与数据库访问相关的配置以及设计,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<p style="text-align: center"><span style="font-size: 16px">mybatis与数据库访问相关的配置以及设计
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" >
看下最关键的,获得数据库连接,和我们自己写的没啥区别。简单粗暴
池化类:
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对于数据源的创建以及管理结束!看下代码,池化获得连接的代码
<div class="cnblogs_code">
(conn == ) {
E) {
</span><span style="color: #008000">//</span><span style="color: #008000">只有有连接还回来,再走这里</span>
<span style="color: #0000ff">if</span> (!<span style="color: #000000">state.idleConnections.isEmpty()) {
</span><span style="color: #008000">//</span><span style="color: #008000"> Pool has available connection</span>
conn = state.idleConnections.remove(0<span style="color: #000000">);
</span><span style="color: #0000ff">if</span><span style="color: #000000"> (log.isDebugEnabled()) {
log.debug(</span>"checked out connection " + conn.getRealHashCode() + " from pool."<span style="color: #000000">);
}
} </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
</span><span style="color: #008000">//</span><span style="color: #008000">有两种可能:1种,都在使用中,池子没满,再新建
</span><span style="color: #008000">//</span><span style="color: #008000"> Pool does not have available connection</span>
<span style="color: #0000ff">if</span> (state.activeConnections.size() <<span style="color: #000000"> poolMaximumActiveConnections) {
</span><span style="color: #008000">//</span><span style="color: #008000"> Can create new connection
</span><span style="color: #008000">//</span><span style="color: #008000">新建连接</span>
conn = <span style="color: #0000ff">new</span> PooledConnection(datasource.getConnection(),<span style="color: #0000ff">this</span><span style="color: #000000">);
</span><span style="color: #0000ff">if</span><span style="color: #000000"> (log.isDebugEnabled()) {
log.debug(</span>"Created connection " + conn.getRealHashCode() + "."<span style="color: #000000">);
}
} </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
</span><span style="color: #008000">//</span><span style="color: #008000"> CAnnot create new connection
</span><span style="color: #008000">//</span><span style="color: #008000">找一个最老的,用的ArrayList,老的在ArrayList数组的前面</span>
PooledConnection oldestActiveConnection = state.activeConnections.get(0<span style="color: #000000">);
</span><span style="color: #0000ff">long</span> longestcheckoutTime =<span style="color: #000000"> oldestActiveConnection.getcheckoutTime();
</span><span style="color: #008000">//</span><span style="color: #008000">借出超时,不让他做了,直接rollBACk。。。暴力</span>
<span style="color: #0000ff">if</span> (longestcheckoutTime ><span style="color: #000000"> poolMaximumcheckoutTimE) {
</span><span style="color: #008000">//</span><span style="color: #008000"> Can claim overdue connection</span>
state.claimedOverdueConnectionCount++<span style="color: #000000">;
state.accumulatedcheckoutTimeOfOverdueConnections </span>+=<span style="color: #000000"> longestcheckoutTime;
state.accumulatedcheckoutTime </span>+=<span style="color: #000000"> longestcheckoutTime;
state.activeConnections.remove(oldestActiveConnection);
</span><span style="color: #0000ff">if</span> (!<span style="color: #000000">oldestActiveConnection.getRealConnection().getAutoCommit()) {
</span><span style="color: #0000ff">try</span><span style="color: #000000"> {
oldestActiveConnection.getRealConnection().rollBACk();
} </span><span style="color: #0000ff">catch</span><span style="color: #000000"> (SQLException E) {
</span><span style="color: #008000">/*</span><span style="color: #008000">
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">*/</span><span style="color: #000000">
log.debug(</span>"Bad connection. Could not roll BACk"<span style="color: #000000">);
}
}
</span><span style="color: #008000">//</span><span style="color: #008000">拿回来后,不再放到原有的PooledConnection,新建立一个。从新开始.老的REAL connection还被oldestActiveConnection引用,不会内存溢出?</span>
conn = <span style="color: #0000ff">new</span> PooledConnection(oldestActiveConnection.getRealConnection(),<span style="color: #0000ff">this</span><span style="color: #000000">);
conn.setCreatedtimestamp(oldestActiveConnection.getCreatedtimestamp());
conn.setLastusedtimestamp(oldestActiveConnection.getLastusedtimestamp());
oldestActiveConnection.invalidate();
</span><span style="color: #0000ff">if</span><span style="color: #000000"> (log.isDebugEnabled()) {
log.debug(</span>"Claimed overdue connection " + conn.getRealHashCode() + "."<span style="color: #000000">);
}
} </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
</span><span style="color: #008000">//</span><span style="color: #008000"> Must wait
</span><span style="color: #008000">//</span><span style="color: #008000">大家都在用着,你只能等着了。</span>
<span style="color: #0000ff">try</span><span style="color: #000000"> {
</span><span style="color: #0000ff">if</span> (!<span style="color: #000000">countedWait) {
state.hadToWaitCount</span>++<span style="color: #000000">;
countedWait </span>= <span style="color: #0000ff">true</span><span style="color: #000000">;
}
</span><span style="color: #0000ff">if</span><span style="color: #000000"> (log.isDebugEnabled()) {
log.debug(</span>"WaiTing as long as " + poolTimeToWait + " milliseconds for connection."<span style="color: #000000">);
}
</span><span style="color: #0000ff">long</span> wt =<span style="color: #000000"> System.currentTimeMillis();
state.wait(poolTimeToWait);
state.accumulatedWaitTime </span>+= System.currentTimeMillis() -<span style="color: #000000"> wt;
} </span><span style="color: #0000ff">catch</span><span style="color: #000000"> (InterruptedException E) {
</span><span style="color: #0000ff">break</span><span style="color: #000000">;
}
}
}
}
</span><span style="color: #0000ff">if</span> (conn != <span style="color: #0000ff">null</span><span style="color: #000000">) {
</span><span style="color: #008000">//</span><span style="color: #008000"> ping to server and check the connection is valid or not</span>
<span style="color: #0000ff">if</span><span style="color: #000000"> (conn.isValid()) {
</span><span style="color: #0000ff">if</span> (!<span style="color: #000000">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">;
state.accumulatedrequestTime </span>+= System.currentTimeMillis() -<span style="color: #000000"> t;
} </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
</span><span style="color: #0000ff">if</span><span style="color: #000000"> (log.isDebugEnabled()) {
log.debug(</span>"A bad connection (" + conn.getRealHashCode() + ") was returned from the pool,getTing another connection."<span style="color: #000000">);
}
state.badConnectionCount</span>++<span style="color: #000000">;
localBadConnectionCount</span>++<span style="color: #000000">;
conn </span>= <span style="color: #0000ff">null</span><span style="color: #000000">;
</span><span style="color: #008000">//</span><span style="color: #008000">如果累计有这些个链接失效了,则报个异常.</span>
<span style="color: #0000ff">if</span> (localBadConnectionCount > (poolMaximumIdleConnections +<span style="color: #000000"> poolMaximumLocalBadConnectionTolerancE)) {
</span><span style="color: #0000ff">if</span><span style="color: #000000"> (log.isDebugEnabled()) {
log.debug(</span>"PooledDatasource: Could not get a good connection to the database."<span style="color: #000000">);
}
</span><span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> SQLException("PooledDatasource: Could not get a good connection to the database."<span style="color: #000000">);
}
}
}
}
}</span></pre>
<p style="text-align: left">
以上是大佬教程为你收集整理的mybatis与数据库访问相关的配置以及设计全部内容,希望文章能够帮你解决mybatis与数据库访问相关的配置以及设计所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。