使用spring框架實(shí)現(xiàn)數(shù)據(jù)庫(kù)事務(wù)處理方式
使用spring框架實(shí)現(xiàn)數(shù)據(jù)庫(kù)事務(wù)處理
事務(wù)對(duì)于數(shù)據(jù)庫(kù)來(lái)說(shuō)是,是對(duì)sql語(yǔ)句的一系列操作,這些操作被組織成為一個(gè)事務(wù)。事務(wù)具有原子性的,要么全部執(zhí)行,要么不執(zhí)行。若事務(wù)中sql語(yǔ)句發(fā)生錯(cuò)誤,事務(wù)需要對(duì)已經(jīng)執(zhí)行的sql進(jìn)行回滾操作,撤銷先前對(duì)數(shù)據(jù)庫(kù)的操作,防止數(shù)據(jù)庫(kù)出現(xiàn)錯(cuò)誤狀態(tài)。
JDBC對(duì)數(shù)據(jù)庫(kù)事務(wù)處理的支持
jdbc本身提供了對(duì)數(shù)據(jù)庫(kù)處理的支持,使用java.sql.Connection對(duì)象完成事務(wù)的提交。使用Connection提交數(shù)據(jù)庫(kù)事務(wù)處理如下:
發(fā)現(xiàn)里面有一個(gè)方法叫setAutoCommit(),設(shè)置為true時(shí),就是自動(dòng)提交sql,如果設(shè)置為false時(shí),sql語(yǔ)句的提交由程序負(fù)責(zé),應(yīng)用程序必須調(diào)用Commit方法,同時(shí)在執(zhí)行sql語(yǔ)句異常處理塊中調(diào)用rollback方法,對(duì)異常發(fā)生前進(jìn)行的數(shù)據(jù)庫(kù)進(jìn)行回滾。
在企業(yè)級(jí)中,事務(wù)一般都是并發(fā)執(zhí)行的,當(dāng)事務(wù)并發(fā)執(zhí)行時(shí),就會(huì)發(fā)生數(shù)據(jù)庫(kù)同步的問(wèn)題,具體問(wèn)題可分為下面四種類型:
- 臟讀:
- 不可重復(fù)讀:
- 丟失更新:
- 幻讀:
為了解決上面的四種問(wèn)題,解決事務(wù)并發(fā)的問(wèn)題
JDBC定義了五種事務(wù)隔離級(jí)別來(lái)解決這些并發(fā)導(dǎo)致的問(wèn)題
transaction_read_uncommitted
:數(shù)據(jù)級(jí)別最低,俗稱臟讀,在沒(méi)有提交數(shù)據(jù)時(shí),就已經(jīng)讀取到已經(jīng)更新的數(shù)據(jù)。transaction_read_committed
:當(dāng)一個(gè)事務(wù)進(jìn)行查詢時(shí),允許讀取提交前的數(shù)據(jù),數(shù)據(jù)提交后,當(dāng)前查詢就可以讀取到數(shù)據(jù),update數(shù)據(jù)的時(shí)候并不鎖住表。(最長(zhǎng)使用)transaction_repeatable_read
:當(dāng)一個(gè)事務(wù)進(jìn)行查詢時(shí),不允許讀取其他事務(wù)更新的數(shù)據(jù),可以讀取其他事務(wù)新增的數(shù)據(jù)。transaction_serializable
:當(dāng)一個(gè)事務(wù)進(jìn)行查詢時(shí),不允許任何對(duì)這個(gè)事務(wù)進(jìn)行查詢,就是把并行改成了串行。不提倡使用。
在spring框架中調(diào)用一個(gè)數(shù)據(jù)庫(kù)事務(wù)處理分三步走:
- 第一步就是配置數(shù)據(jù)源就是配置數(shù)據(jù)庫(kù)的名稱和密碼,地址等。
- 第二步就是聲明事務(wù)管理TransactionManagerspring框架提供了PlatformTransactionManager作為事務(wù)管理類的頂層接口,聲明了初始化事務(wù),提交事務(wù),回滾事務(wù)等接口。接口實(shí)現(xiàn)由具體的數(shù)據(jù)庫(kù)驅(qū)動(dòng)類實(shí)現(xiàn)。具體包括DataTransactionManager等實(shí)現(xiàn)類。但是主要還是使用的是DataTransactionManager類。
- 定義可以執(zhí)行事務(wù)的DAO類DAO提供了應(yīng)用程序訪問(wèn)數(shù)據(jù)源必要的接口和方法。
spring 事務(wù)實(shí)現(xiàn)方式有哪些
編程式事務(wù)管理,
在代碼中調(diào)用 commit()、rollback()等事務(wù)管理相關(guān)的方法
maven pom.xml文件
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.4.RELEASE</version> </dependency> <!-- mysql驅(qū)動(dòng) --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.18</version> </dependency>
編程式事務(wù)管理,可以通過(guò) java.sql.Connection 控制事務(wù)。spring 配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="driver" class="com.mysql.jdbc.Driver"></bean> <bean id="datasource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <constructor-arg index="0" name="driver" ref="driver" /> <constructor-arg index="1"> <value>jdbc:mysql://localhost:3306/test</value> </constructor-arg> <constructor-arg index="2"> <value>root</value> </constructor-arg> <constructor-arg index="3"> <value>root</value> </constructor-arg> </bean> </beans>
測(cè)試代碼
package constxiong.interview.transaction; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import javax.sql.DataSource; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TransactionTest { public static void main(String[] args) throws Exception { testManualTransaction();//測(cè)試函數(shù)式控制事務(wù) } private static void testManualTransaction() throws SQLException { ApplicationContext context = new ClassPathXmlApplicationContext("spring_transaction.xml"); DataSource ds = (DataSource)context.getBean("datasource"); Connection conn = ds.getConnection(); try { initTable(conn);//初始化表 conn.setAutoCommit(false);//設(shè)置不自動(dòng)提交事務(wù) queryUsers(conn);//查詢打印用戶表 deleteUser(conn);//刪除 id=1 用戶 conn.rollback();//回滾 queryUsers(conn);//查詢打印用戶表 } finally { conn.close(); } } private static void initTable(Connection conn) throws SQLException { conn.createStatement().execute("drop table if exists user"); conn.createStatement().execute("create table user(id int, username varchar(60)) ENGINE=InnoDB DEFAULT CHARSET=utf8 ");//是否支持事務(wù)與數(shù)據(jù)庫(kù)引擎有關(guān),此處刪除 ENGINE=InnoDB DEFAULT CHARSET=utf8 可能不支持事務(wù) conn.createStatement().execute("insert into user values(1, 'user1')"); conn.createStatement().execute("insert into user values(2, 'user2')"); } private static void deleteUser(Connection conn) throws SQLException { conn.createStatement().execute("delete from user where id = 1"); } private static void queryUsers(Connection conn) throws SQLException { Statement st = conn.createStatement(); st.execute("select * from user"); ResultSet rs = st.getResultSet(); while (rs.next()) { System.out.print(rs.getString("id")); System.out.print(" "); System.out.print(rs.getString("username")); System.out.println(); } } }
刪除用戶語(yǔ)句回滾,打印出兩個(gè)用戶
1 user1
2 user2
1 user1
2 user2
TransactionProxyFactoryBean 的聲明式事務(wù)管理
新增 UserDao 接口
package constxiong.interview.transaction; import java.util.List; import java.util.Map; public interface UserDao { /** * 查詢用戶 * @return */ public List<Map<String, Object>> getUsers(); /** * 刪除用戶 * @param id * @return */ public int deleteUser(int id); }
新增 UserDao 實(shí)現(xiàn)
package constxiong.interview.transaction; import java.util.List; import java.util.Map; import org.springframework.jdbc.core.support.JdbcDaoSupport; public class UserDaoImpl extends JdbcDaoSupport implements UserDao { /** * 查詢用戶 * @return */ public List<Map<String, Object>> getUsers() { String sql = "select * from user"; return this.getJdbcTemplate().queryForList(sql); } /** * 刪除用戶 * @param id * @return */ public int deleteUser(int id){ String sql = "delete from user where id = " + id; int result = this.getJdbcTemplate().update(sql); if (id == 1) { throw new RuntimeException(); } return result; } }
修改 spring 配置文件,添加事務(wù)管理器 DataSourceTransactionManager 和事務(wù)代理類 TransactionProxyFactoryBean
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="driver" class="com.mysql.jdbc.Driver"></bean> <bean id="datasource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <constructor-arg index="0" name="driver" ref="driver" /> <constructor-arg index="1"> <value>jdbc:mysql://localhost:3306/test</value> </constructor-arg> <constructor-arg index="2"> <value>root</value> </constructor-arg> <constructor-arg index="3"> <value>root</value> </constructor-arg> </bean> <bean id="userDao" class="constxiong.interview.transaction.UserDaoImpl"> <property name="dataSource" ref="datasource"></property> </bean> <!-- 事務(wù)管理器 --> <bean id="tracnsactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="datasource"></property> </bean> <bean id="userProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager" ref="tracnsactionManager"></property> <property name="target" ref="userDao"></property> <property name="transactionAttributes"> <props> <!-- 主要 key 是方法 ISOLATION_DEFAULT 事務(wù)的隔離級(jí)別 PROPAGATION_REQUIRED 傳播行為 --> <!-- -Exception 表示發(fā)生指定異?;貪L,+Exception 表示發(fā)生指定異常提交 --> <prop key="deleteUser">-java.lang.RuntimeException</prop> </props> </property> </bean> </beans>
測(cè)試代碼
package constxiong.interview.transaction; import java.util.Map; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TransactionTest { static ApplicationContext context = new ClassPathXmlApplicationContext("spring_transaction.xml"); public static void main(String[] args) throws Exception { testUseTransactionProxy(); //測(cè)試使用 spring TransactionProxyFactoryBean } private static void testUseTransactionProxy() { final UserDao userDao = (UserDao)context.getBean("userProxy"); printUsers(userDao);//打印用戶 userDao.deleteUser(1);//刪除 id=1 用戶 } private static void printUsers(UserDao userDao) { for (Map<String, Object> user : userDao.getUsers()) { System.out.println(user); } } }
結(jié)果輸出
{id=1, username=user1}
{id=2, username=user2}
Exception in thread "main" java.lang.RuntimeException
at constxiong.interview.transaction.UserDaoImpl.deleteUser(UserDaoImpl.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy3.deleteUser(Unknown Source)
at constxiong.interview.transaction.TransactionTest.testUseTransactionProxy(TransactionTest.java:32)
at constxiong.interview.transaction.TransactionTest.main(TransactionTest.java:13)
注解 @Transactional 的聲明式事務(wù)管理
UserDaoImpl 刪除用戶方法添加注解 @Transactional(rollbackFor=RuntimeException.class) 出現(xiàn) RuntimeException 回滾
package constxiong.interview.transaction; import java.util.List; import java.util.Map; import org.springframework.jdbc.core.support.JdbcDaoSupport; import org.springframework.transaction.annotation.Transactional; public class UserDaoImpl extends JdbcDaoSupport implements UserDao { /** * 查詢用戶 * @return */ public List<Map<String, Object>> getUsers() { String sql = "select * from user"; return this.getJdbcTemplate().queryForList(sql); } /** * 刪除用戶 * @param id * @return */ @Transactional(rollbackFor=RuntimeException.class) public int deleteUser(int id){ String sql = "delete from user where id = " + id; int result = this.getJdbcTemplate().update(sql); if (id == 1) { throw new RuntimeException(); } return result; } }
修改 spring 配置文件,開(kāi)啟 spring 的事務(wù)注解能力
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="driver" class="com.mysql.jdbc.Driver"></bean> <bean id="datasource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <constructor-arg index="0" name="driver" ref="driver" /> <constructor-arg index="1"> <value>jdbc:mysql://localhost:3306/test</value> </constructor-arg> <constructor-arg index="2"> <value>root</value> </constructor-arg> <constructor-arg index="3"> <value>root</value> </constructor-arg> </bean> <bean id="userDao" class="constxiong.interview.transaction.UserDaoImpl"> <property name="dataSource" ref="datasource"></property> </bean> <!-- 事務(wù)管理器 --> <bean id="tracnsactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="datasource"></property> </bean> <!-- 啟用事務(wù)注解 --> <tx:annotation-driven transaction-manager="tracnsactionManager"/> </beans>
測(cè)試代碼
package constxiong.interview.transaction; import java.util.Map; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TransactionTest { static ApplicationContext context = new ClassPathXmlApplicationContext("spring_transaction.xml"); public static void main(String[] args) throws Exception { testAnnotationTransaction(); } private static void testAnnotationTransaction() { UserDao userDao = (UserDao)context.getBean("userDao"); printUsers(userDao); userDao.deleteUser(1); } private static void printUsers(UserDao userDao) { for (Map<String, Object> user : userDao.getUsers()) { System.out.println(user); } } }
輸出結(jié)果
{id=1, username=user1}
{id=2, username=user2}
Exception in thread "main" java.lang.RuntimeException
at constxiong.interview.transaction.UserDaoImpl.deleteUser(UserDaoImpl.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy5.deleteUser(Unknown Source)
at constxiong.interview.transaction.TransactionTest.testAnnotationTransaction(TransactionTest.java:20)
at constxiong.interview.transaction.TransactionTest.main(TransactionTest.java:13)
Aspectj AOP 配置(注解)事務(wù)
maven pom.xml 添加 Aspectj 的支持
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.13</version> </dependency>
去除 UserDaoImpl 注解@Transactional(rollbackFor=RuntimeException.class)
package constxiong.interview.transaction; import java.util.List; import java.util.Map; import org.springframework.jdbc.core.support.JdbcDaoSupport; public class UserDaoImpl extends JdbcDaoSupport implements UserDao { /** * 查詢用戶 * @return */ public List<Map<String, Object>> getUsers() { String sql = "select * from user"; return this.getJdbcTemplate().queryForList(sql); } /** * 刪除用戶 * @param id * @return */ public int deleteUser(int id){ String sql = "delete from user where id = " + id; int result = this.getJdbcTemplate().update(sql); if (id == 1) { throw new RuntimeException(); } return result; } }
修改 spring 配置文件,織入切面
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <bean id="driver" class="com.mysql.jdbc.Driver"></bean> <bean id="datasource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource"> <constructor-arg index="0" name="driver" ref="driver" /> <constructor-arg index="1"> <value>jdbc:mysql://localhost:3306/test</value> </constructor-arg> <constructor-arg index="2"> <value>root</value> </constructor-arg> <constructor-arg index="3"> <value>root</value> </constructor-arg> </bean> <bean id="userDao" class="constxiong.interview.transaction.UserDaoImpl"> <property name="dataSource" ref="datasource"></property> </bean> <!-- 事務(wù)管理器 --> <bean id="tracnsactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="datasource"></property> </bean> <tx:advice id="txAdvice" transaction-manager="tracnsactionManager"> <tx:attributes> <!-- 為連接點(diǎn)指定事務(wù)屬性 --> <tx:method name="deleteUser" rollback-for="java.lang.RuntimeException"/> </tx:attributes> </tx:advice> <aop:config> <!-- 切入點(diǎn)配置 --> <aop:pointcut id="point" expression="execution(* *constxiong.interview.transaction.UserDao.deleteUser(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="point"/> </aop:config> </beans>
測(cè)試代碼
package constxiong.interview.transaction; import java.util.Map; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class TransactionTest { static ApplicationContext context = new ClassPathXmlApplicationContext("spring_transaction.xml"); public static void main(String[] args) throws Exception { testAspectjTransaction(); } private static void testAspectjTransaction() { UserDao userDao = (UserDao)context.getBean("userDao"); printUsers(userDao); userDao.deleteUser(1); } private static void printUsers(UserDao userDao) { for (Map<String, Object> user : userDao.getUsers()) { System.out.println(user); } } }
輸出結(jié)果
{id=1, username=user1}
{id=2, username=user2}
Exception in thread "main" java.lang.RuntimeException
at constxiong.interview.transaction.UserDaoImpl.deleteUser(UserDaoImpl.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
at com.sun.proxy.$Proxy2.deleteUser(Unknown Source)
at constxiong.interview.transaction.TransactionTest.testAnnotationTransaction(TransactionTest.java:20)
at constxiong.interview.transaction.TransactionTest.main(TransactionTest.java:13)
PS:
這篇僅用事務(wù)回滾為例,了解 spring 事務(wù)控制,還需要關(guān)注數(shù)據(jù)庫(kù)的ACID四種特性、事務(wù)傳播特性、事務(wù)的隔離級(jí)別(臟讀、不可重復(fù)讀、幻讀)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java Eclipse進(jìn)行斷點(diǎn)調(diào)試的方法
本篇文章主要介紹了Java Eclipse進(jìn)行斷點(diǎn)調(diào)試的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11Java 設(shè)計(jì)模式之責(zé)任鏈模式及異步責(zé)任鏈詳解
顧名思義,責(zé)任鏈模式(Chain of Responsibility Pattern)為請(qǐng)求創(chuàng)建了一個(gè)接收者對(duì)象的鏈。這種模式給予請(qǐng)求的類型,對(duì)請(qǐng)求的發(fā)送者和接收者進(jìn)行解耦。這種類型的設(shè)計(jì)模式屬于行為型模式2021-11-11IDEA2023.3.4開(kāi)啟SpringBoot項(xiàng)目的熱部署(圖文)
本文使用的開(kāi)發(fā)工具是idea,使用的是springboot框架開(kāi)發(fā)的項(xiàng)目,配置熱部署,可以提高開(kāi)發(fā)效率,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02@PropertySource 無(wú)法讀取配置文件的屬性值解決方案
這篇文章主要介紹了@PropertySource 無(wú)法讀取配置文件的屬性值解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06eclipse怎么引入spring boot項(xiàng)目插件的方法
這篇文章主要介紹了eclipse怎么引入spring boot項(xiàng)目插件的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06java利用pdfbox+poi往pdf插入數(shù)據(jù)
這篇文章主要給大家介紹了關(guān)于java利用pdfbox+poi如何往pdf插入數(shù)據(jù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02SpringBoot+Mybatis Plus導(dǎo)致PageHelper失效的解決方法
在Springboot項(xiàng)目中使用分頁(yè)插件的時(shí)候,發(fā)現(xiàn)PageHelper插件失效了 ,本文主要介紹了SpringBoot+Mybatis Plus導(dǎo)致PageHelper失效的解決方法,感興趣的可以了解一下2024-07-07通過(guò)Java修改游戲存檔的實(shí)現(xiàn)思路
這篇文章主要介紹了通過(guò)Java修改游戲存檔的實(shí)現(xiàn)思路,實(shí)現(xiàn)方法也很簡(jiǎn)單,因?yàn)橹参锎髴?zhàn)僵尸游戲的數(shù)據(jù)文件存儲(chǔ)在本地的存儲(chǔ)位置是已知的,因此我們可以將實(shí)現(xiàn)過(guò)程拆分為三個(gè)步驟,需要的朋友可以參考下2021-10-10Spring?Boot統(tǒng)一接口返回及全局異常處理
這篇文章主要介紹了Spring?Boot統(tǒng)一接口返回及全局異常處理,文章圍繞主題展開(kāi)相關(guān)資料,具有一定的參考價(jià)值需要的小伙伴可以參考一下2022-04-04