UP群号38612695
一个探讨框架,设计,以J2EE技术的群.

解决方案 »

  1.   

    getHibernateTemplate().save(c1);
    getHibernateTemplate().save(c2);打开了2个session,所以应该是2个事务。
    Spring事务一般是用在业务逻辑里(service),配置好了不麻烦;业务逻辑里面不用添加事物代码。
      

  2.   

    遇到同样的问题,类似上面如果要同时save  c1和c2要如何处理
      

  3.   

    使用aop代理到transaction manager <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <bean id="clinic" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
    <property name="transactionManager" ref="transactionManager"/>
    <property name="target" ref="clinicTarget"/>
    <property name="transactionAttributes">
    <props>
    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop>
    <prop key="store*">PROPAGATION_REQUIRED</prop>
    </props>
    </property>
    </bean>这也就是说所有方法store,之前spring会自动保证一个事务在开启, 之后事务被关闭,这是spring管理你,不需要程序员关心
    也可以用aspectj样式的aop代理事务。
      

  4.   

    clinic   在哪里引用??
      

  5.   

    <bean id="baseTxProxy" lazy-init="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
            <property name="transactionManager">
                <ref local="transactionManager"/>
            </property>
            <property name="transactionAttributes">
                <props>
                    <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
                    <prop key="save*">PROPAGATION_REQUIRED</prop>
                    <prop key="delete*">PROPAGATION_REQUIRED</prop>
                    <prop key="update*">PROPAGATION_REQUIRED</prop>
                    <prop key="bind*">PROPAGATION_REQUIRED</prop>
                    <prop key="cancel*">PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean> <bean id="StuService" parent="baseTxProxy">
    <property name="target">
    <bean
    class="com.ninetowns.ent.service.StuServiceImpl">
    <property name="objectDao">
    <ref bean="objectDao" />
    </property>
    </bean>
    </property>
    </bean>