栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java学习 --- spring5的事务操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java学习 --- spring5的事务操作

一、什么事务 

事务是数据库操作最基本单元,逻辑上一组操作,要么都成功,如果有一个失败所有操作都失败 

二、Spring事务管理介绍

1、事务添加到JavaEE三层结构里面Service层(业务逻辑层)

 2、在Spring进行事务管理操作

(1)有两种方式:编程式事务管理和声明式事务管理(使用)

 3、声明式事务管理

(1)基于注解方式(使用)

public class Account {
    private String id;
    private String name;
    private Integer money;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getMoney() {
        return money;
    }

    public void setMoney(Integer money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id='" + id + ''' +
                ", name='" + name + ''' +
                ", money=" + money +
                '}';
    }
}
public interface AccountDao {

    void addMoney();

    void reduceMoney();
}
@Repository
public class AccountDaoImpl implements AccountDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void addMoney() {
        String sql = "update t_account set money=money+? where name=?";
        jdbcTemplate.update(sql, 100, "tom");

    }

    @Override
    public void reduceMoney() {
        String sql = "update t_account set money=money-? where name=?";
        jdbcTemplate.update(sql, 100, "jack");
    }
}
@Service
@Transactional
public class AccountService {
    @Autowired
    private AccountDao accountDao;

    
    public void accountMoney(){
            //转出
            accountDao.reduceMoney();
            //出现异常
            int a = 10 /0;
            //转入
            accountDao.addMoney();
    }
}



    
        
        
        
        
    
    
    
        
        
    
    
    
    
    
        
        
    
    
    

(2)基于xml配置文件方式  




    
        
        
        
        
    
    
    
        
        
    
    
    
    
    
        
        
    
   
    
       
        
            
        
    
    

    
    
    
    

(3)完全注解方式

@Configuration
@ComponentScan(basePackages = "com.cjc")
@EnableTransactionManagement
public class AccountConfig {
    //创建数据库链接池
    @Bean
    public DruidDataSource druidDataSource(){
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://localhost:3307/demo");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("123456");
        return druidDataSource;
    }
    //创建JdbcTemplate对象
    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }
    //创建事务管理器
    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource){
        DataSourceTransactionManager dataSourceTransactionManager = new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource);
        return dataSourceTransactionManager;
    }
}
  @Test
    public void Test02(){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AccountConfig.class);
        AccountService accountService = context.getBean("accountService", AccountService.class);
        accountService.accountMoney();
    }
}

4、在Spring进行声明式事务管理,底层使用AOP原理  

5、Spring事务管理API

(1)提供一个接口,代表事务管理器,这个接口针对不同的框架提供不同的实现类 

6、 事务隔离级别

1)事务有特性成为隔离性,多事务操作之间不会产生影响。不考虑隔离性产生很多问题

(2)有三个读问题:脏读、不可重复读、虚(幻)读

(3)脏读:一个未提交事务读取到另一个未提交事务的数据

 7、timeout:超时时间

(1)事务需要在一定时间内进行提交,如果不提交进行回滚

(2)默认值是 -1 ,设置时间以秒单位进行计算 

5、readOnly:是否只读

(1)读:查询操作,写:添加修改删除操作

(2)readOnly默认值false,表示可以查询,可以添加修改删除操作

(3)设置readOnly值是true,设置成true之后,只能查询

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/770490.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号