-
事务作用:在数据层保证一系列的数据库操作同成功同失败
-
Spring事务作用:在数据层或者业务层保障一系列的数据库操作同成功同失败
-
接口:PlatfromTransactionManager
-
实现类:DataSourceTransactionManager (底层是jdbc,和spring一样)
-
Spring 事务角色
-
事务管理员:在业务层开启的事务,其它的事务都加入这个事务中,统一完成事务,做到了同成功同失败
-
事务协调员:就是被加入的事务,指的是数据层的方法,也可以是业务层的方法。
-
config
package com.yang.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
import org.springframework.transaction.annotation.EnableTransactionManagement;
jdbcConfig
package com.yang.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import javax.sql.DataSource;
public class JdbcConfig {
service
package com.yang.service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
package com.yang.service;
import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
//可以加方法上;也可以加接口上,表示所有的方法都开始起事务
AccountServiceImpl.java
package com.yang.service.Impl;
import com.yang.dao.AccountDao;
import com.yang.service.AccountService;
import com.yang.service.LogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
test
package com.yang;
import com.yang.config.SpringConfig;
import com.yang.service.AccountService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.io.IOException;