使用HibernateTemplate进行事务管理

时间:2012-04-23 16:17:18

标签: spring hibernate spring-transactions

我正在使用HibernateTemplate并且有一个抽象类,我已经编写了所有插入,删除,更新的东西。不是这样我有一个服务方法,我必须在两个表中输入tbl_StudentMaster和tbl_StudenDetail,我需要实现事务管理。我在Application上下文中设置了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>

1 个答案:

答案 0 :(得分:0)

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

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