2012-04-23 59 views
0

我正在使用HibernateTemplate并拥有一个抽象类,我已经写入了所有插入,删除,更新内容。没有的情况是我有一个服务方法,我必须在两个表中输入条目说tbl_StudentMaster和tbl_StudenDetail,我需要实现事务管理。我已经在应用程序上下文中设置了autocommit false,并且还尝试过使用org.springframework.transaction.interceptor.TransactionProxyFactoryBean类,但是我仍然无法实现事务。而在我的MVC控制器中,我正在注入StudentService。使用HibernateTemplate进行事务管理

<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> 

+0

有你注释你的方法是@Transactional? – verhage 2012-04-23 16:30:23

+0

你能发布你的ApplicationContext.xml吗? – yorkw 2012-04-24 02:47:28

+0

@verhage我必须添加TransactionProxyFactoryBean并且注释Transactional也是?? – TaherT 2012-04-24 05:03:28

回答

0

呀得到了解决:

我已经删除豆 '​​TransactionProxyFactoryBean来',并添加<tx:annotation-driven transaction-manager="transactionManager"/>和@Service上我的课和@Transactional我的方法...