日期:2014-05-19  浏览次数:20703 次

spring 事务问题
我在spring中配置了事务 配置如下
Java code

       <!--定义Hibernate的事务管理器HibernateTransactionManager -->
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <!-- 依赖注入上面定义的sessionFactory -->
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!--定义Spring的事务拦截器TransactionInterceptor -->
    <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <!--  依赖注入上面定义的事务管理器transactionManager -->
        <property name="transactionManager" ref="transactionManager"/>
          <!-- 定义需要进行事务拦截的方法及所采用的事务控制类型 -->        
        <property name="transactionAttributes">            
            <props>
                <!-- 以browse、list、load、get及is开头的所有方法采用只读型事务控制类型 -->
                <prop key="browse*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                <prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>
                <!-- 所有方法均进行事务控制,如果当前没有事务,则新建一个事务 -->
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
    
    <!-- 定义BeanNameAutoProxyCreatorf进行Spring的事务处理-->
    <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <!--  针对指定的bean自动生成业务代理 -->
        <property name="beanNames"> 
            <list>
                <value>testtransactionService</value>
            </list>
        </property>
        <!--  这个属性为true时,表示被代理的是目标类本身而不是目标类的接口 -->
        <property name="proxyTargetClass">
            <value>true</value>
        </property>
       <!--  依赖注入上面定义的事务拦截器transactionInterceptor -->
        <property name="interceptorNames">
            <list>
                <value>transactionInterceptor</value> 
            </list>
        </property>
    </bean>    




这里我声明了 <value>testtransactionService</value>注入事务 但是我这里调用了dao中的两个方法 第二个会抛出异常但是第一个还是插入成功了
public class TesttransactionServiceimp implements TesttransactionService{


private TesttransactionDao testtransactionDao;

public void Testtransaction() {
try {
testtransactionDao.save1();
testtransactionDao.save2();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

------解决方案--------------------
<value>TesttransactionService</value>

跟大小写有关系么,或者注入它的实现类试试呢。
<value>TesttransactionServiceimp</value>