首页 > 其他分享 >深入理解mybatis原理(八) MyBatis事务管理机制

深入理解mybatis原理(八) MyBatis事务管理机制

时间:2022-12-14 20:35:31浏览次数:45  
标签:throws level Connection 管理机制 mybatis connection SQLException MyBatis public


  MyBatis作为​​Java​​语言的​​数据库​​框架,对​​数据库​​的事务管理是其非常重要的一个方面。本文将讲述MyBatis的事务管理的实现机制。首先介绍MyBatis的事务Transaction的接口设计以及其不同实现JdbcTransaction和 ManagedTransaction;接着,从MyBatis的XML配置文件入手,讲解MyBatis事务工厂的创建和维护,进而阐述了MyBatis事务的创建和使用;最后分析JdbcTransactionManagedTransaction的实现和二者的不同特点。

以下是本文的组织结构:

深入理解mybatis原理(八) MyBatis事务管理机制_bc

一、概述

    对数据库的事务而言,应该具有以下几点:创建(create)、提交(commit)、回滚(rollback)、关闭(close)。对应地,MyBatis将事务抽象成了Transaction接口:其接口定义如下:

深入理解mybatis原理(八) MyBatis事务管理机制_Source_02

MyBatis的事务管理分为两种形式:

一、使用JDBC的事务管理机制:即利用Java.sql.Connection对象完成对事务的提交(commit())、回滚(rollback())、关闭(close())等

二、使用MANAGED的事务管理机制:这种机制MyBatis自身不会去实现事务管理,而是让程序的容器如(JBOSS,Weblogic)来实现对事务的管理

这两者的类图如下所示:

深入理解mybatis原理(八) MyBatis事务管理机制_java_03

二、事务的配置、创建和使用

1. 事务的配置

我们在使用MyBatis时,一般会在MyBatisXML配置文件中定义类似如下的信息:

深入理解mybatis原理(八) MyBatis事务管理机制_java_04

<environment>节点定义了连接某个数据库的信息,其子节点<transactionManager> 的type 会决定我们用什么类型的事务管理机制。

2.事务工厂的创建

 MyBatis事务的创建是交给TransactionFactory 事务工厂来创建的,如果我们将<transactionManager>的type 配置为"JDBC",那么,在MyBatis初始化解析<environment>节点时,会根据type="JDBC"创建一个JdbcTransactionFactory工厂,其源码如下:



[java]  ​​view plain​​  ​​copy​​

 



  1.   /**
  2.    * 解析<transactionManager>节点,创建对应的TransactionFactory
  3.    * @param context
  4.    * @return
  5.    * @throws Exception
  6.    */  
  7. private TransactionFactory transactionManagerElement(XNode context) throws Exception {  
  8. if (context != null) {  
  9. "type");  
  10.     Properties props = context.getChildrenAsProperties();  
  11. /*
  12.           在Configuration初始化的时候,会通过以下语句,给JDBC和MANAGED对应的工厂类
  13.           typeAliasRegistry.registerAlias("JDBC", JdbcTransactionFactory.class);
  14.           typeAliasRegistry.registerAlias("MANAGED", ManagedTransactionFactory.class);
  15.           下述的resolveClass(type).newInstance()会创建对应的工厂实例
  16.      */  
  17.     TransactionFactory factory = (TransactionFactory) resolveClass(type).newInstance();  
  18.     factory.setProperties(props);  
  19. return factory;  
  20.   }  
  21. throw new BuilderException("Environment declaration requires a TransactionFactory.");  
  22. }  




