使用方法概述
事务是指满足ACID属性的一组完整操作,是数据库数据完整性和一致性的基本保障。在myBatis中有两种管理事务的方式,一种是依托于底层JDBC的事务管理,另一种是本身不处理事务,将回滚和提交操作置为空操作,由Spring等容器的事务管理机制进行接管。这两种方式在配置文件中将transactionManager属性分别配置为JDBC和MANAGED,比如配置为JDBC:
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
在由JDBC进行事务管理的场景下,使用SqlSession接口提供的方法commit和rollback显示的操作事务。
工作原理分析
在XMLConfigBuilder加载config文件时,会解析environment中关于transactionManager的配置,并使用构造器builder模式将environment设置到config对象中。
private void environmentsElement(XNode context) throws Exception {
if (context != null) {
if (environment == null) {
environment = context.getStringAttribute("default");
}
for (XNode child : context.getChildren()) {
String id = child.getStringAttribute("id");
if (isSpecifiedEnvironment(id)) {
TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
// 解析environment下的dataSource标签
DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
DataSource dataSource = dsFactory.getDataSource();
Environment.Builder environmentBuilder = new Environment.Builder(id).transactionFactory(txFactory)
.dataSource(dataSource);
configuration.setEnvironment(environmentBuilder.build());
}
}
}
}
具体的,在解析transactionManager时,依据配置的type的值,从Configuration对象设置的类型别名(typeAlias)中获取真正的类型:
typeAliasRegistry.registerAlias("JDBC", JdbcTransactionFactory.class);
typeAliasRegistry.registerAlias("MANAGED", ManagedTransactionFactory.class);
然后依赖反射newInstance生成实际的事务工厂。
private TransactionFactory transactionManagerElement(XNode context) throws Exception {
if (context != null) {
String type = context.getStringAttribute("type");
Properties props = context.getChildrenAsProperties();
TransactionFactory factory = (TransactionFactory) resolveClass(type).getDeclaredConstructor().newInstance();
factory.setProperties(props);
return factory;
}
throw new BuilderException("Environment declaration requires a TransactionFactory.");
}
在创建SqlSession时,先读取上述的配置过程构造事务工厂,然后再结合数据库连接配置、事务隔离级别和是否自动提交等参数构造事务实例。最终形成SqlSession -> Executor -> Transaction的对象层次。
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);
Transaction体系的UML图示: