Spring事务配置

    Spring3.0+Hibernate3+mysql5.0 采用Tx,Aop配置事务。默认情况Spring只认Runexception,但是我们可以通过在Tx中配置rollback-for="Exception"来改变捕捉的异常。另外网上有看到帖子说事务配置失败,原因是mysql数据库表的引擎是MyISAM,将引擎修改为InnoDB后就成功了,我特意将引擎改为MyISAM试了下,结果事务控制依然成功。

    我的配置方式如下

 

一、datasource

 

 <!-- 配置数据源 -->
 <bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  <property name="url" value="jdbc:mysql://localhost/ga4jdb?useUnicode=true&amp;characterEncoding=utf-8"></property>
  <property name="username" value="root"></property>
  <property name="password" value="123"></property>
  <property name="maxActive" value="20" />
  <property name="maxIdle" value="10" />
  <property name="minIdle" value="3" />
  <property name="maxWait" value="500" />
  <property name="initialSize" value="5" />
 </bean>


 

二、hibernate

 

 <context:annotation-config />
 <context:component-scan base-package="com.jifan" />
 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
  <property name="dataSource">
   <ref bean="datasource" />
  </property>
  <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    <prop key="hibernate.show_sql">false</prop>
    <prop key="hibernate.format_sql">true</prop>
    <prop key="hibernate.myeclipse.connection.profile">mysql</prop>
   </props>
  </property>
  <property name="mappingResources">
   <list>
    <value>com/jifan/bo/tlog/Tlog.hbm.xml</value>
    <value>com/jifan/bo/tuser/Tuser.hbm.xml</value>
   </list>
  </property>
 </bean>

 

三、transaction

 

 <!--Hibernate TransactionManager-->
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <property name="dataSource" ref="datasource" />
 </bean>

 <tx:advice id="txAdvice" transaction-manager="transactionManager">
  <tx:attributes>
   <tx:method name="get*" read-only="true" />
   <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
   <tx:method name="save*" propagation="REQUIRED" rollback-for="Exception" />
   <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" />
   <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" />
   <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception" />
   <tx:method name="*" propagation="SUPPORTS" read-only="true" rollback-for="Exception" />
   <tx:method name="*" rollback-for="Exception" />
  </tx:attributes>
 </tx:advice>


 <aop:config>
  <aop:pointcut id="serviceOp" expression="execution(* com.company.ucc.*.*(..))" />
  <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOp" />
 </aop:config>
 

四、ucc。这里的ucc是在service上又加的一层,ucc里不做任何处理直接调用service。加这一层的目的是为了方便事务控制。

 

 <bean id="customerUCC" class="com.jifan.ucc.impl.CustomerUCC">
  <property name="service">
   <ref bean="service"/>
  </property>
 </bean>

五、service


 <bean id="service"  class="com.jifan.service.impl.ServiceImpl">
  <property name="logdao">
   <ref bean="logdao" />
  </property>
  <property name="userdao">
   <ref bean="userdao" />
  </property>
 </bean>

 

六、dao

 

<bean id="logdao" class="com.jifan.dao.impl.LogDaoImpl">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>
 <bean id="userdao" class="com.jifan.dao.impl.UserDaoImpl">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>

 

 

 *xml文件头部声明如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值