一、事务隔离的级别(isolation = Isolation.READ_COMMITTED)
最常用的值isolation = Isolation.READ_COMMITTED,读已提交
二、事务异常回滚
三、数据库只读属性,readOnly = false;(true),此方法只会读取数据库数据而不能更改数据
四、timeout=10,单位是秒,只连接等待时间
package top.biglin.spring.tx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Chiawei
* @date 2019/1/15 11:28
*/
@Service("bookShopService")
public class BookShopServiceImpl implements BookShopService {
@Autowired
private BookShopDao bookShopDao;
/**
*添加事务,Propagation.REQUIRED,使用调用原来的事务
* Propagation.REQUIRES_NEW 新开一个事务
*/
@Transactional(propagation = Propagation.REQUIRES_NEW,
isolation = Isolation.READ_COMMITTED,
noRollbackFor = {UserAccountException.class},//对哪些异常不回滚
rollbackFor = {top.biglin.spring.tx.UserAccountException.class},
timeout = 5,
readOnly = false)//对哪些异常进行回滚
@Override
public void purchase(String username, String isbn) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
}
//获取书的单价
int price = bookShopDao.findBookPriceByIsbn(isbn);
//更新书的库存
bookShopDao.updateBookStock(isbn);
//更新余额
bookShopDao.updateUserAccount(username, price);
}
}