[java]  ​​view plain​​  ​​copy​​



  1.   /**
  2.    * 解析<transactionManager>节点,创建对应的TransactionFactory
  3.    * @param context
  4.    * @return
  5.    * @throws Exception
  6.    */  
  7. private TransactionFactory transactionManagerElement(XNode context) throws Exception {  
  8. if (context != null) {  
  9. "type");  
  10.     Properties props = context.getChildrenAsProperties();  
  11. /*
  12.           在Configuration初始化的时候,会通过以下语句,给JDBC和MANAGED对应的工厂类
  13.           typeAliasRegistry.registerAlias("JDBC", JdbcTransactionFactory.class);
  14.           typeAliasRegistry.registerAlias("MANAGED", ManagedTransactionFactory.class);
  15.           下述的resolveClass(type).newInstance()会创建对应的工厂实例
  16.      */  
  17.     TransactionFactory factory = (TransactionFactory) resolveClass(type).newInstance();  
  18.     factory.setProperties(props);  
  19. return factory;  
  20.   }  
  21. throw new BuilderException("Environment declaration requires a TransactionFactory.");  
  22. }  



   JdbcTransactionFactory.class 实例;如果type="MANAGED",则MyBatis会创建一个MangedTransactionFactory.class实例。

 MyBatis对<transactionManager>节点的解析会生成 TransactionFactory实例;而对<dataSource>解析会生成datasouce实例(关于dataSource的解析和原理,读者可以参照我的另一篇博文:​​《深入理解mybatis原理》 Mybatis数据源与连接池 ​​  
),作为<environment>节点,会根据TransactionFactory和DataSource实例创建一个Environment对象,代码如下所示:



[java]  ​​view plain​​  ​​copy​​

 


  1. private void environmentsElement(XNode context) throws Exception {  
  2. if (context != null) {  
  3. if (environment == null) {  
  4. "default");  
  5.     }  
  6. for (XNode child : context.getChildren()) {  
  7. "id");  
  8. //是和默认的环境相同时,解析之  
  9. if (isSpecifiedEnvironment(id)) {  
  10. //1.解析<transactionManager>节点,决定创建什么类型的TransactionFactory  
  11. "transactionManager"));  
  12. //2. 创建dataSource  
  13. "dataSource"));  
  14.         DataSource dataSource = dsFactory.getDataSource();  
  15. //3. 使用了Environment内置的构造器Builder,传递id 事务工厂TransactionFactory和数据源DataSource  
  16. new Environment.Builder(id)  
  17.             .transactionFactory(txFactory)  
  18.             .dataSource(dataSource);  
  19.         configuration.setEnvironment(environmentBuilder.build());  
  20.       }  
  21.     }  
  22.   }  
  23. }  




[java]  ​​view plain​​  ​​copy​​



  1. private void environmentsElement(XNode context) throws Exception {  
  2. if (context != null) {  
  3. if (environment == null) {  
  4. "default");  
  5.     }  
  6. for (XNode child : context.getChildren()) {  
  7. "id");  
  8. //是和默认的环境相同时,解析之  
  9. if (isSpecifiedEnvironment(id)) {  
  10. //1.解析<transactionManager>节点,决定创建什么类型的TransactionFactory  
  11. "transactionManager"));  
  12. //2. 创建dataSource  
  13. "dataSource"));  
  14.         DataSource dataSource = dsFactory.getDataSource();  
  15. //3. 使用了Environment内置的构造器Builder,传递id 事务工厂TransactionFactory和数据源DataSource  
  16. new Environment.Builder(id)  
  17.             .transactionFactory(txFactory)  
  18.             .dataSource(dataSource);  
  19.         configuration.setEnvironment(environmentBuilder.build());  
  20.       }  
  21.     }  
  22.   }  
  23. }  



     Environment表示着一个数据库的连接,生成后的Environment对象会被设置到Configuration实例中,以供后续的使用。

深入理解mybatis原理(八) MyBatis事务管理机制_bc_05

上述一直在讲事务工厂TransactionFactory来创建的Transaction,现在让我们看一下MyBatis中的TransactionFactory的定义吧。

3. 事务工厂TransactionFactory

事务工厂Transaction定义了创建Transaction的两个方法:一个是通过指定的Connection对象创建Transaction,另外是通过数据源DataSource来创建Transaction。与JDBC 和MANAGED两种Transaction相对应,TransactionFactory有两个对应的实现的子类:如下所示:

深入理解mybatis原理(八) MyBatis事务管理机制_java_06

4. 事务Transaction的创建

通过事务工厂TransactionFactory很容易获取到Transaction对象实例。我们以JdbcTransaction为例,看一下JdbcTransactionFactory是怎样生成JdbcTransaction的,代码如下:



