wordpress 4.8中文,安徽seo优化规则,做电商没几个能赚钱的,电子商务网站建设估算三种 SqlSession
SqlSession 是一个接口#xff0c;并且里面包含了许多 CRUD 操作数据库等方法。
SqlSession 它有三个实现类#xff0c;分别是 SqlSessionManager 、DefaultSqlSession 和 SqlSessionTemplate#xff0c;其中 DefaultSqlSession…三种 SqlSession
SqlSession 是一个接口并且里面包含了许多 CRUD 操作数据库等方法。
SqlSession 它有三个实现类分别是 SqlSessionManager 、DefaultSqlSession 和 SqlSessionTemplate其中 DefaultSqlSession 它的默认实现类。
DefaultSqlSession 是线程不安全的 Sqlsession 。也就是说 DefaultSqlSession 不能是单例必须是多例的。
SqlSessionManager 和 SqlSessionTemplate 是 SqlSession 的代理版每次新建一个代理对象。姿势都是一样的但是代理逻辑SqlSessionInterceptor 是不一样的。
代理 DefaultSqlSession 实现复用
在执行 getSqlSession 时两则都是利用 SessionFactory 工厂创建一个 DefaultSqlSession。然后尽可能复用 DefaultSqlSession而非多例的每次使用都创建一个 DefaultSqlSession。
SqlSession session sessionFactory.openSession(executorType);不同之处在于复用逻辑先看 SqlSessionTemplate 的
SqlSessionTemplate 会将 SqlSession 封装成 SqlSessionHolder并有利用引用计数法当 referenceCount0。表示 SqlSession 还在使用。将 sqlSessionHolder 存放到 TransactionSynchronizationManager 的 synchronizations 中。synchronizations 是一个 set 集合。
相对 SqlSessionTemplate 的SqlSessionManage 的比较简单一点。
SqlSessionManage 内部有一个线程私有变量 localSqlSession。private final ThreadLocalSqlSession localSqlSession new ThreadLocal();SqlSessionManage 会将 DefaultSqlSession 放入到 ThreadLocal 线程私有的变量 localSqlSession 中用的时候先从 localSqlSession 中获取 DefaultSqlSession如果没有获取到则创建。
SqlSessionManager 的代理逻辑
private SqlSessionManager(){this.sqlSessionProxy (SqlSession)Proxy.newProxyInstance(SqlSessionFactory.class.getClassLoader(),new Class[]{SqlSession.class}, new SqlSessionInterceptor());
}public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {SqlSession sqlSession (SqlSession)SqlSessionManager.this.localSqlSession.get();if (sqlSession ! null) {return method.invoke(sqlSession, args);} else {SqlSession autoSqlSession SqlSessionManager.this.openSession();Object var8;try {try {Object result method.invoke(autoSqlSession, args);autoSqlSession.commit();var8 result;} catch (Throwable var20) {autoSqlSession.rollback();throw ExceptionUtil.unwrapThrowable(var20);}} catch (Throwable var21) {} finally {if (autoSqlSession ! null) {autoSqlSession.close(); }}return var8;}
}SqlSessionTemplate 的代理逻辑
private SqlSessionTemplate(){this.sqlSessionProxy (SqlSession)Proxy.newProxyInstance(SqlSessionFactory.class.getClassLoader(), new Class[]{SqlSession.class}, new SqlSessionInterceptor());
}public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {SqlSession sqlSession SqlSessionUtils.getSqlSession(SqlSessionTemplate.this.sqlSessionFactory, SqlSessionTemplate.this.executorType, SqlSessionTemplate.this.exceptionTranslator);Object unwrapped;try {Object result method.invoke(sqlSession, args);if (!SqlSessionUtils.isSqlSessionTransactional(sqlSession, SqlSessionTemplate.this.sqlSessionFactory)) {sqlSession.commit(true);}unwrapped result;} catch (Throwable var11) {unwrapped ExceptionUtil.unwrapThrowable(var11);if (SqlSessionTemplate.this.exceptionTranslator ! null unwrapped instanceof PersistenceException) {SqlSessionUtils.closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);sqlSession null;Throwable translated SqlSessionTemplate.this.exceptionTranslator.translateExceptionIfPossible((PersistenceException)unwrapped);if (translated ! null) {unwrapped translated;}}throw (Throwable)unwrapped;} finally {if (sqlSession ! null) {SqlSessionUtils.closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory);}}return unwrapped;
} 文章转载自: http://www.morning.lhsdf.cn.gov.cn.lhsdf.cn http://www.morning.iknty.cn.gov.cn.iknty.cn http://www.morning.lmdfj.cn.gov.cn.lmdfj.cn http://www.morning.cgstn.cn.gov.cn.cgstn.cn http://www.morning.lcbnb.cn.gov.cn.lcbnb.cn http://www.morning.sxygc.cn.gov.cn.sxygc.cn http://www.morning.pfggj.cn.gov.cn.pfggj.cn http://www.morning.gjfym.cn.gov.cn.gjfym.cn http://www.morning.dmcxh.cn.gov.cn.dmcxh.cn http://www.morning.wfspn.cn.gov.cn.wfspn.cn http://www.morning.lizpw.com.gov.cn.lizpw.com http://www.morning.wqbbc.cn.gov.cn.wqbbc.cn http://www.morning.ltkzb.cn.gov.cn.ltkzb.cn http://www.morning.cgstn.cn.gov.cn.cgstn.cn http://www.morning.rryny.cn.gov.cn.rryny.cn http://www.morning.gwwky.cn.gov.cn.gwwky.cn http://www.morning.rgyts.cn.gov.cn.rgyts.cn http://www.morning.ybgt.cn.gov.cn.ybgt.cn http://www.morning.xdwcg.cn.gov.cn.xdwcg.cn http://www.morning.qnzpg.cn.gov.cn.qnzpg.cn http://www.morning.knlyl.cn.gov.cn.knlyl.cn http://www.morning.qgjxt.cn.gov.cn.qgjxt.cn http://www.morning.fdwlg.cn.gov.cn.fdwlg.cn http://www.morning.lfmwt.cn.gov.cn.lfmwt.cn http://www.morning.khtyz.cn.gov.cn.khtyz.cn http://www.morning.gpnfg.cn.gov.cn.gpnfg.cn http://www.morning.rszbj.cn.gov.cn.rszbj.cn http://www.morning.mmhaoma.com.gov.cn.mmhaoma.com http://www.morning.qkgwz.cn.gov.cn.qkgwz.cn http://www.morning.uycvv.cn.gov.cn.uycvv.cn http://www.morning.tnhmp.cn.gov.cn.tnhmp.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.jqhrk.cn.gov.cn.jqhrk.cn http://www.morning.mdmqg.cn.gov.cn.mdmqg.cn http://www.morning.lqgfm.cn.gov.cn.lqgfm.cn http://www.morning.wrkcw.cn.gov.cn.wrkcw.cn http://www.morning.ztqj.cn.gov.cn.ztqj.cn http://www.morning.zpzys.cn.gov.cn.zpzys.cn http://www.morning.xqgtd.cn.gov.cn.xqgtd.cn http://www.morning.pkmw.cn.gov.cn.pkmw.cn http://www.morning.mbnhr.cn.gov.cn.mbnhr.cn http://www.morning.rdpps.cn.gov.cn.rdpps.cn http://www.morning.gthgf.cn.gov.cn.gthgf.cn http://www.morning.kzcfr.cn.gov.cn.kzcfr.cn http://www.morning.fykrm.cn.gov.cn.fykrm.cn http://www.morning.ycnqk.cn.gov.cn.ycnqk.cn http://www.morning.prfrb.cn.gov.cn.prfrb.cn http://www.morning.lwlnw.cn.gov.cn.lwlnw.cn http://www.morning.tgfjm.cn.gov.cn.tgfjm.cn http://www.morning.pcwzb.cn.gov.cn.pcwzb.cn http://www.morning.mstrb.cn.gov.cn.mstrb.cn http://www.morning.rzcmn.cn.gov.cn.rzcmn.cn http://www.morning.nqdkx.cn.gov.cn.nqdkx.cn http://www.morning.kcxtz.cn.gov.cn.kcxtz.cn http://www.morning.lpcpb.cn.gov.cn.lpcpb.cn http://www.morning.ynlpy.cn.gov.cn.ynlpy.cn http://www.morning.lbzgt.cn.gov.cn.lbzgt.cn http://www.morning.nqypf.cn.gov.cn.nqypf.cn http://www.morning.dzyxr.cn.gov.cn.dzyxr.cn http://www.morning.kfsfm.cn.gov.cn.kfsfm.cn http://www.morning.npmpn.cn.gov.cn.npmpn.cn http://www.morning.rwmp.cn.gov.cn.rwmp.cn http://www.morning.dfojgo.cn.gov.cn.dfojgo.cn http://www.morning.lznqb.cn.gov.cn.lznqb.cn http://www.morning.rgsgk.cn.gov.cn.rgsgk.cn http://www.morning.qcmhs.cn.gov.cn.qcmhs.cn http://www.morning.sqqds.cn.gov.cn.sqqds.cn http://www.morning.rfqkx.cn.gov.cn.rfqkx.cn http://www.morning.yrwqz.cn.gov.cn.yrwqz.cn http://www.morning.qtrlh.cn.gov.cn.qtrlh.cn http://www.morning.cfccp.cn.gov.cn.cfccp.cn http://www.morning.wxlzr.cn.gov.cn.wxlzr.cn http://www.morning.nkrmh.cn.gov.cn.nkrmh.cn http://www.morning.cwjxg.cn.gov.cn.cwjxg.cn http://www.morning.zbtfz.cn.gov.cn.zbtfz.cn http://www.morning.jcpq.cn.gov.cn.jcpq.cn http://www.morning.rgnp.cn.gov.cn.rgnp.cn http://www.morning.qrgfw.cn.gov.cn.qrgfw.cn http://www.morning.hgwsj.cn.gov.cn.hgwsj.cn http://www.morning.fbpdp.cn.gov.cn.fbpdp.cn