Spring-事务
Spring事务管理的3个核心接口
PlatformTransactionmanager 管理事务
getTransaction 获取事务状态
commit 提交事务
rollback 回滚事务
### TransactionDefinition 事务规则
- String getName 获取事务隔离级别
- int getlsolationLevel 获取事务隔离级别
int getPropagationBehavior 获取传播行为
int getTimeout 获取事务超时时间
boolean isReadOnly 获取事务是否可读
TransactionStatus 事务的状态
- flush 刷新事务
- hashSavepoint() 是否存在保存点
- isComleted() 是否完成
- isNewTransaction 获取是够是新事物
- isRollbackOnly 获取是否会滚
- setRollbackOnly 设置事务回滚
Spring事务管理的两种方式
- 编程式事务管理
- 声明式事务管理
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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 数据库 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring_transaction?characterEncoding=UTF-8" />
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<!-- JDBC模板 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- bean对象 -->
<bean id="accountDao" class="com.ruanyuan.dao.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务执行细节 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- aop -->
<aop:config>
<aop:pointcut expression="execution(* com.ruanyuan.dao.*.*(..))" id="txPointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
</beans>
public class AccountDaoImpl implements AccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public void transfer(String outUser, String inUser, Double money) {
// TODO Auto-generated method stub
jdbcTemplate.update("update customer set balance = balance + ? "+"where username = ? ",money,inUser);
int i = 10/0;
jdbcTemplate.update("update customer set balance = balance - ? "+"where username = ? ",money,outUser);
}
}
public class Test {
public static void main(String[] args) {
String xmlPath="applicationContext.xml";
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
AccountDao accountDao =(AccountDao) applicationContext.getBean("accountDao");
System.out.println("================================");
accountDao.transfer("张三","李四",100.0);
System.out.println("转账成功!");
}
}
.out.println("================================");
accountDao.transfer(“张三”,“李四”,100.0);
System.out.println(“转账成功!”);
}
}
## Annotation方式的事务实现