[java]  ​​view plain​​  ​​copy​​



  1. public class JdbcTransactionFactory implements TransactionFactory {  
  2.   
  3. public void setProperties(Properties props) {  
  4.   }  
  5.   
  6. /**
  7.      * 根据给定的数据库连接Connection创建Transaction
  8.      * @param conn Existing database connection
  9.      * @return
  10.      */  
  11. public Transaction newTransaction(Connection conn) {  
  12. return new JdbcTransaction(conn);  
  13.   }  
  14.   
  15. /**
  16.      * 根据DataSource、隔离级别和是否自动提交创建Transacion
  17.      *
  18.      * @param ds
  19.      * @param level Desired isolation level
  20.      * @param autoCommit Desired autocommit
  21.      * @return
  22.      */  
  23. public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) {  
  24. return new JdbcTransaction(ds, level, autoCommit);  
  25.   }  
  26. }  




[java]  ​​view plain​​  ​​copy​​



  1. public class JdbcTransactionFactory implements TransactionFactory {  
  2.   
  3. public void setProperties(Properties props) {  
  4.   }  
  5.   
  6. /**
  7.      * 根据给定的数据库连接Connection创建Transaction
  8.      * @param conn Existing database connection
  9.      * @return
  10.      */  
  11. public Transaction newTransaction(Connection conn) {  
  12. return new JdbcTransaction(conn);  
  13.   }  
  14.   
  15. /**
  16.      * 根据DataSource、隔离级别和是否自动提交创建Transacion
  17.      *
  18.      * @param ds
  19.      * @param level Desired isolation level
  20.      * @param autoCommit Desired autocommit
  21.      * @return
  22.      */  
  23. public Transaction newTransaction(DataSource ds, TransactionIsolationLevel level, boolean autoCommit) {  
  24. return new JdbcTransaction(ds, level, autoCommit);  
  25.   }  
  26. }  


  如上说是,JdbcTransactionFactory会创建JDBC类型的Transaction,即JdbcTransaction。类似地,ManagedTransactionFactory也会创建ManagedTransaction。下面我们会分别深入JdbcTranaction 和ManagedTransaction,看它们到底是怎样实现事务管理的。

5. JdbcTransaction

    JdbcTransaction直接使用JDBC的提交和回滚事务管理机制 。它依赖与从dataSource中取得的连接connection 来管理transaction 的作用域,connection对象的获取被延迟到调用getConnection()方法。如果autocommit设置为on,开启状态的话,它会忽略commit和rollback。

    直观地讲,就是JdbcTransaction是使用的java.sql.Connection 上的commit和rollback功能,JdbcTransaction只是相当于对java.sql.Connection事务处理进行了一次包装(wrapper),Transaction的事务管理都是通过java.sql.Connection实现的。JdbcTransaction的代码实现如下:



