如何理解spring的声明式事务管理?大家发表下意见吧。

解决方案 »

  1.   

    一般在spring配置文件中是这样配置的:<!-- spring事物管理 -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="find*" read-only="true"/>
    <tx:method name="get*" read-only="true"/>
    <tx:method name="save*" propagation="REQUIRED"/>
    <tx:method name="update*" propagation="REQUIRED"/>
    <tx:method name="add*" propagation="REQUIRED"/>
    <tx:method name="del*" propagation="REQUIRED"/>
    <tx:method name="Read*" propagation="REQUIRED"/>
    </tx:attributes>
    </tx:advice>
    <aop:config>
    <aop:pointcut id="allManagerMethod" expression="execution(* com.yeezoo..service.*.*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
    </aop:config> 
      

  2.   

    我来纠正下2,3楼,  (4楼太长, 就不看了)声明式事务(Declarative Transaction):
    注意看下面这个例子, 类名和方法名前面都有个@Transactional, 用这种方式来声明事务的方式, 叫声明式事务. (类似的例子, EJB的SessionBean也是用这种方法)@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
    public class BankBusinessServiceImpl extends FlatServiceBase implements BankBusinessService {
    private BankAccountDao bankAccountDao; public void setBankAccountDao(BankAccountDao bankAccountDao) {
    this.bankAccountDao = bankAccountDao;
    } @Transactional(readOnly = true)
    public List<BankAccount> getAllBankAccount() {
    return bankAccountDao.findAll();
    } @Transactional(readOnly = true)
    public BankAccount getBankAccount(String accountId) {
    return bankAccountDao.findById(accountId);
    } @Transactional(readOnly = true)
    public boolean checkAccount(String accountId, String password, String userName) {
    BankAccount bankAccount = bankAccountDao.findById(accountId);
    if (bankAccount == null)
    return false;
    else {
    if (bankAccount.getPassword().equals(password) && bankAccount.getUserName().equals(userName))
    return true;
    else
    return false;
    }
    } public double fetchMoney(BankAccount bankAccount, double money) {
    double currentBalance = bankAccount.getBalance();
    if (money < 0)
    throw new DreamException("Can't transfer money.", "Invalid amount.");
    if (currentBalance < money)
    throw new DreamException("Can't fetch money.", "Not enough balance."); bankAccount.setBalance(currentBalance - money);
    bankAccountDao.update(bankAccount);
    return bankAccount.getBalance();
    } public double saveMoney(BankAccount bankAccount, double money) {
    double currentBalance = bankAccount.getBalance();
    bankAccount.setBalance(currentBalance + money);
    if (money < 0)
    throw new DreamException("Can't transfer money.", "Invalid amount."); bankAccountDao.update(bankAccount);
    return bankAccount.getBalance();
    } public void transferMoney(BankAccount from, BankAccount to, double money) {
    double fromBalance = from.getBalance();
    if (money < 0)
    throw new DreamException("Can't transfer money.", "Invalid amount.");
    if (fromBalance < money)
    throw new DreamException("Can't transfer money.", "Not enough balance."); fetchMoney(from, money);
    saveMoney(to, money);
    }
    }
    面向切面的编程 (AOP)
    2,3楼举的是面向切面的事务. 
      

  3.   

    看了下相关资料,自己总结总结。
    Spring事务处理分两种:
    一。编程式事务:在程序中控制事务开始,执行和提交;
      1.1 使用TransactionTemplate, 使用回调函数执行事务,不需要显示开始事务,不需要显示提交事务,但是出现异常时需要手动回滚;开始、执行和提交事务的过程在模板中定义好了;
      1.2 使用PlatformTransactionManager,代码量要比使用TransactionTemplate大,需要在程序总中使用TransactionDefinetion和TransactionStatus对象显示开始、提交和回滚事务。
    二。声明式事务:在Spring配置文件中对事务进行配置,无须在程序中写代码;
        我对”声明式“的理解是这样的:Spring配置文件中定义好了这样一个规则,这个规则可以指定对哪些类的哪些方法在执行的时候添加事务控制,并配置好了事务的相关执行属性,就是在这些类的这些方法执行的时候隐式地添加事务开始、执行、提交或回滚的代码(当然我们看不到),然后在程序运行的时候就得执行这个规则,这是Spring提前声明好的,必须照办,不照办不行。
        Spring事务代理:Spring为每个加了事务控制的类或方法生成一个代理,用这个代理来控制事务;
        这么理解不知道对不对,请大家指正。  2.1 声明式事务代理:这种方法配置代理时代码较多,比较繁琐;每个对象需要两段配置;
      2.2 根据beanname自动生成事务代理,这种方法用到一个BeanNameAutoNameCreator,即一个生成器,生成代理的规则用一个Interceptor来控制,对满足条件的bean自动生成事务代理;这种配置相对比较简单;
      2.3 根据使用继承来简化事务代理:就是定义一个超级类,配置好相关事务属性,配置其他需要事务控制的bean时只需继承下这个超级类即可。
          我们在项目中就是用的2.3方法来处理事务。上面是我的总结,大家如果还有大家如果还有要补充或指正的尽管说,明天结贴给分。