在Spring中集成Hibernate事务

本文介绍如何在Spring框架中配置Hibernate事务管理。通过配置applicationContext.xml文件,设置数据源、SessionFactory及事务管理器等组件,实现对数据库操作的事务控制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Spring中集成Hibernate事务非常简单,只需配置一下spring的applicationContext.xml文件,配置文件的主体部分如下:
<!-- 配置数据源 -->
<bean id="dataSource"
  class="org.apache.commons.dbcp.BasicDataSource"
  destroy-method="close">
  <property name="driverClassName"
   value="${jdbc.driverClassName}" />
  <property name="url" value="${jdbc.url}" />
  <property name="username" value="${jdbc.username}" />
  <property name="password" value="${jdbc.password}" />
 </bean>
 <!-- 配置sessionFactory  -->
 <bean id="sessionFactory"
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
  <property name="lobHandler">
   <ref local="defaultLobHandler" />
  </property>
  <property name="dataSource">
   <ref bean="dataSource" />
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">
     ${hibernate.dialect}
    </prop>
    <prop key="hibernate.show_sql">
     ${hibernate.show_sql}
    </prop>
    <prop key="hibernate.jdbc.batch_size">
     ${hibernate.jdbc.batch_size}
    </prop>
    <prop key="hibernate.query.factory_class">
     ${hibernate.query.factory_class}
    </prop>
    <prop key="hibernate.cache.provider_class">
     ${hibernate.cache.provider_class}
    </prop>
    <prop key="hibernate.cache.use_query_cache">
     ${hibernate.cache.use_query_cache}
    </prop>
   </props>
  </property>
 
<!-- 配置从HibernateDaoSupport派生自定义的子类HibernateBookDao  -->
  <bean id="HibernateBookDao"
  class="com.dao.HibernateBookDao">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>

 <!-- Transaction manager for a Hibernate DataSource-->
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>

 <!-- the transactional advice for privilege service -->
 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <!-- the transactional semantics... -->
  <tx:attributes>
   <!-- all methods starting with 'get' or 'find' are read-only -->
   <tx:method name="get*" read-only="true" />
   <tx:method name="find*" read-only="true" />
   <!-- all methods starting with 'update' or 'insert' or 'delete' will use the default transaction settings (see below) -->
   <tx:method name="update*" />
   <tx:method name="insert*" />
   <tx:method name="save*" />
   <tx:method name="delete*" />
  </tx:attributes>
 </tx:advice>

 <!-- ensure that the above transactional advice runs for any execution
  of an operation defined by the Service interface. All service object end with
  BO and under package com.hacongz. will match the pointcut
  
  声明的事务对象必须是接口,而不是实现
 -->
 <aop:config>
  <aop:pointcut id="serviceOperation"
   expression="execution(* com.hacongz..*Impl.*(..))" />
  <aop:advisor advice-ref="txAdvice"
   pointcut-ref="serviceOperation" />
 </aop:config>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值