[java]  ​​view plain​​  ​​copy​​

 


  1. /**
  2.  * @see JdbcTransactionFactory
  3.  */  
  4. /**
  5.  * @author Clinton Begin
  6.  */  
  7. public class JdbcTransaction implements Transaction {  
  8.   
  9. private static final Log log = LogFactory.getLog(JdbcTransaction.class);  
  10.   
  11. //数据库连接  
  12. protected Connection connection;  
  13. //数据源  
  14. protected DataSource dataSource;  
  15. //隔离级别  
  16. protected TransactionIsolationLevel level;  
  17. //是否为自动提交  
  18. protected boolean autoCommmit;  
  19.   
  20. public JdbcTransaction(DataSource ds, TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit) {  
  21.     dataSource = ds;  
  22.     level = desiredLevel;  
  23.     autoCommmit = desiredAutoCommit;  
  24.   }  
  25.   
  26. public JdbcTransaction(Connection connection) {  
  27. this.connection = connection;  
  28.   }  
  29.   
  30. public Connection getConnection() throws SQLException {  
  31. if (connection == null) {  
  32.       openConnection();  
  33.     }  
  34. return connection;  
  35.   }  
  36.   
  37. /**
  38.      * commit()功能 使用connection的commit()
  39.      * @throws SQLException
  40.      */  
  41. public void commit() throws SQLException {  
  42. if (connection != null && !connection.getAutoCommit()) {  
  43. if (log.isDebugEnabled()) {  
  44. "Committing JDBC Connection [" + connection + "]");  
  45.       }  
  46.       connection.commit();  
  47.     }  
  48.   }  
  49.   
  50. /**
  51.      * rollback()功能 使用connection的rollback()
  52.      * @throws SQLException
  53.      */  
  54. public void rollback() throws SQLException {  
  55. if (connection != null && !connection.getAutoCommit()) {  
  56. if (log.isDebugEnabled()) {  
  57. "Rolling back JDBC Connection [" + connection + "]");  
  58.       }  
  59.       connection.rollback();  
  60.     }  
  61.   }  
  62.   
  63. /**
  64.      * close()功能 使用connection的close()
  65.      * @throws SQLException
  66.      */  
  67. public void close() throws SQLException {  
  68. if (connection != null) {  
  69.       resetAutoCommit();  
  70. if (log.isDebugEnabled()) {  
  71. "Closing JDBC Connection [" + connection + "]");  
  72.       }  
  73.       connection.close();  
  74.     }  
  75.   }  
  76.   
  77. protected void setDesiredAutoCommit(boolean desiredAutoCommit) {  
  78. try {  
  79. if (connection.getAutoCommit() != desiredAutoCommit) {  
  80. if (log.isDebugEnabled()) {  
  81. "Setting autocommit to " + desiredAutoCommit + " on JDBC Connection [" + connection + "]");  
  82.         }  
  83.         connection.setAutoCommit(desiredAutoCommit);  
  84.       }  
  85. catch (SQLException e) {  
  86. // Only a very poorly implemented driver would fail here,  
  87. // and there's not much we can do about that.  
  88. throw new TransactionException("Error configuring AutoCommit.  "  
  89. "Your driver may not support getAutoCommit() or setAutoCommit(). "  
  90. "Requested setting: " + desiredAutoCommit + ".  Cause: " + e, e);  
  91.     }  
  92.   }  
  93.   
  94. protected void resetAutoCommit() {  
  95. try {  
  96. if (!connection.getAutoCommit()) {  
  97. // MyBatis does not call commit/rollback on a connection if just selects were performed.  
  98. // Some databases start transactions with select statements  
  99. // and they mandate a commit/rollback before closing the connection.  
  100. // A workaround is setting the autocommit to true before closing the connection.  
  101. // Sybase throws an exception here.  
  102. if (log.isDebugEnabled()) {  
  103. "Resetting autocommit to true on JDBC Connection [" + connection + "]");  
  104.         }  
  105. true);  
  106.       }  
  107. catch (SQLException e) {  
  108. "Error resetting autocommit to true "  
  109. "before closing the connection.  Cause: " + e);  
  110.     }  
  111.   }  
  112.   
  113. protected void openConnection() throws SQLException {  
  114. if (log.isDebugEnabled()) {  
  115. "Opening JDBC Connection");  
  116.     }  
  117.     connection = dataSource.getConnection();  
  118. if (level != null) {  
  119.       connection.setTransactionIsolation(level.getLevel());  
  120.     }  
  121.     setDesiredAutoCommit(autoCommmit);  
  122.   }  
  123.   
  124. }  




