科技报道|一次性搞清楚,Java并发编程在各主流框架中的应用,保证看懂( 四 )
/** * 管理每个线程的资源和事务同步的中心帮助程序 。 供资源管理代码使用 , 但不供典型应用程序代码使用 。* * 资源管理代码应该检查线程绑定的资源 , 如 , JDBC连接 或 Hibernate Sessions 。* 此类代码通常不应该将资源绑定到线程 , 因为这是事务管理器的职责 。 另一个选项是 ,* 如果事务同步处于活动状态 , 则在首次使用时延迟绑定 , 以执行跨任意数量资源的事务 。*/public abstract class TransactionSynchronizationManager {/***一般是一个线程持有一个 独立的事务 , 以相互隔离地处理各自的事务 。*所以这里使用了很多 ThreadLocal对象 , 为每个线程绑定 对应的事务属性及资源 ,*以便后续使用时能直接获取 。*/private static final ThreadLocalThreadLocal 在 Mybatis 中的使用
Mybatis 的 SqlSession 对象 也是各线程私有的资源 , 所以对其的管理也使用到了 ThreadLocal 类 。 源码如下 。
public class SqlSessionManager implements SqlSessionFactory, SqlSession {private final ThreadLocal localSqlSession = new ThreadLocal<>();public void startManagedSession() {this.localSqlSession.set(openSession());}public void startManagedSession(boolean autoCommit) {this.localSqlSession.set(openSession(autoCommit));}public void startManagedSession(Connection connection) {this.localSqlSession.set(openSession(connection));}public void startManagedSession(TransactionIsolationLevel level) {this.localSqlSession.set(openSession(level));}public void startManagedSession(ExecutorType execType) {this.localSqlSession.set(openSession(execType));}public void startManagedSession(ExecutorType execType, boolean autoCommit) {this.localSqlSession.set(openSession(execType, autoCommit));}public void startManagedSession(ExecutorType execType, TransactionIsolationLevel level) {this.localSqlSession.set(openSession(execType, level));}public void startManagedSession(ExecutorType execType, Connection connection) {this.localSqlSession.set(openSession(execType, connection));}public boolean isManagedSessionStarted() {return this.localSqlSession.get() != null;}@Overridepublic Connection getConnection() {final SqlSession sqlSession = localSqlSession.get();if (sqlSession == null) {throw new SqlSessionException("Error:Cannot get connection.No managed session is started.");}return sqlSession.getConnection();}@Overridepublic void clearCache() {final SqlSession sqlSession = localSqlSession.get();if (sqlSession == null) {throw new SqlSessionException("Error:Cannot clear the cache.No managed session is started.");}sqlSession.clearCache();}@Overridepublic void commit() {final SqlSession sqlSession = localSqlSession.get();if (sqlSession == null) {throw new SqlSessionException("Error:Cannot commit.No managed session is started.");}sqlSession.commit();}@Overridepublic void commit(boolean force) {final SqlSession sqlSession = localSqlSession.get();if (sqlSession == null) {throw new SqlSessionException("Error:Cannot commit.No managed session is started.");}sqlSession.commit(force);}@Overridepublic void rollback() {final SqlSession sqlSession = localSqlSession.get();if (sqlSession == null) {throw new SqlSessionException("Error:Cannot rollback.No managed session is started.");}sqlSession.rollback();}@Overridepublic void rollback(boolean force) {final SqlSession sqlSession = localSqlSession.get();if (sqlSession == null) {throw new SqlSessionException("Error:Cannot rollback.No managed session is started.");}sqlSession.rollback(force);}@Overridepublic List flushStatements() {final SqlSession sqlSession = localSqlSession.get();if (sqlSession == null) {throw new SqlSessionException("Error:Cannot rollback.No managed session is started.");}return sqlSession.flushStatements();}@Overridepublic void close() {final SqlSession sqlSession = localSqlSession.get();if (sqlSession == null) {throw new SqlSessionException("Error:Cannot close.No managed session is started.");}try {sqlSession.close();} finally {localSqlSession.set(null);}}}
推荐阅读
- 所持股份|万兴科技:公司控股股东、实际控制人吴太兵质押150万股
- 发布公告|数量过半!博创科技:天通股份累计减持约150万股
- 英雄科技聊数码|蔡崇信有实力买下篮网,那身价3200亿的马云,能买下几支NBA球队
- 光明网|多几个角度看待“集中清退研究生”
- 前沿军事报道|普京4个字回应十分解气,中俄率先突破!美国要求疫苗必须无偿供应
- 科技前沿阵地|涨疯了!海思安防芯片遭哄抬“围剿”
- 月影浓|吴亦凡机械造型走秀 垫肩披风搭银框眼镜科技感足
- 中国历史发展过程|中国历史发展过程.中国的科技史界过去半个多世纪
- 天津|桂发祥:不再持有昆汀科技股份
- 消费|减持!天通股份:减持博创科技约32万股
