通过DataSourceTransactionManager实现Spring事务

通过注解实现

<!-- 配置阿里druid数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="username" value="root"/>
    <property name="password" value="0113"/>
    <property name="url" value="jdbc:mysql://localhost:3306/shiro"/>
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

<!--
spring-tx模块以AOP方式管理spring中的事务;
proxy-target-class设为"true"是基于类的代理;
tx:annotation-driven开启注解式事务声明,在方法上添加@Transactional注解即可开启事务
-->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

在需要使用事务的方法上添加"@Transactional",注解,可以通过"rollbackFor"指定特定异常时才进行回滚操作

@Transactional(rollbackFor = Exception.class)
@Override
public void addPermission(String roleName, Set<String> permissions) throws SQLException{
    String addPermission = "INSERT  INTO shiro_web_roles_permissions (role,permission) VALUES (?,?)";
    //批量添加数据
    jdbcTemplate.batchUpdate(addPermission, new BatchPreparedStatementSetter() {
        @Override
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            ps.setString(1, roleName);
            ps.setString(2, tempPermissions.get(i));
        }


        @Override
        public int getBatchSize() {
            return permissions.size();
        }
    });
}

通过AOP配置全局事务

使用该种方法,可以不用手动添加"@Transactional"注解
maven依赖:

<!-- AOP -->
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.6.12</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.6.12</version>
</dependency>

配置:

<!-- 配置阿里druid数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="username" value="root"/>
    <property name="password" value="0113"/>
    <property name="url" value="jdbc:mysql://localhost:3306/shiro"/>
</bean>

<!-- 配置JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"/>
</bean>

<!-- 配置注解方式使用事务 -->
<tx:annotation-driven proxy-target-class="true"/>

<!-- 配置AOP,配置通知的属性 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="*" propagation="REQUIRED" rollback-for="Exception"/>
    </tx:attributes>
</tx:advice>

<!-- 配置AOP,配置切面 -->
<aop:config proxy-target-class="true">
    <aop:advisor advice-ref="txAdvice" pointcut="within(com.lifeofcoding.shiro.dao.impl..*)"/>
</aop:config>

编程式事务

需要手动commit以及rollback,比以上各种方式更灵活

public void addPermission(String roleName, Set<String> permissions) throws SQLException{
    String addPermission = "INSERT  INTO shiro_web_roles_permissions (role,permission) VALUES (?,?)";
    //声明TransactionDefinition
    DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
    //声明DatasourceTransactionManager
    DataSourceTransactionManager dm = new DataSourceTransactionManager(jdbcTemplate.getDataSource());
    //声明TransactionStatus
    TransactionStatus tmp = dm.getTransaction(transactionDefinition);
    permissions.remove("");
    ArrayList<String> tempPermissions = new ArrayList<>(permissions);
    try {
        //批量添加数据
        jdbcTemplate.batchUpdate(addPermission, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                ps.setString(1, roleName);
                ps.setString(2, tempPermissions.get(i));
            }
            
            @Override
            public int getBatchSize() {
                return permissions.size();
            }
        });
        //手动提交事务
        dm.commit(tmp);
    }catch (Exception e){
        //发生异常时回滚
        dm.rollback(tmp);
        //把异常抛出,让调用方处理
        throw e;
    }
}
posted @ 2020-06-08 12:51  LifeOfCoding  阅读(3836)  评论(0编辑  收藏  举报