import demo1.config.TestConfig;
import demo1.service.TransferService;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @ClassName TransferTest
* @Description TODO
* @Author seven
* @Date 2019/12/29 14:51
* @Version 1.0
*/
@Slf4j //记录日志
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfig.class)
public class TransferTest {
private Logger log = (Logger) LoggerFactory.getLogger(TransferTest.class);
/**
* 在test3之后开始使用注解
*/
@Autowired
private TransferService transferService;
/**
* test1没有添加任何事务时,不管有没有抛出异常,都会将数据库的数据修改,即转账成功
*/
@Test
public void test1(){
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(TestConfig.class);
TransferService transferService = ac.getBean(TransferService.class);
try {
transferService.transferMoney("张三","李四",2000);
System.out.println("转账成功");
}catch (Exception ex){
ex.printStackTrace();
System.out.println("转账失败");
}
}
/**
* test2 在TransferServiceImpl#transferMoney()上添加了 @Transactional即使抛出了异常,还是会转账成功
* 但是抛出的时runtimeException或者error,转账会失败
* 事务代码中捕获了异常信息,也是不会回滚的,转账会成功
*/
@Test
public void test2(){
AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(TestConfig.class);
TransferService transferService = ac.getBean(TransferService.class);
try {
transferService.transferMoney("张三","李四",2000);
System.out.println("转账成功");
}catch (Exception ex){
ex.printStackTrace();
System.out.println("转账失败");
}
}
/**
* 在service包下创建一个TransferServiceImplB.java类,将加钱和减钱的方法分开,测试事务
* 在TransferServiceImpl#transferMoney()上添加了 @Transactional(propagation = Propagation.REQUIRED)开启默认事务
* 在TransferServiceImplB#addMoney()上添加了@Transactional(propagation = Propagation.REQUIRED)开启默认事务
* 转账失败会回滚 日志信息如下:
* Creating new transaction with name [demo1.service.TransferServiceImpl.transferMoney]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
* Participating in existing transaction
* Executing prepared SQL update
* Executing prepared SQL statement [update bank_account set money = money + ? where name = ?]
* Executing prepared SQL update
* Executing prepared SQL statement [update bank_account set money = money - ? where name = ?]
* Initiating transaction rollback
*/
@Test
public void test3(){
try {
transferService.transferMoney("张三","李四",2000);
log.info("转账成功");
}catch (Exception ex){
ex.printStackTrace();
System.out.println("转账失败");
}
}
/**
* 在service包下创建一个TransferServiceImplB.java类,将加钱和减钱的方法分开,测试事务
* 在TransferServiceImplB#addMoney()上添加了 @Transactional(propagation = Propagation.REQUIRED)开启默认事务
* 在TransferServiceImplB#subMoney()上添加了 @Transactional(propagation = Propagation.REQUIRES_NEW)新建事务,如果当前已经存在事务,则挂起当前事务
* 转账失败但是数据不会回滚 日志信息如下:
* Creating new transaction with name [demo1.service.TransferServiceImplB.addMoney]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
* Creating new JDBC Driver Connection to [jdbc:mysql://localhost:3306/zj]
* Acquired Connection [com.mysql.jdbc.JDBC4Connection@7e19ebf0] for JDBC transaction
* Switching JDBC Connection [com.mysql.jdbc.JDBC4Connection@7e19ebf0] to manual commit
* Executing prepared SQL update
* Executing prepared SQL statement [update bank_account set money = money + ? where name = ?]
* Initiating transaction commit
* Committing JDBC transaction on Connection [com.mysql.jdbc.JDBC4Connection@7e19ebf0]
* Releasing JDBC Connection [com.mysql.jdbc.JDBC4Connection@7e19ebf0] after transaction
* Creating new transaction with name [demo1.service.TransferServiceImplB.subMoney]: PROPAGATION_REQUIRES_NEW,ISOLATION_DEFAULT
* Creating new JDBC Driver Connection to [jdbc:mysql://localhost:3306/zj]
* Acquired Connection [com.mysql.jdbc.JDBC4Connection@488d1cd7] for JDBC transaction
* Switching JDBC Connection [com.mysql.jdbc.JDBC4Connection@488d1cd7] to manual commit
* Executing prepared SQL update
* Executing prepared SQL statement [update bank_account set money = money - ? where name = ?]
* Initiating transaction commit
*/
@Test
public void test4(){
try {
transferService.transferMoney("张三","李四",2000);
log.info("转账成功");
}catch (Exception ex){
ex.printStackTrace();
System.out.println("转账失败");
}
}
/**
* 在service包下创建一个TransferServiceImplB.java类,将加钱和减钱的方法分开,测试事务
* 在TransferServiceImplB#addMoney()上添加了 @Transactional(propagation = Propagation.REQUIRED)开启默认事务
* 在TransferServiceImplB#subMoney()上添加了 @Transactional(propagation = Propagation.NESTED)如果当前存在事务,则嵌套事务内执行,如果当前没有事务,则与REQUIRED的传播特性相同
* 转账失败但是数据不会回滚 日志信息如下:
* eating new transaction with name [demo1.service.TransferServiceImplB.addMoney]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT
* Creating new JDBC Driver Connection to [jdbc:mysql://localhost:3306/zj]
* Sun Dec 29 22:50:58 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
* Acquired Connection [com.mysql.jdbc.JDBC4Connection@7e19ebf0] for JDBC transaction
* Switching JDBC Connection [com.mysql.jdbc.JDBC4Connection@7e19ebf0] to manual commit
* Executing prepared SQL update
* Executing prepared SQL statement [update bank_account set money = money + ?,update_time = now() where name = ?]
* Initiating transaction commit
* Committing JDBC transaction on Connection [com.mysql.jdbc.JDBC4Connection@7e19ebf0]
* Releasing JDBC Connection [com.mysql.jdbc.JDBC4Connection@7e19ebf0] after transaction
* Creating new transaction with name [demo1.service.TransferServiceImplB.subMoney]: PROPAGATION_NESTED,ISOLATION_DEFAULT
* Creating new JDBC Driver Connection to [jdbc:mysql://localhost:3306/zj]
* Sun Dec 29 22:50:59 CST 2019 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL conn
评论0