Spring-事务

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方式的事务实现
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Java123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值