SQL语言分类 DDL(数据定义语言)create drop alter DML(数据操纵语言)insert update delete select * TCL(事务控制语言)commit rollback DCL(数据控制语言)grant revoke
spring事务管理
1、声明事务管理器(将数据源委托给spring提供的平台同一管理)
<!--配置事务管理器--> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--注入数据源委托事务管理平台管理事务--> <property name="dataSource" ref="dataSource"></property> </bean>
2、使用注解方式实现事务管理
a、spring配置文件添加头部分
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
b、添加使用注解实现事务配置
<!--使用注解方式实现事务管理--> <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
c、在需要添加事务的业务方法上加上@Transactional
@Transactional public void zz(){ Bank b1 = this.bankDAO.get("lifan"); Bank b2 = this.bankDAO.get("zhangsan"); b1.setMoney(b1.getMoney()-500); b2.setMoney(b2.getMoney()+500); this.bankDAO.update(b1); String s = null; s.charAt(0); this.bankDAO.update(b2); }
a、定义事务管理通知
<tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="find*" read-only="true"/> <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/> </tx:attributes> </tx:advice>
b、配置aop实现事务 织入
<aop:config> <!--切入点--> <aop:pointcut id="txPointcut" expression="execution(public * com.web.service.*.*(..))"/> <!--通知--> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> </aop:config>