0

我正在使用 HibernateTemplate 并且有一个抽象类,我在其中编写了所有插入、删除、更新的东西。不,我有一个服务方法,我必须在两个表中输入 tbl_StudentMaster 和 tbl_StudenDetail ,我需要实现事务管理。我在应用程序上下文中设置了 autocommit false 并且也尝试过使用 org.springframework.transaction.interceptor.TransactionProxyFactoryBean 类,但我仍然无法实现事务。在我的 MVC 控制器中,我正在注入 StudentService。

<bean class="org.apache.commons.dbcp.BasicDataSource" id="dataSource" destroy-method="close" >
    <property name="driverClassName">
        <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
        <value>url</value>
    </property>
    <property name="username">
        <value>root</value>
    </property>
    <property name="password">
        <value>root</value>
    </property>
    <property name="maxActive" value="100"/>
    <property name="maxWait" value="10000"/>
    <property name="maxIdle" value="10"/>
</bean>
<bean class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean" id="sessionFactory">
    <property name="dataSource">
        <ref bean="dataSource"/>
    </property>
    <property name="annotatedClasses">
        <list>
            <value>com.taher.test.model.StudentMaster</value>
            <value>com.taher.test.model.StudentDetail</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="current_session_context_class">thread</prop>
            <!--<prop key="hibernate.hbm2ddl.auto">update</prop>-->
            <prop key="hibernate.cache.use_second_level_cache">true</prop>
            <prop key="hibernate.connection.autocommit">false</prop>
            <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.EhCacheProvider</prop>
            <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
            <prop key="hibernate.max_fetch_depth">5</prop>
            <prop key="hibernate.default_batch_fetch_size">16</prop>
            <prop key="hibernate.jdbc.batch_size">25</prop>
            <prop key="hibernate.jdbc.fetch_size">8</prop>
            <prop key="hibernate.show_sql">false</prop>
            <prop key="hibernate.connection.release_mode">after_statement</prop>
        </props>
    </property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" p:sessionFactory-ref="sessionFactory" />

<bean  class="org.springframework.orm.hibernate3.HibernateTemplate" id="hibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--DAO BEANS-->
<bean name="HibernateQueryDao" class="com.taher.test.dao.HibernateQueryImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean name="StudentMasterDao" class="com.taher.test.dao.StudentMasterImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean name="StudentDetailDao" class="com.taher.test.dao.StudentDetailImpl">
    <property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!--Service BEANS-->

<bean name="wrapedStudentService" class="com.taher.test.service.StudentService" autowire="no">
    <property name="studentMasterDao" ref="StudentMasterDao"/>
    <property name="studentDetailDao" ref="StudentDetailDao"/>
</bean>
<bean id="StudentService" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" abstract="true">
    <property name="transactionManager">
        <ref bean="transactionManager"/>
    </property>
    <property name="target">
        <ref bean="wrapedStudentService"/>
    </property>
    <property name="transactionAttributes">
        <props>
            <prop key="insertStudentDetail">PROPAGATION_REQUIRED,-Exception</prop>
            <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
        </props>
    </property>
</bean>

4

1 回答 1

0

是的,得到了​​解决方案:

我已经删除了 bean 'TransactionProxyFactoryBean' 并将<tx:annotation-driven transaction-manager="transactionManager"/>@Service 添加到我的类和 @Transactional 到我的方法......

于 2012-04-24T06:35:21.863 回答