您好,登錄后才能下訂單哦!
Spring怎么管理控制mybatis事務(wù)?針對(duì)這個(gè)問(wèn)題,今天小編總結(jié)這篇有關(guān)mybatis事務(wù)管理的文章,希望能幫助更多想解決這個(gè)問(wèn)題的朋友找到更加簡(jiǎn)單易行的辦法。
mapper 文件是怎么解析的, SqlSessionFactory
這個(gè)重要的對(duì)象,是的就是我們經(jīng)常需要配置的:
@Bean @ConditionalOnMissingBean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { // 略 }
這里面做了很多自動(dòng)化的配置,當(dāng)然我們可以通過(guò)重寫(xiě)它來(lái)自定義我們自己的 sqlSessionFactory
,借用一下上篇文章的圖片:
spring 借助 SqlSessionFactoryBean
來(lái)創(chuàng)建 sqlSessionFactory
,這可以視作是一個(gè)典型的建造者模式,來(lái)創(chuàng)建 SqlSessionFactory
。
spring 拿到我們配置的 mapper 路徑去掃描我們 mapper.xml 然后進(jìn)行一個(gè)循環(huán)進(jìn)行解析(上篇文章第二章節(jié):二、SqlSessionFactory 的初始化與 XMLMapperBuilder):
-- 代碼位于 org.mybatis.spring.SqlSessionFactoryBean#buildSqlSessionFactory -- if (this.mapperLocations != null) { if (this.mapperLocations.length == 0) { LOGGER.warn(() -> "Property 'mapperLocations' was specified but matching resources are not found."); } else { for (Resource mapperLocation : this.mapperLocations) { if (mapperLocation == null) { continue; } try { XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(mapperLocation.getInputStream(), targetConfiguration, mapperLocation.toString(), targetConfiguration.getSqlFragments()); xmlMapperBuilder.parse(); } catch (Exception e) { throw new NestedIOException("Failed to parse mapping resource: '" + mapperLocation + "'", e); } finally { ErrorContext.instance().reset(); } LOGGER.debug(() -> "Parsed mapper file: '" + mapperLocation + "'"); } } } else { LOGGER.debug(() -> "Property 'mapperLocations' was not specified."); } -- 代碼位于 org.apache.ibatis.builder.xml.XMLMapperBuilder#parse -- public void parse() { if (!configuration.isResourceLoaded(resource)) { configurationElement(parser.evalNode("/mapper")); // 上篇文章主要說(shuō)的 configuration.addLoadedResource(resource); bindMapperForNamespace();// 創(chuàng)建mapperProxy的工廠對(duì)象 } parsePendingResultMaps(); parsePendingCacheRefs(); parsePendingStatements(); }
configurationElement(parser.evalNode("/mapper"));
里面發(fā)生的故事,實(shí)際上還有后續(xù)的步驟,如果對(duì) mybatis 有所了解的,應(yīng)該知道,mybatis 會(huì)為我們的接口創(chuàng)建一個(gè)叫做 mapperProxy
的代理對(duì)象(劃重點(diǎn)),其實(shí)就是在這后續(xù)的步驟 bindMapperForNamespace();
做的(不盡然,實(shí)際上是創(chuàng)建并綁定了 mapperProxyFactory
)。
不貼太多代碼,bindMapperForNamespace()
方法里核心做的主要就是調(diào)用 configuration.addMapper()
方法
if (boundType != null) { if (!configuration.hasMapper(boundType)) { // Spring may not know the real resource name so we set a flag // to prevent loading again this resource from the mapper interface // look at MapperAnnotationBuilder#loadXmlResource configuration.addLoadedResource("namespace:" + namespace); configuration.addMapper(boundType); } }
這個(gè) boundType
就是我們?cè)?mapper 文件里面指定的 namespace
,比如:
<mapper namespace="com.anur.mybatisdemo.test.TrackerConfigMapper"> XXXXXXXXXXXXXXXXXX 里面寫(xiě)的sql語(yǔ)句,resultMap 等等,略</mapper>
在 configuration.addMapper()
中調(diào)用了 mapperRegistry.addMapper()
,看到 knowMappers
,這個(gè)就是存儲(chǔ)我們生產(chǎn) MapperProxy
的工廠映射 map,我們稍微再講,先繼續(xù)往下看。
public <T> void addMapper(Class<T> type) { if (type.isInterface()) { if (hasMapper(type)) { throw new BindingException("Type " + type + " is already known to the MapperRegistry."); } boolean loadCompleted = false; try { knownMappers.put(type, new MapperProxyFactory<>(type)); // It's important that the type is added before the parser is run // otherwise the binding may automatically be attempted by the // mapper parser. If the type is already known, it won't try. MapperAnnotationBuilder parser = new MapperAnnotationBuilder(config, type); parser.parse(); loadCompleted = true; } finally { if (!loadCompleted) { knownMappers.remove(type); } } } }
看到 MapperAnnotationBuilder#parse()
,parse()
中主要是對(duì)這個(gè)接口里面定義的方法做了 parseStatement
這件事
for (Method method : methods) { try { // issue #237 if (!method.isBridge()) { parseStatement(method); } } catch (IncompleteElementException e) { configuration.addIncompleteMethod(new MethodResolver(this, method)); } }
parseStatement()
就是解析注解語(yǔ)句的地方, 如果說(shuō)我們沒(méi)有寫(xiě) xml,將語(yǔ)句以注解的形式寫(xiě)在方法上,則會(huì)在這里進(jìn)行語(yǔ)句解析。它和我們上篇文章講到的解析xml很像,就是拿到一大堆屬性,比如 resultMap
,keyGenerator
等等,生成一個(gè) MappedStatement
對(duì)象,這里就不贅述了。
void parseStatement(Method method) { Class<?> parameterTypeClass = getParameterType(method); LanguageDriver languageDriver = getLanguageDriver(method); SqlSource sqlSource = getSqlSourceFromAnnotations(method, parameterTypeClass, languageDriver); if (sqlSource != null) { // 解析注解式的 sql 語(yǔ)句,略 } }
我們知道承載 mapperStatement
的是一個(gè) map 映射,通過(guò)我們?cè)谏掀恼轮蟹磸?fù)強(qiáng)調(diào)的 id
來(lái)作為 key,那么重復(fù)添加會(huì)出現(xiàn)什么呢?
答案在這里,mybatis
的這個(gè) map 被重寫(xiě)了,同時(shí)寫(xiě)這兩者的話,會(huì)拋出 ...already contains value for...
的異常
-- 代碼位置 org.apache.ibatis.session.Configuration.StrictMap#put -- @Override @SuppressWarnings("unchecked") public V put(String key, V value) { if (containsKey(key)) { throw new IllegalArgumentException(name + " already contains value for " + key + (conflictMessageProducer == null ? "" : conflictMessageProducer.apply(super.get(key), value))); } if (key.contains(".")) { final String shortKey = getShortName(key); if (super.get(shortKey) == null) { super.put(shortKey, value); } else { super.put(shortKey, (V) new Ambiguity(shortKey)); } } return super.put(key, value); }
剛才在1.1中我們提到了,mapperProxy
,也就是剛才 org.apache.ibatis.binding.MapperRegistry#addMapper
的代碼:knownMappers.put(type, new MapperProxyFactory<>(type));
看到 MapperProxyFactory
的內(nèi)部:
-- 有刪減 --public class MapperProxyFactory<T> { private final Class<T> mapperInterface; private final Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<>(); public MapperProxyFactory(Class<T> mapperInterface) { this.mapperInterface = mapperInterface; } @SuppressWarnings("unchecked") protected T newInstance(MapperProxy<T> mapperProxy) { return (T) Proxy.newProxyInstance(mapperInterface.getClassLoader(), new Class[] { mapperInterface }, mapperProxy); } public T newInstance(SqlSession sqlSession) { final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession, mapperInterface, methodCache); return newInstance(mapperProxy); } }
了解JDK動(dòng)態(tài)代理的小伙伴應(yīng)該很清楚了, newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
意為,為接口創(chuàng)建一個(gè)實(shí)現(xiàn)了 InvocationHandler
的代理對(duì)象。我們?cè)谡{(diào)用接口方法的時(shí)候,實(shí)際上要看代理類(lèi)是如何實(shí)現(xiàn)的。
那么看看 mapperProxy 的內(nèi)部的 invoke
是如何實(shí)現(xiàn)的,這里有三類(lèi)方法,
一種是一些 Object
對(duì)象帶來(lái)的方法,這里不進(jìn)行代理,直接 invoke
,
一種是default方法,一種比較蛋疼的寫(xiě)法,把接口當(dāng)抽象類(lèi)寫(xiě),里面可以放一個(gè)default方法寫(xiě)實(shí)現(xiàn),這種代理了也沒(méi)太大意義
最后一種也就是我們準(zhǔn)備代理的方法, 它會(huì)為每個(gè)非上面兩者的方法,懶加載一個(gè) MapperMethod
對(duì)象,并調(diào)用 MapperMethod#execute
來(lái)執(zhí)行真正的 mybatis 邏輯。
-- 有刪減 --public class MapperProxy<T> implements InvocationHandler, Serializable { public MapperProxy(SqlSession sqlSession, Class<T> mapperInterface, Map<Method, MapperMethod> methodCache) { this.sqlSession = sqlSession; this.mapperInterface = mapperInterface; this.methodCache = methodCache; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { try { if (Object.class.equals(method.getDeclaringClass())) {// 來(lái)自 Object 的方法,比如 toString() return method.invoke(this, args); } else if (method.isDefault()) {// 靜態(tài)方法,我們可以直接忽略 if (privateLookupInMethod == null) { return invokeDefaultMethodJava8(proxy, method, args); } else { return invokeDefaultMethodJava9(proxy, method, args); } } } catch (Throwable t) { throw ExceptionUtil.unwrapThrowable(t); } final MapperMethod mapperMethod = cachedMapperMethod(method); return mapperMethod.execute(sqlSession, args); } private MapperMethod cachedMapperMethod(Method method) { return methodCache.computeIfAbsent(method, k -> new MapperMethod(mapperInterface, method, sqlSession.getConfiguration())); } }
MapperMethod
的邏輯是怎么樣的,也很好猜到,它的構(gòu)造函數(shù)中創(chuàng)建了兩個(gè)對(duì)象,
public class MapperMethod { private final SqlCommand command; private final MethodSignature method; public MapperMethod(Class<?> mapperInterface, Method method, Configuration config) { this.command = new SqlCommand(config, mapperInterface, method); this.method = new MethodSignature(config, mapperInterface, method); }
sqlCommand
sqlCommand
實(shí)際上就是從 configuration
里面把它對(duì)應(yīng)的 MappedStatement
取出來(lái),持有它的唯一 id
和執(zhí)行類(lèi)型。
public static class SqlCommand { private final String name; private final SqlCommandType type; public SqlCommand(Configuration configuration, Class<?> mapperInterface, Method method) { final String methodName = method.getName(); final Class<?> declaringClass = method.getDeclaringClass(); MappedStatement ms = resolveMappedStatement(mapperInterface, methodName, declaringClass, configuration); if (ms == null) { if (method.getAnnotation(Flush.class) != null) { name = null; type = SqlCommandType.FLUSH; } else { throw new BindingException("Invalid bound statement (not found): " + mapperInterface.getName() + "." + methodName); } } else { name = ms.getId(); type = ms.getSqlCommandType(); if (type == SqlCommandType.UNKNOWN) { throw new BindingException("Unknown execution method for: " + name); } } }
MethodSignature MethodSignature
是針對(duì)接口返回值、參數(shù)等值的解析,比如我們的 @Param
注解,就是在 new ParamNameResolver(configuration, method);
里面解析的,比較簡(jiǎn)單,在之前的文章 簡(jiǎn)單概括的mybatis sqlSession 源碼解析 里也提到過(guò),這里就不多說(shuō)了。
public MethodSignature(Configuration configuration, Class<?> mapperInterface, Method method) { Type resolvedReturnType = TypeParameterResolver.resolveReturnType(method, mapperInterface); if (resolvedReturnType instanceof Class<?>) { this.returnType = (Class<?>) resolvedReturnType; } else if (resolvedReturnType instanceof ParameterizedType) { this.returnType = (Class<?>) ((ParameterizedType) resolvedReturnType).getRawType(); } else { this.returnType = method.getReturnType(); } this.returnsVoid = void.class.equals(this.returnType); this.returnsMany = configuration.getObjectFactory().isCollection(this.returnType) || this.returnType.isArray(); this.returnsCursor = Cursor.class.equals(this.returnType); this.returnsOptional = Optional.class.equals(this.returnType); this.mapKey = getMapKey(method); this.returnsMap = this.mapKey != null; this.rowBoundsIndex = getUniqueParamIndex(method, RowBounds.class); this.resultHandlerIndex = getUniqueParamIndex(method, ResultHandler.class); this.paramNameResolver = new ParamNameResolver(configuration, method); }
mapperMethod
就是 sqlSession
與 mappedStatement
的一個(gè)整合。它的執(zhí)行是一個(gè)策略模式:
public Object execute(SqlSession sqlSession, Object[] args) { Object result; switch (command.getType()) { case INSERT: { Object param = method.convertArgsToSqlCommandParam(args); result = rowCountResult(sqlSession.insert(command.getName(), param)); break; } case UPDATE: { Object param = method.convertArgsToSqlCommandParam(args); result = rowCountResult(sqlSession.update(command.getName(), param)); break; } case DELETE: { Object param = method.convertArgsToSqlCommandParam(args); result = rowCountResult(sqlSession.delete(command.getName(), param)); break; } case SELECT: // 略.. }
具體是怎么執(zhí)行的在文章 簡(jiǎn)單概括的mybatis sqlSession 源碼解析 提到過(guò),這里也不過(guò)多贅述。
這里對(duì) MapperProxy
在初始化與調(diào)用過(guò)程中的關(guān)系做一下羅列:
為了避免有小伙伴對(duì) sqlSession
完全沒(méi)有概念,這里將接口代碼貼出,可以看出 sqlSession
是執(zhí)行語(yǔ)句的一個(gè)入口,同時(shí)也提供了事務(wù)的一些操作,實(shí)際上就是如此:
public interface SqlSession extends Closeable { <T> T selectOne(String statement); <T> T selectOne(String statement, Object parameter); <E> List<E> selectList(String statement); <E> List<E> selectList(String statement, Object parameter); <E> List<E> selectList(String statement, Object parameter, RowBounds rowBounds); <K, V> Map<K, V> selectMap(String statement, String mapKey); <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey); <K, V> Map<K, V> selectMap(String statement, Object parameter, String mapKey, RowBounds rowBounds); <T> Cursor<T> selectCursor(String statement); <T> Cursor<T> selectCursor(String statement, Object parameter); <T> Cursor<T> selectCursor(String statement, Object parameter, RowBounds rowBounds); void select(String statement, Object parameter, ResultHandler handler); void select(String statement, ResultHandler handler); void select(String statement, Object parameter, RowBounds rowBounds, ResultHandler handler); int insert(String statement); int insert(String statement, Object parameter); int update(String statement); int update(String statement, Object parameter); int delete(String statement); int delete(String statement, Object parameter); void commit(); void commit(boolean force); void rollback(); void rollback(boolean force); List<BatchResult> flushStatements(); void close(); void clearCache(); Configuration getConfiguration(); <T> T getMapper(Class<T> type); Connection getConnection(); }
首先忘掉 spring 為我們提供的便利,看一下基礎(chǔ)的,脫離了 spring 托管的 mybatis 是怎么進(jìn)行 sql 操作的:
SqlSession sqlSession = sqlSessionFactory.openSession(); TrackerConfigMapper mapper = sqlSession.getMapper(TrackerConfigMapper.class); TrackerConfigDO one = mapper.getOne(1);
SqlSessionFactory
有兩個(gè)子類(lèi)實(shí)現(xiàn):DefaultSqlSessionFactory
和 SqlSessionManager
,SqlSessionManager
使用動(dòng)態(tài)代理 + 靜態(tài)代理對(duì) DefaultSqlSessionFactory
進(jìn)行了代理,不過(guò)不用太在意這個(gè) SqlSessionManager
,后面會(huì)說(shuō)明原因。
上面不管怎么代理,實(shí)際邏輯的執(zhí)行者都是 DefaultSqlSessionFactory
,我們看看它的創(chuàng)建方法,也就是 openSession()
實(shí)際執(zhí)行的方法:
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { Transaction tx = null; try { final Environment environment = configuration.getEnvironment(); final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); final Executor executor = configuration.newExecutor(tx, execType); return new DefaultSqlSession(configuration, executor, autoCommit); } catch (Exception e) { closeTransaction(tx); // may have fetched a connection so lets call close() throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
environment
可用于數(shù)據(jù)源切換,那么提到數(shù)據(jù)源切換,就很容易想到了,連接的相關(guān)信息是這貨維持的。 所以看到我們的代碼: tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit);
, TransactionFactory
有三個(gè)實(shí)現(xiàn),它們分別是 JdbcTransactionFactory
、ManagedTransactionFactory
和 SpringManagedTransactionFactory
。
JdbcTransactionFactory
和 ManagedTransactionFactory
最大的區(qū)別就在于 ManagedTransactionFactory
實(shí)現(xiàn)了空的 commit 與 rollback,源碼中這樣說(shuō)道:付與容器來(lái)管理 transaction
的生命周期,這個(gè)博主不是特別熟悉,因?yàn)闆](méi)這么用過(guò),tomcat、jetty 等容器實(shí)現(xiàn)了對(duì) jdbc 的代理。要注意,不管如何都是使用的 jdbc 這套接口規(guī)范進(jìn)行數(shù)據(jù)庫(kù)操作的。
/** * {@link Transaction} that lets the container manage the full lifecycle of the transaction. * Delays connection retrieval until getConnection() is called. * Ignores all commit or rollback requests. * By default, it closes the connection but can be configured not to do it. * * @author Clinton Begin * * @see ManagedTransactionFactory */
Transaction
是 mybatis 創(chuàng)建的一個(gè)對(duì)象,它實(shí)際上是對(duì) jdbc
connection
對(duì)象的一個(gè)封裝:
-- 代碼位于 org.apache.ibatis.transaction.jdbc.JdbcTransaction -- @Override public Connection getConnection() throws SQLException { if (connection == null) { openConnection(); } return connection; } @Override public void commit() throws SQLException { if (connection != null && !connection.getAutoCommit()) { if (log.isDebugEnabled()) { log.debug("Committing JDBC Connection [" + connection + "]"); } connection.commit(); } } @Override public void rollback() throws SQLException { if (connection != null && !connection.getAutoCommit()) { if (log.isDebugEnabled()) { log.debug("Rolling back JDBC Connection [" + connection + "]"); } connection.rollback(); } }
我們知道 sqlSession 的 四大對(duì)象之一,Executor,負(fù)責(zé)統(tǒng)領(lǐng)全局,從語(yǔ)句獲取(從 mappedStatement),到參數(shù)拼裝(parameterHandler),再到執(zhí)行語(yǔ)句(statementHandler),最后結(jié)果集封裝(resultHandler),都是它負(fù)責(zé)“指揮”的。
我們看到它使用 Transaction
進(jìn)行初始化,另外的一個(gè)參數(shù)是它的類(lèi)型,這里不多說(shuō),REUSE 是帶語(yǔ)句緩存的,和普通的 SimpleExecutor 沒(méi)有特別大的區(qū)別,BATCH 類(lèi)型則是通過(guò) jdbc 提供的批量提交來(lái)對(duì)網(wǎng)絡(luò)請(qǐng)求進(jìn)行優(yōu)化。
public enum ExecutorType { SIMPLE, REUSE, BATCH}
最后將持有 Transaction
的 Executor 置入 SqlSession
,完成一個(gè) SqlSession
對(duì)象的創(chuàng)建。
可以看到,我們的確是一個(gè)SqlSession
對(duì)應(yīng)一個(gè)連接(Transaction
),MapperProxy
這個(gè)業(yè)務(wù)接口的動(dòng)態(tài)代理對(duì)象又持有一個(gè) SqlSession
對(duì)象,那么總不可能一直用同一個(gè)連接吧?
當(dāng)然有疑問(wèn)是好的,而且通過(guò)對(duì) SqlSession 初始化過(guò)程的剖析,我們已經(jīng)完善了我們對(duì) mybatis 的認(rèn)知:
接下來(lái)就是來(lái)打消這個(gè)疑問(wèn),MapperProxy
持有的 sqlSession
和 SqlSessionFactory
創(chuàng)建的這個(gè)有什么關(guān)系?
實(shí)際上答案就在 SqlSessionTemplate
,SqlSessionTemplate
是 spring 對(duì) mybatis SqlSessionFactory
的封裝,同時(shí),它還是 SqlSession
的代理。
SqlSessionTemplate
和 mybatis 提供的 SqlSessionManager
( SqlSessionFactory
的另一個(gè)實(shí)現(xiàn)類(lèi),也是DefaultSqlSessionFactory
的代理類(lèi),可以細(xì)想一下,業(yè)務(wù)是否共用同一個(gè) sqlSession
還要在業(yè)務(wù)里面去傳遞,去控制是不是很麻煩) 是一樣的思路,不過(guò) spring 直接代理了 sqlSession
:
-- 代碼位于 org.mybatis.spring.SqlSessionTemplate -- private final SqlSessionFactory sqlSessionFactory; private final ExecutorType executorType; private final SqlSession sqlSessionProxy; private final PersistenceExceptionTranslator exceptionTranslator; /** * Constructs a Spring managed {@code SqlSession} with the given * {@code SqlSessionFactory} and {@code ExecutorType}. * A custom {@code SQLExceptionTranslator} can be provided as an * argument so any {@code PersistenceException} thrown by MyBatis * can be custom translated to a {@code RuntimeException} * The {@code SQLExceptionTranslator} can also be null and thus no * exception translation will be done and MyBatis exceptions will be * thrown * * @param sqlSessionFactory a factory of SqlSession * @param executorType an executor type on session * @param exceptionTranslator a translator of exception */ public SqlSessionTemplate(SqlSessionFactory sqlSessionFactory, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator) { notNull(sqlSessionFactory, "Property 'sqlSessionFactory' is required"); notNull(executorType, "Property 'executorType' is required"); this.sqlSessionFactory = sqlSessionFactory; this.executorType = executorType; this.exceptionTranslator = exceptionTranslator; this.sqlSessionProxy = (SqlSession) newProxyInstance( SqlSessionFactory.class.getClassLoader(), new Class[] { SqlSession.class }, new SqlSessionInterceptor()); }
還是熟悉的配方,就是 jdk 的動(dòng)態(tài)代理,SqlSessionTemplate
在初始化時(shí)創(chuàng)建了一個(gè) SqlSession
代理,也內(nèi)置了 ExecutorType
,SqlSessionFactory
等 defaultSqlSession
初始化的必要組件。
想必看到這里,已經(jīng)有很多小伙伴知道這里是怎么回事了,是的,我們對(duì) SqlSession
的操作都是經(jīng)由這個(gè)代理來(lái)完成,代理的內(nèi)部,實(shí)現(xiàn)了真正 SqlSession
的創(chuàng)建與銷(xiāo)毀,回滾與提交等,我們先縱覽以下它的代理實(shí)現(xiàn)。
對(duì)于這種jdk動(dòng)態(tài)代理,我們看到 SqlSessionInterceptor#invoke
方法就明了了。我們先過(guò)一遍常規(guī)的流程,也就是沒(méi)有使用 spring 事務(wù)功能支持,執(zhí)行完 sql 就直接提交事務(wù)的常規(guī)操作:
1、getSqlSession()
創(chuàng)建 sqlSession
2、執(zhí)行 MapperProxy
,也就是前面講了一大堆的,MapperProxy
中,通過(guò) MapperMethod
來(lái)調(diào)用 sqlSession
和我們生成好的 mappedStatement
操作 sql 語(yǔ)句。
3、提交事務(wù)
4、關(guān)閉事務(wù)
注:代碼有很大刪減
private class SqlSessionInterceptor implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { SqlSession sqlSession = getSqlSession( SqlSessionTemplate.this.sqlSessionFactory, SqlSessionTemplate.this.executorType, SqlSessionTemplate.this.exceptionTranslator); // 創(chuàng)建或者獲取真正需要的 SqlSession try { Object result = method.invoke(sqlSession, args); // 執(zhí)行原本想對(duì) SqlSession 做的事情 if (!isSqlSessionTransactional(sqlSession, SqlSessionTemplate.this.sqlSessionFactory)) { // force commit even on non-dirty sessions because some databases require // a commit/rollback before calling close() sqlSession.commit(true);// 如非 spring 管理事務(wù),則直接提交 } finally { if (sqlSession != null) { closeSqlSession(sqlSession, SqlSessionTemplate.this.sqlSessionFactory); } } } }
注意:注釋掉的代碼在此類(lèi)型的操作中沒(méi)有什么意義,getSqlSession()
在這里只是簡(jiǎn)單通過(guò) sessionFactory
創(chuàng)建了一個(gè) sqlSession
:
public static SqlSession getSqlSession(SqlSessionFactory sessionFactory, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator) { // SqlSessionHolder holder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sessionFactory); // SqlSession session = sessionHolder(executorType, holder); // if (session != null) { // return session; // } LOGGER.debug(() -> "Creating a new SqlSession"); session = sessionFactory.openSession(executorType); // registerSessionHolder(sessionFactory, executorType, exceptionTranslator, session); return session; }
看完前面的實(shí)現(xiàn),有小伙伴會(huì)好奇,我的 @Transactional 注解呢?我的事務(wù)傳播等級(jí)呢?
實(shí)際上,除去上述常規(guī)流程,更多的是要借助 TransactionSynchronizationManager
這個(gè)對(duì)象來(lái)完成,比如剛才步驟一,getSqlSession()
我暫時(shí)注釋掉的代碼里面,有一個(gè)很重要的操作:
我們把剛才 getSqlSession()
中注釋掉的代碼再拿回來(lái)看看:
SqlSessionHolder holder = (SqlSessionHolder) TransactionSynchronizationManager.getResource(sessionFactory); SqlSession session = sessionHolder(executorType, holder); if (session != null) { return session; } session = sessionFactory.openSession(executorType); registerSessionHolder(sessionFactory, executorType, exceptionTranslator, session); return session;
我們可以看到首先獲取一個(gè)叫做 SqlSessionHolder
的東西,如果里面沒(méi)有 sqlSession
則調(diào)用 sessionFactory.openSession(executorType);
創(chuàng)建一個(gè),并把它注冊(cè)到 TransactionSynchronizationManager。
sqlSessionHolder 沒(méi)什么可說(shuō)的,它就只是個(gè)純粹的容器,里面主要就是裝著一個(gè) SqlSession
:
public SqlSessionHolder(SqlSession sqlSession, ExecutorType executorType, PersistenceExceptionTranslator exceptionTranslator) { notNull(sqlSession, "SqlSession must not be null"); notNull(executorType, "ExecutorType must not be null"); this.sqlSession = sqlSession; this.executorType = executorType; this.exceptionTranslator = exceptionTranslator; }
所以說(shuō)我們只需要把目光焦距在 TransactionSynchronizationManager
就可以了,它的內(nèi)部持有了很多個(gè)元素為 Map<Object, Object>
的 ThreadLocal
(代碼示例中只貼出了 resources
這一個(gè) ThreadLocal
):
public abstract class TransactionSynchronizationManager { private static final Log logger = LogFactory.getLog(TransactionSynchronizationManager.class); private static final ThreadLocal<Map<Object, Object>> resources = new NamedThreadLocal<>("Transactional resources"); @Nullable public static Object getResource(Object key) { Object actualKey = TransactionSynchronizationUtils.unwrapResourceIfNecessary(key); Object value = doGetResource(actualKey); if (value != null && logger.isTraceEnabled()) { logger.trace("Retrieved value [" + value + "] for key [" + actualKey + "] bound to thread [" + Thread.currentThread().getName() + "]"); } return value; } @Nullable private static Object doGetResource(Object actualKey) { Map<Object, Object> map = resources.get(); if (map == null) { return null; } Object value = map.get(actualKey); // Transparently remove ResourceHolder that was marked as void... if (value instanceof ResourceHolder && ((ResourceHolder) value).isVoid()) { map.remove(actualKey); // Remove entire ThreadLocal if empty... if (map.isEmpty()) { resources.remove(); } value = null; } return value; }
也就是說(shuō),spring 的事務(wù),是借助 TransactionSynchronizationManager
+ SqlSessionHolder
對(duì) sqlSession
的控制來(lái)實(shí)現(xiàn)的。
那么這樣就很清晰了,如下總結(jié),也如下圖:
MapperProxy
內(nèi)置的 sqlSession
是 sqlSessiontemplate
sqlSessiontemplate
通過(guò)持有 SqlSessionFactory
來(lái)創(chuàng)建真正的 SqlSession
TransactionSynchronizationManager
+ SqlSessionHolder
則扮演著 SqlSession
管理的角色
上一個(gè)小節(jié)只是講了是什么,沒(méi)有講為什么,到了這里如果有好奇寶寶一定會(huì)好奇諸如 spring 的一系列事務(wù)控制是怎么實(shí)現(xiàn)的,當(dāng)然本文不會(huì)講太多 spring 事務(wù)管理相關(guān)的太多東西,以后會(huì)有后續(xù)文章專(zhuān)門(mén)剖析事務(wù)管理。
我們可以簡(jiǎn)單看下 TransactionInterceptor
,這是 @Transactional
注解的代理類(lèi)。
/** * AOP Alliance MethodInterceptor for declarative transaction * management using the common Spring transaction infrastructure * ({@link org.springframework.transaction.PlatformTransactionManager}/ * {@link org.springframework.transaction.ReactiveTransactionManager}). * * <p>Derives from the {@link TransactionAspectSupport} class which * contains the integration with Spring's underlying transaction API. * TransactionInterceptor simply calls the relevant superclass methods * such as {@link #invokeWithinTransaction} in the correct order. * * <p>TransactionInterceptors are thread-safe. * * @author Rod Johnson * @author Juergen Hoeller * @see TransactionProxyFactoryBean * @see org.springframework.aop.framework.ProxyFactoryBean * @see org.springframework.aop.framework.ProxyFactory */@SuppressWarnings("serial")public class TransactionInterceptor extends TransactionAspectSupport implements MethodInterceptor, Serializable { /** * Create a new TransactionInterceptor. * <p>Transaction manager and transaction attributes still need to be set. * @see #setTransactionManager * @see #setTransactionAttributes(java.util.Properties) * @see #setTransactionAttributeSource(TransactionAttributeSource) */ public TransactionInterceptor() { } @Override @Nullable public Object invoke(MethodInvocation invocation) throws Throwable { // Work out the target class: may be {@code null}. // The TransactionAttributeSource should be passed the target class // as well as the method, which may be from an interface. Class<?> targetClass = (invocation.getThis() != null ? AopUtils.getTargetClass(invocation.getThis()) : null); // Adapt to TransactionAspectSupport's invokeWithinTransaction... return invokeWithinTransaction(invocation.getMethod(), targetClass, invocation::proceed); }
可以看到它的代理方法 invoke()
的執(zhí)行邏輯在 invokeWithinTransaction()
里:
--代碼位于 org.springframework.transaction.interceptor.TransactionAspectSupport#invokeWithinTransaction -- @Nullable protected Object invokeWithinTransaction(Method method, @Nullable Class<?> targetClass, final InvocationCallback invocation) throws Throwable { // If the transaction attribute is null, the method is non-transactional. TransactionAttributeSource tas = getTransactionAttributeSource(); final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null); final TransactionManager tm = determineTransactionManager(txAttr); if (this.reactiveAdapterRegistry != null && tm instanceof ReactiveTransactionManager) { // 響應(yīng)式事務(wù)相關(guān) } PlatformTransactionManager ptm = asPlatformTransactionManager(tm); final String joinpointIdentification = methodIdentification(method, targetClass, txAttr); if (txAttr == null || !(ptm instanceof CallbackPreferringPlatformTransactionManager)) { // Standard transaction demarcation with getTransaction and commit/rollback calls. TransactionInfo txInfo = createTransactionIfNecessary(ptm, txAttr, joinpointIdentification); Object retVal; try { // This is an around advice: Invoke the next interceptor in the chain. // This will normally result in a target object being invoked. retVal = invocation.proceedWithInvocation(); } catch (Throwable ex) { // target invocation exception completeTransactionAfterThrowing(txInfo, ex); throw ex; } finally { cleanupTransactionInfo(txInfo); } if (vavrPresent && VavrDelegate.isVavrTry(retVal)) { // Set rollback-only in case of Vavr failure matching our rollback rules... TransactionStatus status = txInfo.getTransactionStatus(); if (status != null && txAttr != null) { retVal = VavrDelegate.evaluateTryFailure(retVal, txAttr, status); } } commitTransactionAfterReturning(txInfo); return retVal; } else { // CallbackPreferringPlatformTransactionManager 的處理邏輯 } }
invokeWithinTransaction()
的代碼雖然長(zhǎng),我們還是把它分段來(lái)看:
第一部分,準(zhǔn)備階段
也就是這部分代碼:
// If the transaction attribute is null, the method is non-transactional. TransactionAttributeSource tas = getTransactionAttributeSource(); final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null); final TransactionManager tm = determineTransactionManager(txAttr); PlatformTransactionManager ptm = asPlatformTransactionManager(tm); final String joinpointIdentification = methodIdentification(method, targetClass, txAttr);
獲取 TransactionAttribute
(TransactionDefinition
(底層接口),這里面裝載了事務(wù)傳播等級(jí),隔離級(jí)別等屬性。TransactionAttribute
的創(chuàng)建依據(jù)配置,或者我們的事務(wù)傳播等級(jí)注解,對(duì)什么異常進(jìn)行回滾等,后續(xù)會(huì)繼續(xù)對(duì)它的應(yīng)用做說(shuō)明, PlatformTransactionManager
則是進(jìn)行事務(wù)管理的主要操作者。
第二部分,事務(wù)開(kāi)啟或者獲取與準(zhǔn)備,也就是我們執(zhí)行邏輯的第一行代碼 createTransactionIfNecessary()
(是不是和前面說(shuō)到的 SqlSession的創(chuàng)建或者獲取很像?)
我們可以看到 createTransactionIfNecessary()
的實(shí)現(xiàn)就做了兩件事,其一是獲取一個(gè)叫做 TransactionStatus
的東西,另外則是調(diào)用 prepareTransactionInfo()
,獲取一個(gè) TransactionInfo
:
// Standard transaction demarcation with getTransaction and commit/rollback calls. TransactionInfo txInfo = createTransactionIfNecessary(ptm, txAttr, joinpointIdentification); --代碼位于 org.springframework.transaction.interceptor.TransactionAspectSupport#createTransactionIfNecessary -- protected TransactionInfo createTransactionIfNecessary(@Nullable PlatformTransactionManager tm, @Nullable TransactionAttribute txAttr, final String joinpointIdentification) { TransactionStatus status = tm.getTransaction(txAttr); return prepareTransactionInfo(tm, txAttr, joinpointIdentification, status); }
先看看第一件事,也就是獲取 TransactionStatus
,它保存了事務(wù)的 savePoint
,是否新事物等。刪減掉一些判斷方法,代碼如下:
public final TransactionStatus getTransaction(@Nullable TransactionDefinition definition) throws TransactionException { // Use defaults if no transaction definition given. TransactionDefinition def = (definition != null ? definition : TransactionDefinition.withDefaults()); Object transaction = doGetTransaction(); boolean debugEnabled = logger.isDebugEnabled(); if (isExistingTransaction(transaction)) { // Existing transaction found -> check propagation behavior to find out how to behave. return handleExistingTransaction(def, transaction, debugEnabled); } if (def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED || def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW || def.getPropagationBehavior() == TransactionDefinition.PROPAGATION_NESTED) { SuspendedResourcesHolder suspendedResources = suspend(null); try { boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER); DefaultTransactionStatus status = newTransactionStatus( def, transaction, true, newSynchronization, debugEnabled, suspendedResources); doBegin(transaction, def); prepareSynchronization(status, def); return status; } catch (RuntimeException | Error ex) { resume(null, suspendedResources); throw ex; } } else { // Create "empty" transaction: no actual transaction, but potentially synchronization. if (def.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT && logger.isWarnEnabled()) { logger.warn("Custom isolation level specified but no actual transaction initiated; " + "isolation level will effectively be ignored: " + def); } boolean newSynchronization = (getTransactionSynchronization() == SYNCHRONIZATION_ALWAYS); return prepareTransactionStatus(def, null, true, newSynchronization, debugEnabled, null); } }
代碼很長(zhǎng),但是不急,我們可以簡(jiǎn)單看出它分為兩個(gè)部分:
第一部分是獲取事務(wù) doGetTransaction()
第二部分則是判斷是否新事物,
則 TransactionDefinition.PROPAGATION_REQUIRED
、TransactionDefinition.PROPAGATION_REQUIRES_NEW
、TransactionDefinition.PROPAGATION_NESTED
是一種邏輯
其余是另一種邏輯,信息量有點(diǎn)大,但是慢慢來(lái):
如果不是新事物,則執(zhí)行 handleExistingTransaction
,
如果是新事物
protected Object doGetTransaction() { DataSourceTransactionObject txObject = new DataSourceTransactionObject(); txObject.setSavepointAllowed(isNestedTransactionAllowed()); ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(obtainDataSource()); txObject.setConnectionHolder(conHolder, false); return txObject; }
doGetTransaction
獲取我們的事務(wù)對(duì)象,這里也使用了 TransactionSynchronizationManager
(前面說(shuō)到的 SqlSession
的管理類(lèi)),事務(wù)對(duì)象會(huì)嘗試獲取本事務(wù)所使用的連接對(duì)象,這個(gè)和事務(wù)傳播等級(jí)有關(guān),先立個(gè) flag。
我們可以看到這里面主要邏輯就是去獲取 ConnectionHolder
,實(shí)際上很簡(jiǎn)單,只要能獲取到,就是已經(jīng)存在的事務(wù),獲取不到(或者事務(wù)已經(jīng)關(guān)閉)就是新事物。
如果說(shuō)前面無(wú)法從 TransactionSynchronizationManager
獲取到 conHolder
,或者說(shuō),我們的線程中并沒(méi)有 ConnectionHolder
那么將會(huì)進(jìn)入此分支,此分支的支持的三個(gè)事務(wù)傳播等級(jí) TransactionDefinition.PROPAGATION_REQUIRED
、TransactionDefinition.PROPAGATION_REQUIRES_NEW
、TransactionDefinition.PROPAGATION_NESTED
都是需要?jiǎng)?chuàng)建新事務(wù)的,所以它們?cè)谕粋€(gè)分支里面:
SuspendedResourcesHolder suspendedResources = suspend(null); boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER); DefaultTransactionStatus status = newTransactionStatus( def, transaction, true, newSynchronization, debugEnabled, suspendedResources); doBegin(transaction, def); prepareSynchronization(status, def); return status;
SuspendedResourcesHolder
與事務(wù)的掛起相關(guān),doBegin()
則是對(duì)連接對(duì)象 connection
的獲取和配置,prepareSynchronization()
則是對(duì)新事物的一些初始化操作。我們一點(diǎn)點(diǎn)看:
/** * This implementation sets the isolation level but ignores the timeout. */ @Override protected void doBegin(Object transaction, TransactionDefinition definition) { DataSourceTransactionObject txObject = (DataSourceTransactionObject) transaction; Connection con = null; if (!txObject.hasConnectionHolder() || txObject.getConnectionHolder().isSynchronizedWithTransaction()) { Connection newCon = obtainDataSource().getConnection(); if (logger.isDebugEnabled()) { logger.debug("Acquired Connection [" + newCon + "] for JDBC transaction"); } txObject.setConnectionHolder(new ConnectionHolder(newCon), true); } txObject.getConnectionHolder().setSynchronizedWithTransaction(true); con = txObject.getConnectionHolder().getConnection(); Integer previousIsolationLevel = DataSourceUtils.prepareConnectionForTransaction(con, definition); txObject.setPreviousIsolationLevel(previousIsolationLevel); txObject.setReadOnly(definition.isReadOnly()); // Switch to manual commit if necessary. This is very expensive in some JDBC drivers, // so we don't want to do it unnecessarily (for example if we've explicitly // configured the connection pool to set it already). if (con.getAutoCommit()) { txObject.setMustRestoreAutoCommit(true); if (logger.isDebugEnabled()) { logger.debug("Switching JDBC Connection [" + con + "] to manual commit"); } con.setAutoCommit(false); } prepareTransactionalConnection(con, definition); txObject.getConnectionHolder().setTransactionActive(true); // Bind the connection holder to the thread. if (txObject.isNewConnectionHolder()) { TransactionSynchronizationManager.bindResource(obtainDataSource(), txObject.getConnectionHolder()); } } }
可以看到,ConnectionHolder
的創(chuàng)建和連接的打開(kāi)就是在這里進(jìn)行的,創(chuàng)建后,設(shè)置其隔離級(jí)別,取消 connection
的自動(dòng)提交,將提交操作納入到 spring 管理,并且將其存到 TransactionSynchronizationManager
使得 4.2.1 提到的 doGetTransaction()
可以拿到此 ConnectionHolder
。
做完連接的獲取與配置后,下一步就是對(duì)事物的一些初始化:
/** * Initialize transaction synchronization as appropriate. */ protected void prepareSynchronization(DefaultTransactionStatus status, TransactionDefinition definition) { if (status.isNewSynchronization()) { TransactionSynchronizationManager.setActualTransactionActive(status.hasTransaction()); TransactionSynchronizationManager.setCurrentTransactionIsolationLevel( definition.getIsolationLevel() != TransactionDefinition.ISOLATION_DEFAULT ? definition.getIsolationLevel() : null); TransactionSynchronizationManager.setCurrentTransactionReadOnly(definition.isReadOnly()); TransactionSynchronizationManager.setCurrentTransactionName(definition.getName()); TransactionSynchronizationManager.initSynchronization(); } }
這個(gè)代碼都是代碼字面意義的簡(jiǎn)單設(shè)置,就不贅述了。
剛才講的是 “無(wú)法從 TransactionSynchronizationManager
獲取到 conHolder
”,并且屬于一些需要?jiǎng)?chuàng)建新事物的傳播等級(jí)的情況。
如果說(shuō)方才沒(méi)有事務(wù),也不需要?jiǎng)?chuàng)建新的事務(wù),則會(huì)進(jìn)入此分支,創(chuàng)建一個(gè)空的 TransactionStatus
,內(nèi)部的事務(wù)對(duì)象為空,代碼很簡(jiǎn)單就不貼了,有興趣可以去看看 org.springframework.transaction.support.AbstractPlatformTransactionManager#getTransaction
的最后一個(gè)分支。
剛才說(shuō)的都是無(wú)法獲取到 conHolder
的情況,如果獲取到了,則又是另一套代碼了,handleExistingTransaction
很長(zhǎng),它的第一個(gè)部分是對(duì)傳播等級(jí)的控制,有興趣的小伙伴可以去看看源碼,我這里只挑一個(gè)簡(jiǎn)單的傳播等級(jí) PROPAGATION_NESTED_NEW
做說(shuō)明(其他的會(huì)在專(zhuān)門(mén)的事務(wù)一期做講解):
-- 代碼位于 org.springframework.transaction.support.AbstractPlatformTransactionManager#handleExistingTransaction --private TransactionStatus handleExistingTransaction( TransactionDefinition definition, Object transaction, boolean debugEnabled) throws TransactionException { if (definition.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRES_NEW) { if (debugEnabled) { logger.debug("Suspending current transaction, creating new transaction with name [" + definition.getName() + "]"); } SuspendedResourcesHolder suspendedResources = suspend(transaction); try { boolean newSynchronization = (getTransactionSynchronization() != SYNCHRONIZATION_NEVER); DefaultTransactionStatus status = newTransactionStatus( definition, transaction, true, newSynchronization, debugEnabled, suspendedResources); doBegin(transaction, definition); prepareSynchronization(status, definition); return status; } catch (RuntimeException | Error beginEx) { resumeAfterBeginException(transaction, suspendedResources, beginEx); throw beginEx; } } ... 略 }
我們可以發(fā)現(xiàn)和 4.2.2 新事物的處理
代碼是一樣的,唯一的區(qū)別就是此 TransactionStatus
對(duì)象會(huì)真正內(nèi)嵌一個(gè)事務(wù)掛起對(duì)象 SuspendedResourcesHolder
。
拿到 TransactionStatus
之后, prepareTransactionInfo()
里簡(jiǎn)單的將剛才那些 PlatformTransactionManager
、TransactionAttribute
、TransactionStatus
包裝成一個(gè) TransactionInfo
對(duì)象,并將其保存在 ThreadLocal
中,這個(gè) bindToThread()
還會(huì)將當(dāng)前已經(jīng)持有的 TransactionInfo
對(duì)象暫存。
protected TransactionInfo prepareTransactionInfo(@Nullable PlatformTransactionManager tm, @Nullable TransactionAttribute txAttr, String joinpointIdentification, @Nullable TransactionStatus status) { TransactionInfo txInfo = new TransactionInfo(tm, txAttr, joinpointIdentification); if (txAttr != null) { // The transaction manager will flag an error if an incompatible tx already exists. txInfo.newTransactionStatus(status); } // We always bind the TransactionInfo to the thread, even if we didn't create // a new transaction here. This guarantees that the TransactionInfo stack // will be managed correctly even if no transaction was created by this aspect. txInfo.bindToThread(); return txInfo; }
到這里思路就很清晰了,代理為我們做的事情就是生成了一個(gè)叫做 TransactionInfo
的東西,里面的 TransactionManager
可以使得 spring 去對(duì)最底層的 connection
對(duì)象做一些回滾,提交操作。TransactionStatus
則保存掛起的事務(wù)的信息,以及當(dāng)前事務(wù)的一些狀態(tài),如下圖:
讓我們回到第四節(jié)開(kāi)頭的那段很長(zhǎng)的代碼,到這里是不是很明了了:
protected Object invokeWithinTransaction(Method method, @Nullable Class<?> targetClass, final InvocationCallback invocation) throws Throwable { // If the transaction attribute is null, the method is non-transactional. TransactionAttributeSource tas = getTransactionAttributeSource(); final TransactionAttribute txAttr = (tas != null ? tas.getTransactionAttribute(method, targetClass) : null); final TransactionManager tm = determineTransactionManager(txAttr); PlatformTransactionManager ptm = asPlatformTransactionManager(tm); final String joinpointIdentification = methodIdentification(method, targetClass, txAttr); if (txAttr == null || !(ptm instanceof CallbackPreferringPlatformTransactionManager)) { // Standard transaction demarcation with getTransaction and commit/rollback calls. TransactionInfo txInfo = createTransactionIfNecessary(ptm, txAttr, joinpointIdentification); Object retVal; try { // This is an around advice: Invoke the next interceptor in the chain. // This will normally result in a target object being invoked. retVal = invocation.proceedWithInvocation(); } catch (Throwable ex) { // target invocation exception completeTransactionAfterThrowing(txInfo, ex); throw ex; } finally { cleanupTransactionInfo(txInfo); } if (vavrPresent && VavrDelegate.isVavrTry(retVal)) { // Set rollback-only in case of Vavr failure matching our rollback rules... TransactionStatus status = txInfo.getTransactionStatus(); if (status != null && txAttr != null) { retVal = VavrDelegate.evaluateTryFailure(retVal, txAttr, status); } } commitTransactionAfterReturning(txInfo); return retVal; } }
1、獲取 TransactionInfo
2、執(zhí)行切面
3、將之前掛起的 TransactionInfo
找回:
private void bindToThread() { // Expose current TransactionStatus, preserving any existing TransactionStatus // for restoration after this transaction is complete. this.oldTransactionInfo = transactionInfoHolder.get(); transactionInfoHolder.set(this); } private void restoreThreadLocalStatus() { // Use stack to restore old transaction TransactionInfo. // Will be null if none was set. transactionInfoHolder.set(this.oldTransactionInfo); }
4、如果需要,則提交當(dāng)前事務(wù)
5、返回切面值
我們?cè)诘谌轮v到,mybatis有一個(gè)叫做 defualtSqlSessionFactory
的類(lèi),負(fù)責(zé)創(chuàng)建 sqlSession
,但是它和 spring 又是怎么產(chǎn)生關(guān)聯(lián)的呢?
答案就在于,spring 實(shí)現(xiàn)了自己的 TransactionFactory
,以及自己的 Transaction
對(duì)象 SpringManagedTransaction
。回顧一下 SqlSession
的創(chuàng)建過(guò)程:
private SqlSession openSessionFromDataSource(ExecutorType execType, TransactionIsolationLevel level, boolean autoCommit) { Transaction tx = null; try { final Environment environment = configuration.getEnvironment(); final TransactionFactory transactionFactory = getTransactionFactoryFromEnvironment(environment); tx = transactionFactory.newTransaction(environment.getDataSource(), level, autoCommit); final Executor executor = configuration.newExecutor(tx, execType); return new DefaultSqlSession(configuration, executor, autoCommit); } catch (Exception e) { closeTransaction(tx); // may have fetched a connection so lets call close() throw ExceptionFactory.wrapException("Error opening session. Cause: " + e, e); } finally { ErrorContext.instance().reset(); } }
看一下 SpringManagedTransaction
是如何管理 connection
的:
private void openConnection() throws SQLException { this.connection = DataSourceUtils.getConnection(this.dataSource); this.autoCommit = this.connection.getAutoCommit(); this.isConnectionTransactional = DataSourceUtils.isConnectionTransactional(this.connection, this.dataSource); LOGGER.debug(() -> "JDBC Connection [" + this.connection + "] will" + (this.isConnectionTransactional ? " " : " not ") + "be managed by Spring"); }
DataSourceUtils.getConnection(this.dataSource);
劃重點(diǎn),里面的實(shí)現(xiàn)不用我多說(shuō)了,我們可以看到熟悉的身影,也就是 ConnectionHolder
,連接是從這里(優(yōu)先)拿的:
ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource); if (conHolder != null && (conHolder.hasConnection() || conHolder.isSynchronizedWithTransaction())) { conHolder.requested(); if (!conHolder.hasConnection()) { logger.debug("Fetching resumed JDBC Connection from DataSource"); conHolder.setConnection(fetchConnection(dataSource)); } return conHolder.getConnection(); }
更新整套體系圖:
我們整體簡(jiǎn)單過(guò)一次:
mybatis 啟動(dòng)時(shí)根據(jù)xml、注解創(chuàng)建了 mapperedStatement
,用于sql執(zhí)行,創(chuàng)建了 SqlSessionFactory
用于創(chuàng)建 SqlSession
對(duì)象。
mybatis 啟動(dòng)時(shí)創(chuàng)建了 MapperProxyFactory
用于創(chuàng)建接口的代理對(duì)象 MapperProxy
在創(chuàng)建 MapperProxy
時(shí),spring 為其注入了一個(gè) sqlSession
用于 sql執(zhí)行,但是這個(gè) sqlSession
是一個(gè)代理對(duì)象,叫做 sqlSessionTemplate
,它會(huì)自動(dòng)選擇我們?cè)撌褂媚膫€(gè) sqlSession
去執(zhí)行
在執(zhí)行時(shí),spring 切面在執(zhí)行事務(wù)之前,會(huì)創(chuàng)建一個(gè)叫做 TransactionInfo
的對(duì)象,此對(duì)象會(huì)根據(jù)事務(wù)傳播等級(jí)來(lái)控制是否創(chuàng)建新連接,是否掛起上一個(gè)連接,將信息保存在 TransactionSynchronizationManager
到了真正需要?jiǎng)?chuàng)建或者獲取 sqlSession
時(shí),spring 重寫(xiě)的 TransactionFactory
會(huì)優(yōu)先去 TransactionSynchronizationManager
中拿連接對(duì)象。
看完上述內(nèi)容,你們對(duì)mybatis事務(wù)的管理控制有進(jìn)一步的了解嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。