mybatis事务相关知识点
来源:互联网 发布:淘宝店多少单一颗心 编辑:程序博客网 时间:2024/06/05 16:06
mybatis事务相关知:
事务接口(Transaction)一般包括如下几个方法:创建(getConnection) 提交(commit) 回滚(rollback) 关闭(close)可能还会有getTimeout。
Transaction接口代码片段:
{
public abstract Connection getConnection()
throws SQLException;
public abstract void commit()
throws SQLException;
public abstract void rollback()
throws SQLException;
public abstract void close()
throws SQLException;
public abstract Integer getTimeout()
throws SQLException;
}
public abstract Connection getConnection()
throws SQLException;
public abstract void commit()
throws SQLException;
public abstract void rollback()
throws SQLException;
public abstract void close()
throws SQLException;
public abstract Integer getTimeout()
throws SQLException;
}
mybatis有两种机制来对事务进行管理:
1.使用JdbcTransaction机制。从源码中可以知道,JdbcTransaction是对数据库连接Connection接口的包装,内部事务管理还是调用Connection中的commit、rollback 等方法。如图:
JdbcTransaction implements Transaction
public Connection getConnection()
throws SQLException
{
if (this.connection == null) {
openConnection();
}
return this.connection;
}
public void commit()
throws SQLException
{
if ((this.connection != null) && (!this.connection.getAutoCommit()))
{
if (log.isDebugEnabled()) {
log.debug("Committing JDBC Connection [" + this.connection + "]");
}
this.connection.commit();
}
}
public void rollback()
throws SQLException
{
if ((this.connection != null) && (!this.connection.getAutoCommit()))
{
if (log.isDebugEnabled()) {
log.debug("Rolling back JDBC Connection [" + this.connection + "]");
}
this.connection.rollback();
}
}
public void close()
throws SQLException
{
if (this.connection != null)
{
resetAutoCommit();
if (log.isDebugEnabled()) {
log.debug("Closing JDBC Connection [" + this.connection + "]");
}
this.connection.close();
}
}
throws SQLException
{
if (this.connection == null) {
openConnection();
}
return this.connection;
}
public void commit()
throws SQLException
{
if ((this.connection != null) && (!this.connection.getAutoCommit()))
{
if (log.isDebugEnabled()) {
log.debug("Committing JDBC Connection [" + this.connection + "]");
}
this.connection.commit();
}
}
public void rollback()
throws SQLException
{
if ((this.connection != null) && (!this.connection.getAutoCommit()))
{
if (log.isDebugEnabled()) {
log.debug("Rolling back JDBC Connection [" + this.connection + "]");
}
this.connection.rollback();
}
}
public void close()
throws SQLException
{
if (this.connection != null)
{
resetAutoCommit();
if (log.isDebugEnabled()) {
log.debug("Closing JDBC Connection [" + this.connection + "]");
}
this.connection.close();
}
}
2.使用ManagedTransaction机制。从源码中得知,ManagedTransaction什么都不做,而是事务交由外部容器(JBoss、WebLogic)来处理,如图:
public Connection getConnection()
throws SQLException
{
if (this.connection == null) {
openConnection();
}
return this.connection;
}
public void commit()
throws SQLException
{}
public void rollback()
throws SQLException
{}
public void close()
throws SQLException
{
if ((this.closeConnection) && (this.connection != null))
{
if (log.isDebugEnabled()) {
log.debug("Closing JDBC Connection [" + this.connection + "]");
}
this.connection.close();
}
}
throws SQLException
{
if (this.connection == null) {
openConnection();
}
return this.connection;
}
public void commit()
throws SQLException
{}
public void rollback()
throws SQLException
{}
public void close()
throws SQLException
{
if ((this.closeConnection) && (this.connection != null))
{
if (log.isDebugEnabled()) {
log.debug("Closing JDBC Connection [" + this.connection + "]");
}
this.connection.close();
}
}
现在的web项目,mybatis的事务一般交由spring事务管理。
以上都是个人理解,若有不妥的地方,可以私信交流。
阅读全文
0 0
- mybatis事务相关知识点
- 事务相关知识点
- mybatis相关知识点
- Mybatis相关知识点
- mybatis相关知识点的总结
- spring事务管理配置以及事务相关知识点
- mybatis中xml文件的相关知识点
- mybatis PageHelper和pageInfo的相关知识点
- Java软件开发基础知识梳理之(6)------事务相关知识点
- MyBatis知识点
- mybatis知识点
- MyBatis知识点
- mybatis知识点
- Mybatis知识点
- mybatis事务
- 事务相关
- ocp知识点:控制事务
- 事务知识点整理
- x&(x-1) 表达式
- 信息安全工程师 学习笔记(三十六)
- MySQL必知必会
- Apache eagle监控hive任务问题
- linux 下多线程错误 undefined reference to `sem_init'
- mybatis事务相关知识点
- 如何处理List条目中含有EditText,并且EditText中文字或者数字可以修改
- 虚拟化相关概念与技术整理(2)——硬件辅助虚拟化简介
- Android Studio查看函数(方法)的形参列表,返回值,以及提示信息
- 数据库 ——(1)
- PHP--内存泄漏
- mybatis知识汇总(一)
- 《Node.js简记》包管理器npm的安装、加速和常用指令
- HBuilder真机调试检测不了魅族手机