最近看《Spring实战》的书,每看一章写一个简单的例子记录一下,这次是Spring事务实现,我在Spring配置文档里面通过Spring的Aop切面编程,给业务逻辑的所有方法配置了事务,使用默认的隔离和传播机制,具体是什么机制后续作答。然后测试业务逻辑方法如果遇到运行期异常是否会回滚sql语句,通过测试都符合预期。
Dao接口
package com.dao;
import com.common.Person;
public interface IPerson {
public boolean insertPerson(Person person);
}
Dao实现类
package com.dao.impl;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.common.Person;
import com.dao.IPerson;
public class PersonImpl extends SqlMapClientDaoSupport implements IPerson{
@Override
public boolean insertPerson(Person person) {
this.getSqlMapClientTemplate().insert("insertPerson",person);
return false;
}
}
业务接口
package com.service;
import com.common.Person;
public interface IPersonService {
public void testTX1(Person person);
public void testTX2(Person person) throws Exception;
public void testTX3(Person person) throws Exception;
}
业务实现类
package com.service.impl;
import com.common.Person;
import com.dao.IPerson;
import com.service.IPersonService;
public class PersonServiceImpl implements IPersonService{
IPerson personDao;
public void setPersonDao(IPerson personDao) {
this.personDao = personDao;
}
@Override
public void testTX1(Person person){
personDao.insertPerson(person);
}
//测试遇到运行时异常是否RollBack
@Override
public void testTX2(Person person) throws Exception{
personDao.insertPerson(person);
throw new Exception();
}
//测试被testTX3里面的方法是否在一个事务
@Override
public void testTX3(Person person) throws Exception {
this.testTX1(person);
this.testTX2(person);
}
}
Spring配置文档【applicationContext.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@10.11.115.51:1521:orcl" />
<property name="username" value="CQGT0325" />
<property name="password" value="1" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:config/sqlmap/sqlMapConfig.xml</value>
</property>
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<bean id="currentTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
<tx:advice id="txAdvice" transaction-manager="currentTransactionManager">
<tx:attributes>
<tx:method name="*" rollback-for="Exception" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="daoImpl"
expression="execution(* com.service.impl.*Impl.*(..))" />
<aop:advisor pointcut-ref="daoImpl" advice-ref="txAdvice" />
</aop:config>
<bean id="personDao" class="com.dao.impl.PersonImpl">
<property name="sqlMapClient">
<ref local="sqlMapClient"/>
</property>
</bean>
<bean id="personService" class="com.service.impl.PersonServiceImpl">
<property name="personDao">
<ref local="personDao"/>
</property>
</bean>
</beans>
ibatis文档配置【sqlMapConfig.xml】
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<settings cacheModelsEnabled="false" enhancementEnabled="true"
lazyLoadingEnabled="false" errorTracingEnabled="true"
useStatementNamespaces="true" />
<sqlMap resource="config/sqlmap/person.xml" />
</sqlMapConfig>
具体ibatis配置【person.xml】
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap>
<typeAlias alias="person" type="com.common.Person" />
<insert id="insertPerson" parameterClass="person">
<![CDATA[
insert into person values (#id#,#name#)
]]>
</insert>
</sqlMap>
测试类
package test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.common.Person;
import com.service.IPersonService;
public class TestSpringTran {
public static void main(String[] args) throws Exception {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:config/spring/applicationContext.xml");
Person person = new Person("1","xuxh");
IPersonService personService = (IPersonService) context.getBean("personService");
//这个方法会抛出异常,然后sql语句回滚
personService.testTX3(person);
}
}
代码结构图
依赖包