【Spring七】JDBC编程之声明式事务处理
来源:互联网 发布:珍宝岛自卫反击战知乎 编辑:程序博客网 时间:2024/05/20 23:34
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();
}
}
============================
0 0
- 【Spring七】JDBC编程之声明式事务处理
- Spring的事务处理之声明式事务处理
- JDBC编程之事务处理
- JDBC编程之事务处理
- spring声明式事务处理(使用jdbc操作数据库)
- spring04 数据库操作 spring+jdbc/hibernate 及其声明式事务处理
- Spring 声明式事务处理
- spring 声明式事务处理
- 《Spring》------声明式事务处理
- Spring声明式事务处理
- Spring 的声明式事务处理
- Spring声明式事务处理理解
- Spring的声明式事务处理
- Spring学习之声明式事务处理实例(DriverManagerDataSource注入)
- Spring学习心得(22)-- spring声明式事务处理(使用jdbc操作数据库)
- Spring——声明式的事务处理
- spring声明式事务处理(hibernate)
- spring jdbc 声明式事务
- unity3d5.0 SurvivalShooter(四)碰撞层的创建
- shell中curl 用户详解
- 【算法渣渣的逆袭之路】Property Distribution Aizu - 0118
- 编码GBK造成的问题
- 使用pushstatus 实现 ajax页面seo 优化
- 【Spring七】JDBC编程之声明式事务处理
- 常用MySQL命令
- Java中==和equals的区别,equals和hashCode的区别
- css的一些注意事项
- duilib中添加自定义控件之后怎么能够在xml文件中配置使用
- 使用jersey(2.5.1)搭建java REST服务
- java FileReader与FileWriter 代码笔记
- IOS tableView 选中时的背景颜色设置
- 利用java类的反射机制,实现了 映射 获得post(get)的参数 的BaseServlet