Spring声明式事务处理:通过书写配置文件,Spring帮我们处理事务!
由于使用不同的数据库操作技术,开启事务的方式不一样,但是核心都是从数据源获取链接,然后开启事务。
1、spring处理事务的session和目标方法进行数据库操作用到的session必须保持一致
2、spring中事务和session是绑定在一起,因为session是由当前线程产生的
2、spring中事务和session是绑定在一起,因为session是由当前线程产生的
<
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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"
>
<!-- 读取配置文件 -->
<
bean
class= "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
<property name= "locations">
<value> classpath:jdbc.properties</value >
</property>
</
bean
>
<!-- 根据配置文件,获取数据源 -->
<
bean
id="dataSource" destroy-method="close"
class= "org.apache.commons.dbcp.BasicDataSource" >
<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
>
<!--
1、引入目标类
2、引入切面
-->
<bean id="classesDao" class="cn.itheima03.spring.jdbc.transaction.ClassesDaoImpl" >
<property name= "dataSource">
<ref bean= "dataSource"/>
</property>
</bean >
<bean id="classesService" class="cn.itheima03.spring.jdbc.transaction.ClassesServiceImpl" >
<property name= "classesDao">
<ref bean= "classesDao"/>
</property>
</bean >
<!--声明一个事务管理器,需要传入数据源,传入的数据源与目标类里的数据源是同一个。 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name= "dataSource">
<ref bean= "dataSource"/>
</property>
</bean >
<!--
通知
id 是唯一标示
transaction-manager 事务管理器
-->
<tx:advice id="tx" transaction-manager="transactionManager" >
<tx:attributes>
<!--
propagation 事务的传播属性 默认值REQUIRED
isolation 默认值
read-only
true 只读事务
false 读写事务
-->
<tx:method name= "save*" read-only ="false"/>
<tx:method name= "update*" read-only="false" />
<!--
除了上述情况以外的 qingkuan
-->
<tx:method name= "*" read-only ="true"/>
</tx:attributes>
</tx:advice >
<aop:config >
<aop:pointcut expression="execution(* cn.itheima03.spring.jdbc.transaction.ClassesServiceImpl.*(..))" id ="perform"/>
<aop:advisor advice-ref="tx" pointcut-ref="perform" />
</aop:config >
</
beans
>
|
2.java代码:
public
class ClassesDaoImpl extends JdbcDaoSupport implements ClassesDao{
@Override
public
void saveClasses() {
this.getJdbcTemplate().execute("insert into classes(cname,description) values('aq','as')");
}
}
============================
public
class ClassesServiceImpl implements ClassesService{
private
ClassesDao classesDao;
public
ClassesDao getClassesDao() {
return classesDao ;
}
public
void setClassesDao(ClassesDao classesDao) {
this.classesDao = classesDao;
}
@Override
public
void saveClasses() {
// TODO Auto-generated method stub
this.classesDao .saveClasses();
int a = 1/0;
this.classesDao .saveClasses();
}
}
============================
public
class ClassesDaoTest {
@Test
public
void testSaveClasses(){
ApplicationContext context =
new ClassPathXmlApplicationContext("cn/itheima03/spring/jdbc/transaction/applicationContext.xml" );
ClassesService classesService = (ClassesService)context.getBean(
"classesService"
);
classesService.saveClasses();
}
}
============================
|