[java]  ​​view plain​​  ​​copy​​



  1. /**
  2.  * @see JdbcTransactionFactory
  3.  */  
  4. /**
  5.  * @author Clinton Begin
  6.  */  
  7. public class JdbcTransaction implements Transaction {  
  8.   
  9. private static final Log log = LogFactory.getLog(JdbcTransaction.class);  
  10.   
  11. //数据库连接  
  12. protected Connection connection;  
  13. //数据源  
  14. protected DataSource dataSource;  
  15. //隔离级别  
  16. protected TransactionIsolationLevel level;  
  17. //是否为自动提交  
  18. protected boolean autoCommmit;  
  19.   
  20. public JdbcTransaction(DataSource ds, TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit) {  
  21.     dataSource = ds;  
  22.     level = desiredLevel;  
  23.     autoCommmit = desiredAutoCommit;  
  24.   }  
  25.   
  26. public JdbcTransaction(Connection connection) {  
  27. this.connection = connection;  
  28.   }  
  29.   
  30. public Connection getConnection() throws SQLException {  
  31. if (connection == null) {  
  32.       openConnection();  
  33.     }  
  34. return connection;  
  35.   }  
  36.   
  37. /**
  38.      * commit()功能 使用connection的commit()
  39.      * @throws SQLException
  40.      */  
  41. public void commit() throws SQLException {  
  42. if (connection != null && !connection.getAutoCommit()) {  
  43. if (log.isDebugEnabled()) {  
  44. "Committing JDBC Connection [" + connection + "]");  
  45.       }  
  46.       connection.commit();  
  47.     }  
  48.   }  
  49.   
  50. /**
  51.      * rollback()功能 使用connection的rollback()
  52.      * @throws SQLException
  53.      */  
  54. public void rollback() throws SQLException {  
  55. if (connection != null && !connection.getAutoCommit()) {  
  56. if (log.isDebugEnabled()) {  
  57. "Rolling back JDBC Connection [" + connection + "]");  
  58.       }  
  59.       connection.rollback();  
  60.     }  
  61.   }  
  62.   
  63. /**
  64.      * close()功能 使用connection的close()
  65.      * @throws SQLException
  66.      */  
  67. public void close() throws SQLException {  
  68. if (connection != null) {  
  69.       resetAutoCommit();  
  70. if (log.isDebugEnabled()) {  
  71. "Closing JDBC Connection [" + connection + "]");  
  72.       }  
  73.       connection.close();  
  74.     }  
  75.   }  
  76.   
  77. protected void setDesiredAutoCommit(boolean desiredAutoCommit) {  
  78. try {  
  79. if (connection.getAutoCommit() != desiredAutoCommit) {  
  80. if (log.isDebugEnabled()) {  
  81. "Setting autocommit to " + desiredAutoCommit + " on JDBC Connection [" + connection + "]");  
  82.         }  
  83.         connection.setAutoCommit(desiredAutoCommit);  
  84.       }  
  85. catch (SQLException e) {  
  86. // Only a very poorly implemented driver would fail here,  
  87. // and there's not much we can do about that.  
  88. throw new TransactionException("Error configuring AutoCommit.  "  
  89. "Your driver may not support getAutoCommit() or setAutoCommit(). "  
  90. "Requested setting: " + desiredAutoCommit + ".  Cause: " + e, e);  
  91.     }  
  92.   }  
  93.   
  94. protected void resetAutoCommit() {  
  95. try {  
  96. if (!connection.getAutoCommit()) {  
  97. // MyBatis does not call commit/rollback on a connection if just selects were performed.  
  98. // Some databases start transactions with select statements  
  99. // and they mandate a commit/rollback before closing the connection.  
  100. // A workaround is setting the autocommit to true before closing the connection.  
  101. // Sybase throws an exception here.  
  102. if (log.isDebugEnabled()) {  
  103. "Resetting autocommit to true on JDBC Connection [" + connection + "]");  
  104.         }  
  105. true);  
  106.       }  
  107. catch (SQLException e) {  
  108. "Error resetting autocommit to true "  
  109. "before closing the connection.  Cause: " + e);  
  110.     }  
  111.   }  
  112.   
  113. protected void openConnection() throws SQLException {  
  114. if (log.isDebugEnabled()) {  
  115. "Opening JDBC Connection");  
  116.     }  
  117.     connection = dataSource.getConnection();  
  118. if (level != null) {  
  119.       connection.setTransactionIsolation(level.getLevel());  
  120.     }  
  121.     setDesiredAutoCommit(autoCommmit);  
  122.   }  
  123.   
  124. }  


6. ManagedTransaction

   ManagedTransaction让容器来管理事务Transaction的整个生命周期,意思就是说,使用ManagedTransaction的commit和rollback功能不会对事务有任何的影响,它什么都不会做,它将事务管理的权利移交给了容器来实现。看如下Managed的实现代码大家就会一目了然:



