Spring數據庫事務管理

Spring數據庫事務管理

Spring中最常用的事務管理器是DataSourceTransactionManager

配置事務管理器

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

Java Config方式配置事務

@Configuration
@ComponentScan(basePackages = "com.spring.transaction.*")
@EnableTransactionManagement
public class JavaConfig implements TransactionManagementConfigurer {

    private DataSource dataSource = null;

    @Bean(name = "dataSource")
    public DataSource initDataSource() {
        if (dataSource != null)
            return dataSource;
        Properties props = new Properties();
        props.setProperty("driverClassName", "com.mysql.jdbc.Driver");
        props.setProperty("url", "jdbc:mysql://localhost:3306/weixin");
        props.setProperty("username", "root");
        props.setProperty("password", "root");
        props.setProperty("maxActive", "200");
        props.setProperty("maxIdle", "20");
        props.setProperty("maxWait", "30000");
        try {
            dataSource = BasicDataSourceFactory.createDataSource(props);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dataSource;
    }

    @Bean(name = "jdbcTemplate")
    public JdbcTemplate initJdbcTemplate() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(initDataSource());
        return jdbcTemplate;
    }

    @Override
    @Bean(name = "transactionManager")
    public PlatformTransactionManager annotationDrivenTransactionManager() {
        DataSourceTransactionManager transactionManager =
                new DataSourceTransactionManager();
        transactionManager.setDataSource(dataSource);
        return transactionManager;
    }
}

使用註解@EnableTransactionManagement後,在Spring上下文中使用事務註解@Transactional,這樣Spring就知道使用該數據庫事務管理器管理事務了。

編程式事務

 public static void main(String[] args) {
    /*編程式事務(不推薦使用)*/
    ClassPathXmlApplicationContext ctx =
            new ClassPathXmlApplicationContext("applicationContext-dao.xml");
    JdbcTemplate jdbcTemplate = ctx.getBean(JdbcTemplate.class);
    TransactionDefinition def = new DefaultTransactionDefinition();
    PlatformTransactionManager transactionManager =
            ctx.getBean(PlatformTransactionManager.class);
    TransactionStatus status = transactionManager.getTransaction(def);
    try {
        jdbcTemplate.update("insert into t_lecture(lecture_name, note) " +
                "values ('Python', 'Xixi')");
        transactionManager.commit(status);
    } catch (Exception e) {
        transactionManager.rollback(status);
        e.printStackTrace();
    }
}

聲明式事務(一種約定型的事務)

<!--配置使用@Transactional配置事務-->
<tx:annotation-driven transaction-manager="transactionManager"/>

XML方式配置事務管理器

<!--配置事務攔截器-->
<bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
    <property name="transactionManager" ref="transactionManager"/>
    <!--Spring IoC啓動時會解析這些內容,放到事務定義類TransactionDefinition中,運行時根據正則匹配程度決定採取的策略-->
    <property name="transactionAttributes">
        <props>
            <prop key="insert*">PROPAGATION_REQUIRED, ISOLATION_READ_UNCOMMITTED</prop>
            <prop key="save*">PROPAGATION_REQUIRED, ISOLATION_READ_UNCOMMITTED</prop>
            <prop key="add*">PROPAGATION_REQUIRED, ISOLATION_READ_UNCOMMITTED</prop>
            <prop key="select*">PROPAGATION_REQUIRED, readOnly</prop>
            <prop key="get*">PROPAGATION_REQUIRED, readOnly</prop>
            <prop key="find*">PROPAGATION_REQUIRED, readOnly</prop>
            <prop key="del*">PROPAGATION_REQUIRED, ISOLATION_READ_UNCOMMITTED</prop>
            <prop key="remove*">PROPAGATION_REQUIRED, ISOLATION_READ_UNCOMMITTED</prop>
            <prop key="update*">PROPAGATION_REQUIRED, ISOLATION_READ_UNCOMMITTED</prop>
        </props>
    </property>
</bean>

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
    <!--指定攔截哪些類-->
    <property name="beanNames">
        <list>
            <value>*ServiceImpl</value>
        </list>
    </property>
    <!--定義事務攔截器-->
    <property name="interceptorNames">
        <list>
            <value>transactionInterceptor</value>
        </list>
    </property>
</bean>

在ServiceImpl上使用註解定義事務屬性

public class RoleServiceImpl implements IRoleService {

    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT
        , timeout = 3)
    public int insertRole(Role role) {
        // 執行業務邏輯...
        return 0;
    }
}

@Transaction的底層實現是基於AOP,而AOP又是基於動態代理的,所以對於靜態方法和非public方法,@Transaction是失效的。

在動態代理中,被代理對象調用了自身的其他方法,而該方法又被@Transaction標註,此時@Transaction失效。因爲在方法內部調用是不會調用代理對象的。解決方法就是從Spring IoC容器中獲取對象(代理對象)。

事務的使用注意:

  1. 由於Service對應的是一個事務,所以在Controller中需要注意多次操作是否是在同一個事務中。
  2. 注意不要過長時間的佔用事務資源。
  3. 錯誤的捕捉異常,導致在約定的事務流程中捕捉不到異常,不會產生回滾。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章