1. What is a transaction
(1) A transaction is the most basic unit of database operations. It is a logical set of operations. Either all of them succeed. If one fails, the operations will all fail.
(2) Typical scenario: bank transfer
2. Four characteristics of transactions (ACID)
(1) Atomicity
(2) Consistency
(3) Isolation
(4) Persistence
3. Transaction operation (build a transaction operation environment)
(1) Create a database table and add records
(2) Create service, build dao, complete object creation and injection relationship
-
Service injects dao, injects JdbcTemplate in dao, injects Datasource in JdbcTemplate
@Service
public class UserService {
// 注入dao
@Autowired
private UserDao userDao;
}
@Repository
public class UserDaoImpl implements UserDao{
@Autowired
private JdbcTemplate jdbcTemplate;
}
(3) Create two methods in dao and one method in service
@Repository
public class UserDaoImpl implements UserDao{
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void addMoney() {
String sql = "update t_account set money=money+? where username=?";
jdbcTemplate.update(sql, 100, "mary");
}
@Override
public void reduceMoney() {
String sql = "update t_account set money=money-? where username=?";
jdbcTemplate.update(sql, 100, "lucy");
}
}
@Service
public class UserService {
// 注入dao
@Autowired
private UserDao userDao;
public void accountMoney(){
userDao.reduceMoney();
userDao.addMoney();
}
}
4. If the above code runs normally, there is no problem, if there is an exception
solve:
Use transactions to resolve.
(1) Open the transaction
(2) Conduct business operations
(3) No exception occurs, submit the transaction
(4) An exception occurs and the transaction is rolled back
Transaction Operation (Introduction to Spring Transaction Management)
1. Transactions are added to the Servie layer (business logic layer) in the JavaEE three-tier structure
2. Perform transaction management operations in Spring
(1) Two ways: programmatic and declarative transaction management
3. Declarative
(1) Annotation-based (using)
(2) Based on the xml configuration file
4. Perform declarative transaction management in spring, and use the aop principle at the bottom
5. Spring transaction management API
(1) Provide an interface to represent the transaction manager, which provides different implementation classes for different frameworks
Transaction operation (annotation declarative transaction management)
1. Configure the transaction manager in the spring configuration file
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入dataSource-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--创建事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"/>
2. In the spring configuration file, enable transaction annotations
(1) Introduce the namespace tx in the spring configuration file
xmlns:tx="http://www.springframework.org/schema/tx" http://www.springframework.org/schema/tx http://www.springframework.org/schema/beans/spring-tx.xsd"
2) Turn on transaction annotations
<!--开启事务注解-->
<tx:annotation-driven transaction-manager="transactionManager"/>
3. Add transaction annotations on the service class (or above the methods in the service class)
@Transactional
-
If you add this annotation to the class, all methods in this class will add transactions
-
If you add this annotation to the method, add a transaction to this method
Transaction operation (declarative transaction management parameter configuration)
1.Propagation(): transaction propagation behavior
2.Isolation () transaction isolation level:
(1) Transactions have a characteristic called isolation, and there will be no impact between multi-transaction operations, and many problems will arise if isolation is not considered.
(2) The problem of three reads: dirty reads, non-repeatable reads, phantom reads
Dirty read: One uncommitted transaction reads data from another uncommitted transaction
Non-repeatable read: One uncommitted transaction reads to another committed transaction to modify data
Phantom read: one uncommitted transaction reads another committed transaction to add data
solve:
Solve the read problem by setting transaction isolation.
3. timeout, timeout
(1) The transaction is submitted within a certain period of time, and if it is not submitted, it will be rolled back
(2) The default value is -1, and the setting time is calculated in seconds
4. readOnly, whether to read only
(1) Read: query operation, write: add modify delete operation
(2) The default value is false, which means that it can be queried, and operations can be added, modified, and deleted
(3) Set to true, only query
5.rollbackFor, rollback
(1) Set which exceptions to query for transaction rollback
6.noRollbackFor: no rollback
(1) Set which exceptions do not perform transaction rollback
Transaction operation (xml declarative transaction management)
1. Configure in the spring configuration file
Step 1: Configure the transaction manager
Part 2: Configure Notifications
Step 3: Configure the entry point and facet
Transactional operations (fully annotated development)
1. Create a configuration class and use the configuration class to replace the xml configuration file
@Configuration
@ComponentScan(basePackages = "com.demo")
@EnableTransactionManagement // 开启事务
public class TxConfig {
// 创建数据库连接池
@Bean
public DruidDataSource getDruidDataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://user_db");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
// 创建jdbcTemplate对象
@Bean
public JdbcTemplate getJdbcTemplate(DataSource dataSource){
JdbcTemplate jdbcTemplate = new JdbcTemplate();
jdbcTemplate.setDataSource(getDruidDataSource());
return jdbcTemplate;
}
// 创建事务管理器
@Bean
public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource){
DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource);
return dataSourceTransactionManager;
}
}