日期:2014-05-18  浏览次数:20712 次

为何spring声明式事务无作用?
为何spring声明式事务无作用?


//为何log中没有spring 动态代理的信息,有事务回滚信息?数据库中还是更新了test表中的字段

Java code

@Repository
public class BaseHibernateDao extends HibernateDaoSupport {
}


SERVICE
Java code

@Service
public class TestService extends BaseService {
    @Autowired
    TestDao testDao;

    public void testTT(){
        testDao.testTT();
   }

}



DAO

Java code

@Service
public class TestDAO extends BaseHibernateDao{


    public void testTT(){
        String hql = " update Test set age= ?  where id = 1000 ";
        Query q = this.getSession(true).createQuery(hql);
        q.setInteger(0, 3333333);
        q.executeUpdate();        
        
    
        Test n = new Test();
        n.setAddtime( new Date() );
        n.age( new Long(101) );
        n.setDatastr("22222222222");
        n.setPid( Long.parseLong("sssssssssssss") );
                //此处故意throw RuntimeException子类
        
        session.save(n);       
  //为何log中没有spring 动态代理的信息,有事务回滚信息?数据库中还是更新了test表中的字段
   }

}





------解决方案--------------------
spring-hibernate.xml 
没有看到你把SessionFactory
注入
因为HibernateDaoSupport需要他才可以把事物注入上

------解决方案--------------------
<!-- ========================= TABLE T_BZ_JS_Criterion_Demand_Info: CONFIGURE ========================= -->
<bean id="bzJsCriterionDemandInfoDAO" class="com.excellence.gzby.standard.dao.BzJsCriterionDemandInfoDAO" singleton="true" init-method="initialize">
<property name="dataSource">
<ref local="dataSource"/>
</property>
<property name="sqlMapClient">
<ref local="sqlMapClient"/>
</property>
</bean>
<bean id="bzJsCriterionDemandInfoBll" class="com.excellence.gzby.standard.bll.BzJsCriterionDemandInfoBll">
<property name="_dao">
<ref local="bzJsCriterionDemandInfoDAO"/>
</property>
</bean>
<bean id="bzJsCriterionDemandInfoBllProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager"/>
</property>
<property name="target">
<ref local="bzJsCriterionDemandInfoBll"/>
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED,-Throwable</prop>
<prop key="deleteAll*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
------解决方案--------------------
1 在spring-hibernate.xml 中最上面加上<context:component-scan base-package="*" />
2 规范一下注释吧,把BaseHibernateDao上的注释去掉
建议用junit测试,试试吧
------解决方案--------------------
sping配置文件:<context:component-scan base-package="com.nes.platform.s