事务基本概念:
事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit)。事务通常由高级数据库操纵语言或编程语言(如SQL,C++或Java)书写的用户程序的执行所引起,并用形如begin transaction和end transaction语句(或函数调用)来界定。事务由事务开始(begin transaction)和事务结束(end transaction)之间执行的全体操作组成。
而在实际的应用当中,事务可能只是一条单个的SQL语句,或者是几条SQL组成,共同执行的。
事务的基本特征:
事务是恢复和并发控制的基本单位。
事务应该具有4个属性:原子性、一致性、隔离性、持久性。这四个属性通常称为ACID特性。
**原子性(atomicity):**一个事务是一个不可分割的工作单位,事务中包括的诸操作要么都做,要么都不做。
**一致性(consistency):**事务必须是使数据库从一个一致性状态变到另一个一致性状态。一致性与原子性是密切相关的。
**隔离性(isolation):**一个事务的执行不能被其他事务干扰。即一个事务内部的操作及使用的数据对并发的其他事务是隔离的,并发执行的各个事务之间不能互相干扰。
**持久性(durability):**指一个事务一旦提交,它对数据库中数据的改变就应该是永久性的。接下来的其他操作或故障不应该对其有任何影响。
那么该如何进行理解事务的基本特性呢:
事务的原子性:
—>原子性在最初在物理上解释为:不可再分割的。
可是在这里却感觉与物理上的理解有所不同,因为它把你随便一个事务都划分为原子性的,且它的原子性更偏向于结果的唯一性:要么成功,要么失败。
事务是否可以什么都不做?还是说一定要与数据库联系起来?
—>由事务的定义:“事务是访问并可能更新数据库中各种数据项的一个程序执行单元"可知,事务首先要实现对数据库的访问。这样来看,事务是不可能什么都不做的。因此事务的存在确实与数据库相关。
事务的一致性?
—>这个问题不是很好理解。一致性与原子性关系密切。
假如有一个产品表和一个产品明细表,是父子关系,当我们添加产品表的时候,一定要添加产品明细表的资料,如果我们只是添加了产品表没有添加产品明细,这样就会是数据不一致,混乱,这里我们就要用到事务了。
事务的隔离性?
—>这个比较好理解,若学过多线程的话就能理解多线程为什么要引入锁机制了。而同样为了解决事务的并发问题而引入的事务的隔离级别,数据库和应用程序都可以设置隔离级别。
事务的持久性?
—>假若你的事务是去银行存钱,那么你总不希望你刚刚提交完存钱请求之后(即事务已提交成功),由于突然的故障而让你的金钱“失联”吧!这就是事务的持久性。
这里有个问题,如果我的“事务”过程只是存钱,那么,根据以上的栗子,存钱完后——即事务已提交成功——那么是不是意味着事务已经完成了呢?那么后续的动作是不是就不是事务的范围之内了呢?存钱的事务完成,动作就传递给了数据库,那么于数据库的动作不再我刚刚那个“事务”当中呀!确实有待多了解。
===========================================================================
接下来进入今天的正文:Mybatis的事务支持
Mybatis之中的事务是由Tractional接口进行定义:见下图:
在这里插入代码片
Transaction提供了如上几个方法:全部是和Connection相关的方法,控制着Connection的生命周期:创建、预编译、提交/回滚和关闭。
Transaction给开发人员提供了两种事务实现类:
JDBCTransaction
ManagedTranstion
继承关系如下:
图代为上传
Mybatis事务的基本核心配置文件:
<environment id="dev">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="${name}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
其中的transactionManager type属性表示启用哪个事务;
事务源码解析:
// 解析事务标签
private TransactionFactory transactionManagerElement(XNode context) throws Exception {
if (context != null) {
// 解析出【JDBC】
String type = context.getStringAttribute("type");
Properties props = context.getChildrenAsProperties();
// 获取【JDBC】对应的类
TransactionFactory factory = (TransactionFactory) resolveClass(type).newInstance();
factory.setProperties(props);
return factory;
}
throw new BuilderException("Environment declaration requires a TransactionFactory.");
}
TypeAliasRegistry类
public <T> Class<T> resolveAlias(String string) {
try {
if (string == null) {
return null;
}
// issue #748 变成小写
String key = string.toLowerCase(Locale.ENGLISH);
Class<T> value;
// 别名判断
if (TYPE_ALIASES.containsKey(key)) {
value = (Class<T>) TYPE_ALIASES.get(key);
// 获取JdbcTransactionFactory类
} else {
value = (Class<T>) Resources.classForName(string);
}
return value;
} catch (ClassNotFoundException e) {
throw new TypeException("Could not resolve type alias '" + string + "'. Cause: " + e, e);
}
}
上面文件解析之后全部进行封装到Configuration之中。
源码解析过程分析:
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
TransactionFactory txFactory = transactionManagerElement(child.evalNode("transactionManager"));
// 继续解析其他标签
DataSourceFactory dsFactory = dataSourceElement(child.evalNode("dataSource"));
DataSource dataSource = dsFactory.getDataSource();
// 封装到Environment
Environment.Builder environmentBuilder = new Environment.Builder(id)
.transactionFactory(txFactory)
.dataSource(dataSource);
// 封装到Configuration中
configuration.setEnvironment(environmentBuilder.build());
}
}
}
}
JDBCTransactionFactory如何生成一个JDBCTransaction的
public class JdbcTransactionFactory implements TransactionFactory {
@Override
public void setProperties(Properties props) {
}
// 传递进去一个Connection
@Override
public Transaction newTransaction(Connection conn) {
return new JdbcTransaction(conn);
}
@Override
public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) {
return new JdbcTransaction(ds, level, autoCommit);
}
}
public class JdbcTransaction implements Transaction {
private static final Log log = LogFactory.getLog(JdbcTransaction.class);
protected Connection connection;
protected DataSource dataSource;
protected TransactionIsolationLevel level; // 事务隔离级别
protected boolean autoCommmit; // 自动提交
/**构造函数**/
public JdbcTransaction(DataSource ds, TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit) {
dataSource = ds;
level = desiredLevel;
autoCommmit = desiredAutoCommit;
}
public JdbcTransaction(Connection connection) {
this.connection = connection;
}
@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();
}
}
// 链接关闭
@Override
public void close() throws SQLException {
if (connection != null) {
resetAutoCommit();
if (log.isDebugEnabled()) {
log.debug("Closing JDBC Connection [" + connection + "]");
}
connection.close();
}
}
protected void setDesiredAutoCommit(boolean desiredAutoCommit) {
try {
if (connection.getAutoCommit() != desiredAutoCommit) {
if (log.isDebugEnabled()) {
log.debug("Setting autocommit to " + desiredAutoCommit + " on JDBC Connection [" + connection + "]");
}
connection.setAutoCommit(desiredAutoCommit);
}
} catch (SQLException e) {
// Only a very poorly implemented driver would fail here,
// and there's not much we can do about that.
throw new TransactionException("Error configuring AutoCommit. "
+ "Your driver may not support getAutoCommit() or setAutoCommit(). "
+ "Requested setting: " + desiredAutoCommit + ". Cause: " + e, e);
}
}
protected void resetAutoCommit() {
try {
if (!connection.getAutoCommit()) {
// MyBatis does not call commit/rollback on a connection if just selects were performed.
// Some databases start transactions with select statements
// and they mandate a commit/rollback before closing the connection.
// A workaround is setting the autocommit to true before closing the connection.
// Sybase throws an exception here.
if (log.isDebugEnabled()) {
log.debug("Resetting autocommit to true on JDBC Connection [" + connection + "]");
}
connection.setAutoCommit(true);
}
} catch (SQLException e) {
if (log.isDebugEnabled()) {
log.debug("Error resetting autocommit to true "
+ "before closing the connection. Cause: " + e);
}
}
}
protected void openConnection() throws SQLException {
if (log.isDebugEnabled()) {
log.debug("Opening JDBC Connection");
}
connection = dataSource.getConnection();
if (level != null) {
connection.setTransactionIsolation(level.getLevel());
}
setDesiredAutoCommit(autoCommmit);
}
@Override
public Integer getTimeout() throws SQLException {
return null;
}
}
Sqlsession的生成过程:
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();
}
}
未完待续。。。敬请期待 哈哈~