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