[java]  ​​view plain​​  ​​copy​​

 



  1. /**
  2.  * 
  3.  * 让容器管理事务transaction的整个生命周期
  4.  * connection的获取延迟到getConnection()方法的调用
  5.  * 忽略所有的commit和rollback操作
  6.  * 默认情况下,可以关闭一个连接connection,也可以配置它不可以关闭一个连接
  7.  * 让容器来管理transaction的整个生命周期
  8.  * @see ManagedTransactionFactory
  9.  */  
  10. /**
  11.  * @author Clinton Begin
  12.  */  
  13. public class ManagedTransaction implements Transaction {  
  14.   
  15. private static final Log log = LogFactory.getLog(ManagedTransaction.class);  
  16.   
  17. private DataSource dataSource;  
  18. private TransactionIsolationLevel level;  
  19. private Connection connection;  
  20. private boolean closeConnection;  
  21.   
  22. public ManagedTransaction(Connection connection, boolean closeConnection) {  
  23. this.connection = connection;  
  24. this.closeConnection = closeConnection;  
  25.   }  
  26.   
  27. public ManagedTransaction(DataSource ds, TransactionIsolationLevel level, boolean closeConnection) {  
  28. this.dataSource = ds;  
  29. this.level = level;  
  30. this.closeConnection = closeConnection;  
  31.   }  
  32.   
  33. public Connection getConnection() throws SQLException {  
  34. if (this.connection == null) {  
  35.       openConnection();  
  36.     }  
  37. return this.connection;  
  38.   }  
  39.   
  40. public void commit() throws SQLException {  
  41. // Does nothing  
  42.   }  
  43.   
  44. public void rollback() throws SQLException {  
  45. // Does nothing  
  46.   }  
  47.   
  48. public void close() throws SQLException {  
  49. if (this.closeConnection && this.connection != null) {  
  50. if (log.isDebugEnabled()) {  
  51. "Closing JDBC Connection [" + this.connection + "]");  
  52.       }  
  53. this.connection.close();  
  54.     }  
  55.   }  
  56.   
  57. protected void openConnection() throws SQLException {  
  58. if (log.isDebugEnabled()) {  
  59. "Opening JDBC Connection");  
  60.     }  
  61. this.connection = this.dataSource.getConnection();  
  62. if (this.level != null) {  
  63. this.connection.setTransactionIsolation(this.level.getLevel());  
  64.     }  
  65.   }  
  66.   
  67. }  




[java]  ​​view plain​​  ​​copy​​



  1. /**
  2.  * 
  3.  * 让容器管理事务transaction的整个生命周期
  4.  * connection的获取延迟到getConnection()方法的调用
  5.  * 忽略所有的commit和rollback操作
  6.  * 默认情况下,可以关闭一个连接connection,也可以配置它不可以关闭一个连接
  7.  * 让容器来管理transaction的整个生命周期
  8.  * @see ManagedTransactionFactory
  9.  */  
  10. /**
  11.  * @author Clinton Begin
  12.  */  
  13. public class ManagedTransaction implements Transaction {  
  14.   
  15. private static final Log log = LogFactory.getLog(ManagedTransaction.class);  
  16.   
  17. private DataSource dataSource;  
  18. private TransactionIsolationLevel level;  
  19. private Connection connection;  
  20. private boolean closeConnection;  
  21.   
  22. public ManagedTransaction(Connection connection, boolean closeConnection) {  
  23. this.connection = connection;  
  24. this.closeConnection = closeConnection;  
  25.   }  
  26.   
  27. public ManagedTransaction(DataSource ds, TransactionIsolationLevel level, boolean closeConnection) {  
  28. this.dataSource = ds;  
  29. this.level = level;  
  30. this.closeConnection = closeConnection;  
  31.   }  
  32.   
  33. public Connection getConnection() throws SQLException {  
  34. if (this.connection == null) {  
  35.       openConnection();  
  36.     }  
  37. return this.connection;  
  38.   }  
  39.   
  40. public void commit() throws SQLException {  
  41. // Does nothing  
  42.   }  
  43.   
  44. public void rollback() throws SQLException {  
  45. // Does nothing  
  46.   }  
  47.   
  48. public void close() throws SQLException {  
  49. if (this.closeConnection && this.connection != null) {  
  50. if (log.isDebugEnabled()) {  
  51. "Closing JDBC Connection [" + this.connection + "]");  
  52.       }  
  53. this.connection.close();  
  54.     }  
  55.   }  
  56.   
  57. protected void openConnection() throws SQLException {  
  58. if (log.isDebugEnabled()) {  
  59. "Opening JDBC Connection");  
  60.     }  
  61. this.connection = this.dataSource.getConnection();  
  62. if (this.level != null) {  
  63. this.connection.setTransactionIsolation(this.level.getLevel());  
  64.     }  
  65.   }  
  66.   
  67. }  



