配置声明式事务,在代码里就不用管事务了(代码简洁了点)
getHibernateTemplate跟spring事务没关系,只不过HibernateTemplate又对session做了封装
不再用手动关闭session,也不再用从SessionFactory中拿session
还有:比如你在以一个UserManager里面需要保存一个user,可能你之前的做法是先得到session,然后session.save(user);
又有一个需求,在保存user的时候顺便记录下user的操作,在UserManager代码里面可能还要假如下面:
LogManager logManager = new LogManager();
Log log = new Log();
log.setType("DELETE");
log.setUser(user);
log.setDate(new Date());
logManager.save(log);
然而LogManager里的save方法也是需要session的,我们要求是user与log要同时保存成功或失败
但你根本没办法保证UserManager与LogManager里的session是同一个session(当然用SessionFactory的getCurrentSession()方法也是可以的),所以可能就出错了
用getHibernateTemplate就能保证session不会丢失,其实本质还是session.save(),可以看源代码去
说明一下:要是用SessionFactory的getCurrentSession()的话还要在hibernate.cfg.xml加上<!--本地事务-->
<property name="hibernate.current_session_context_class">thread</property>
<!--JTA用下面这个,我不懂这个--->
<property name="hibernate.current_session_context_class">jta</property>总之:我觉得getHibernateTemplate()经常是跟spring的声明式事务一起使用让人觉得它跟事务有关,其实就是为了让我们的一些操作简单化而已,
也就是simplifies Hibernate data access
我认识只能到这里了,希望有高人继续,呵呵

解决方案 »

  1.   

    首先多謝樓上,講地很明白。
    我剛才又去試了試,重新試了下,但是這個事務不回滾啊,要不就是我的測試方法有問題。
    applicationContext.xml<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>

      <bean id="transactionInterceptor"
            class="org.springframework.transaction.interceptor.TransactionInterceptor">
            <property name="transactionManager" ref="transactionManager" />
            <property name="transactionAttributes">
                <props>
                    <prop key="save*">PROPAGATION_REQUIRED</prop>
                    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
                    <prop key="*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>
        <bean
            class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
            <property name="beanNames">
                <list>
                    <value>userService</value>
                </list>
              </property>
            <property name="interceptorNames">
                <list>
                    <value>transactionInterceptor</value>
                </list>
            </property>
        </bean> <bean id="userDAO" class="kohisa.cn.dao.impl.UserDAO">
    <property name="sessionFactory">
    <ref bean="sessionFactory"/>
    </property>
    </bean>

    <bean id="userService" class="kohisa.cn.service.impl.UserService" >
    <property name="userDAO">
    <ref bean="userDAO"/>
    </property>
    </bean>
    <bean id="myDwr" class="kohisa.cn.dwr.MyDwr">
    <property name="userService">
    <ref bean="userService"/>
    </property>
    </bean>
    UserService類
    public class UserService implements IUserService {
    public UserService(){}
    private IUserDAO userDAO;
    public IUserDAO getUserDAO() {
    return userDAO;
    }
    public void setUserDAO(IUserDAO userDAO) {
    this.userDAO = userDAO;
    }
    public void save(Users users){
    userDAO.save(users);
    userDAO.save(null);我讓這地方傳個空出錯,然後上邊那條記錄好回滾。。不知道這樣對不。
    }
    }
    UserDAO類
    public class UserDAO extends HibernateDaoSupport implements IUserDAO {
    public UserDAO(){}
    public void save(Users user){
    getHibernateTemplate().save(user);
    }
    }
    main方法
    public static void main(String args[]) throws UnsupportedEncodingException{
    IUserService userService = (IUserService)ApplicationContextUtil.getApplicationContext().getBean("userService");
                    Users users = new Users();
                    users.setName("abc");
    userService.save(users);
    }總是有一條記錄插了進去,不回滾啊。
    不知道我上面的配置是不是有問題,還是測試方法有問題,請達人指點啊能測試回滾的方法。。
      

  2.   

    我靠,原來不回滾是我表的問題,改成innodb就好了..
      

  3.   

    会不会是别的save方法也启用事务了呢?我也学spring不久,不是很能看明白你的配置文件,因为现在应该用<aop:config>这种方式了
    给你改了一下(搞不好spring2以后才可以用,如果你的版本太低就别往下看了)<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 配置sessionFactory -->
    <bean name="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation">
    <value>classpath:hibernate.cfg.xml</value>
    </property>
    </bean> <!-- 事务管理器 -->
    <bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
    <ref bean="sessionFactory" />
    </property>
    </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
    <tx:method name="save*" propagation="REQUIRED" />
    <tx:method name="del*" propagation="REQUIRED" />
    <tx:method name="modify*" propagation="REQUIRED" />
    <tx:method name="*" read-only="true" />
    </tx:attributes>
    </tx:advice> <aop:config>
    <aop:pointcut id="allServiceMethod"
    expression="execution(* kohisa.cn.service.impl.UserService .*(..))" />
    <aop:advisor advice-ref="txAdvice"
    pointcut-ref="allServiceMethod" />
    </aop:config>        <bean id="userDAO" class="kohisa.cn.dao.impl.UserDAO">
                <property name="sessionFactory">
                    <ref bean="sessionFactory"/>
                </property>
            </bean>
        
        <bean id="userService" class="kohisa.cn.service.impl.UserService" >
            <property name="userDAO">
                <ref bean="userDAO"/>
            </property>
        </bean>
        <bean id="myDwr" class="kohisa.cn.dwr.MyDwr">
            <property name="userService">
                <ref bean="userService"/>
            </property>
        </bean>
    </beans>