1. 程式人生 > 其它 >spring事務控制筆記

spring事務控制筆記

程式設計事務控制相關物件

PlatfromTransactionManager

PlatfromTransactionManager時spring介面是spring的事務管理器,提高具體事務操作方法

方法 說明
TransactionStatus.getTransation(TransactionDefination defination) 獲取事務的狀態資訊
void commit(TransactionStatus status) 提交事務
void rollback(TransactionStatus status) 回滾事務

TransactionDefinition

TransactionDefinition是事務的定義資訊物件,有如下方法

方法 說明
int getIsolationLevel() 獲得事務的隔離級別
int getPropogationBehavior() 獲得事務的傳播行為
int getTimeout() 獲得超時時間
boolean isReadOnly() 是否只讀

TransactionStatus

TransactionStatus介面提供的是事務具體的執行狀態

方法 說明
boolean hasSavepoint() 是否儲存回滾點
boolean isCompleted() 事務是否完成
boolean isNewTransaction() 是否是新事物
boolean isRollbackOnly() 事務是否回滾

基於XML的宣告式事務控制

宣告式事務控制的實現

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
        <property name="user" value="root"/>
        <property name="password" value="y123456"/>
    </bean>

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

    <bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>

<!--    目標物件  內部的方法就是切點-->
    <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>

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

<!--    通知  事務的增強-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--        設定事務的屬性資訊的-->
        <tx:attributes>
            <tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
            <tx:method name="save" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
            <tx:method name="findAll" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
            <tx:method name="update*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="true"/>
            <tx:method name="*"/>
        </tx:attributes>
    </tx:advice>

<!--    配置事務的aop織入-->
    <aop:config>
        <aop:pointcut id="txPointcut" expression="execution(* com.itheima.service.impl.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
    </aop:config>

基於註解的宣告式事務管理

@Service("accountService")
@Transactional(isolation = Isolation.REPEATABLE_READ)
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Transactional(isolation = Isolation.READ_COMMITTED,propagation = Propagation.REQUIRED)
    public void transfer(String outMan, String inMan, double money) {
        accountDao.out(outMan,money);
        int i = 1/0;
        accountDao.in(inMan,money);
    }
}
<!--元件掃描-->
    <context:component-scan base-package="com.itheima"/>

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"/>
        <property name="user" value="root"/>
        <property name="password" value="root"/>
    </bean>

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

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


    <!--事物的註解驅動,註解方式事務控制必加-->
    <tx:annotation-driven transaction-manager="transactionManager"/>