注意:如果我们使用MyBatis构建本地程序,即不是WEB程序,若将type设置成"MANAGED",那么,我们执行的任何update操作,即使我们最后执行了commit操作,数据也不会保留,不会对数据库造成任何影响。因为我们将MyBatis配置成了“MANAGED”,即MyBatis自己不管理事务,而我们又是运行的本地程序,没有事务管理功能,所以对数据库的update操作都是无效的。

《深入理解mybatis原理》 MyBatis事务管理机制

标签:throws,level,Connection,管理机制,mybatis,connection,SQLException,MyBatis,public
From: https://blog.51cto.com/u_15912341/5938357

相关文章

  • 深入理解mybatis原理(一) Mybatis初始化机制详解
     对于任何框架而言,在使用前都要进行一系列的初始化,MyBatis也不例外。本章将通过以下几点详细介绍MyBatis的初始化过程。   1.MyBatis的初始化做了什么   2.MyBati......
  • mybatis-plus的通用Service
    mybatis-plus的通用service,实际上进一步封装了CRUD操作,同时方法的命名区分BaseMapper,也就是可以通过继承通用service,就可以使用一些基本的CRUD操作了。如何使用内在......
  • mybatis注解开发
    mybatis注解开发主要是下面4个注解@Select@Insert@Update@Delete001-@Select@Select("select*fromuser")List<User>findAll();002-@Insert@Insert("inser......
  • mybatis一级缓存
    mybatis一级缓存缓存概念存在于内存中的临时数据为什么要使用缓存使用mybatis缓存,减少和数据库的交互次数提高执行效率缓存的使用范围经常查询并且不经常改变的数据的......
  • mybatis-plus的BaseMapper
    顾名思义,BaseMapper就是基础的mapper,我们可以通过继承BaseMapper来实现基础的CRUD功能而无需再写单独的xml文件,这个对于SQL不复杂的场景和表来说非常的友好。基本的使用......
  • Maven构建spring整合mybatis的项目
    1.使用Maven构建java项目,修改pom.xml文件,添加所需的依赖jar包<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:......
  • mybatis的resultType和resultMap
    resultType作为返回值可以是一个基本类型也可以是实体类对象也就是说是一个具体的类如果我们要返回的对象不是一个具体的类假如我们的实体类的属性和数据库的字段不一......
  • mybatis的连接池
    mybatis的连接池连接池:我们在实际开发中都会使用连接池因为它可以减少我们获取连接所消耗的时间连接池就是用于存储连接的一个容器容器其实就是一个集合对象该集合必须......
  • Mybatisplus自动生成SQL语句变成下划线
    ###Errorqueryingdatabase.Cause:java.sql.SQLSyntaxErrorException:Unknowncolumn'l_o_g_i_n_n_a_m_e'in'fieldlist'###Theerrormayexistincom/rqzx/api......
  • 基于Springboot+Mybatis+mysql+vue宠物领养网站1
    @目录一、系统介绍二、功能展示1.主页(普通用户)2.登陆、注册(普通用户)3.宠物大全(普通用户)4.宠物详情(申请领养、点赞、评论)(普通用户)5.我的申请(普通用户)6.个人信息(普通用户......