Spring-transcation

本文深入探讨了Spring框架中三种不同的事务管理配置方式:基于XML的声明式事务、半注解配置以及全注解配置。详细解析了每种配置下如何设置事务属性,如隔离级别、传播行为等,并通过具体代码示例展示了如何在业务逻辑中实现转账操作的事务控制。

表结构

目录结构

1.基于xml的声明式事务,如下(applicationContext.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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
    	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
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!-- 组件扫描 -->
    <context:component-scan base-package="com.alibaba.dao.impl"></context:component-scan>
    <!-- 加载jdbc,properties -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!-- 配置Jdbc模板对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 配置Dao对象 -->
    <bean id="accountDao" class="com.alibaba.dao.impl.AccountDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    <!-- 配置service对象 -->
    <bean id="accountService" class="com.alibaba.service.impl.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"></property>
    </bean>
    <!-- 配置平台事务管理器 -->
    <bean id="transcationManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 配置事务的Aop切面 -->
    <aop:config>
        <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.alibaba.service.impl.*.*(..))"/>
    </aop:config>
    <!-- 配置事务增强 -->
    <tx:advice id="txAdvice" transaction-manager="transcationManager">
        <!-- 事务属性配置-->
        <tx:attributes>
            <!-- name:方法名称  *代表全部
                 isolation:隔离级别
                 propagation:传播行为
                 read-only: 是否只读 -->
            <tx:method name="*" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
            <tx:method name="findAll" read-only="true"/>
            <tx:method name="find*" read-only="true"/>
        </tx:attributes>
    </tx:advice>
</beans>

2.半注解配置

   1).applicationContext.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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
    	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
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 加载jdbc.properties文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!-- 配置数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

    <!-- 配置jdbc模版对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 配置平台事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!-- 组件扫描 -->
    <context:component-scan base-package="com.alibaba"></context:component-scan>

    <!-- 事务的注解驱动 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

2).service层的配置

@Service("accountService")
/*
    @Transactional 可以作用于接口、接口方法、类以及类方法上。当作用于类上时,该类的所有 public 方法将都具有该类型的事务属性,
    同时,我们也可以在方法级别使用该标注来覆盖类级别的定义。虽然 @Transactional 注解可以作用于接口、接口方法、类以及类方法上,
    但是 Spring 建议不要在接口或者接口方法上使用该注解,因为这只有在使用基于接口的代理时它才会生效。另外, @Transactional
    注解应该只被应用到 public 方法上,这是由 Spring AOP 的本质决定的。如果你在 protected、private 或者默认可见性的方法上
    使用 @Transactional 注解,这将被忽略,也不会抛出任何异常。默认情况下,只有来自外部的方法调用才会被AOP代理捕获,也就是
    类内部方法调用本类内部的其他方法并不会引起事务行为,即使被调用方法使用@Transactional注解进行修饰。
*/
@Transactional
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;
    @Override
//    @Transactional(isolation = Isolation.DEFAULT, propagation=Propagation.REQUIRED, readOnly = false)
    public void transfer(String outMan, String inMan, Double money) {
        int i = 1 / 0;
        //转出钱
        accountDao.out(outMan, money);
        //转入钱
        accountDao.in(inMan, money);
    }
}

3.全注解配置

   1).applicationContext.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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
    	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
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 加载jdbc.properties文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>

	<!-- 配置数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"></property>
		<property name="jdbcUrl" value="${jdbc.url}"></property>
		<property name="user" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>

	<!-- 配置jdbc模版对象 -->
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 配置平台事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 组件扫描 -->	
	<context:component-scan base-package="com.alibaba"></context:component-scan>
	
	<!-- 事务的注解驱动 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>

   3).添加配置事务类

@Configuration
//<context:component-scan base-package="com.alibaba"></context:component-scan>
@ComponentScan("com.alibaba")
//<tx:annotation-driven transaction-manager="transactionManager"/>
@EnableTransactionManagement
public class SpringConfiguration {
	
	@Bean(name="dataSource")
	public DataSource getDataSource() throws PropertyVetoException{
		ComboPooledDataSource dataSource = new ComboPooledDataSource();
		dataSource.setDriverClass("com.mysql.jdbc.Driver");
		dataSource.setJdbcUrl("jdbc:mysql:///testBase");
		dataSource.setUser("root");
		dataSource.setPassword("root");
		return dataSource;
	}
	
	
	@Bean(name="jdbcTemplate")
	public JdbcTemplate getJdbcTemplate(@Qualifier("dataSource") DataSource dataSource) {
		JdbcTemplate jdbcTemplate = new JdbcTemplate();
		jdbcTemplate.setDataSource(dataSource);
		return jdbcTemplate;
	}
	
	@Bean(name="transactionManager")
	public DataSourceTransactionManager getDataSourceTransactionManager(@Qualifier("dataSource") DataSource dataSource) {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(dataSource);
		return transactionManager;
	}
	

}

   3).测试类

	@Test
	//测试转账的效果(全注解方式)
	public void test(){
		ApplicationContext app = new AnnotationConfigApplicationContext(SpringConfiguration.class);
		AccountService accountService = (AccountService) app.getBean("accountService");
		accountService.transfer("jack", "tom", 500);
	}
	

 

在 IT 领域中,“Transaction”通常指事务,是一个重要的概念,在数据库和应用程序监控等方面都有体现。 在数据库中,事务是作为单个逻辑工作单元执行的一系列操作,具有原子性、一致性、隔离性和持久性(ACID)特性。原子性确保事务中的所有操作要么全部成功,要么全部失败;一致性保证事务在执行前后,数据库的状态保持一致;隔离性使多个事务可以并发执行而互不干扰;持久性则保证一旦事务提交,其结果将永久保存。 在应用程序监控方面,如 Elastic APM 中,事务是指应用程序中用户请求到响应的整个过程。通过监控事务的持续时间(如 `transaction.duration.us` 字段),可以分析应用程序的性能。在 Elastic APM 与 Elastic ML 结合进行异常检测时,会按 `transaction.type` 拆分并按 `service.name` 分区对事务持续时间进行分析,以发现事务性能的异常情况。例如,当特定事务类型的异常分数超过 75 时,会在事务持续时间图表中进行彩色注释,方便分析师定位问题 [^1]。 ```python # 以下是一个简单的 Python 示例,模拟数据库事务操作 import sqlite3 # 连接到数据库 conn = sqlite3.connect('example.db') cursor = conn.cursor() try: # 开始事务 conn.execute('BEGIN') # 执行一些操作 cursor.execute('INSERT INTO users (name, age) VALUES (?, ?)', ('John', 30)) cursor.execute('UPDATE accounts SET balance = balance - 100 WHERE user_id = 1') # 提交事务 conn.execute('COMMIT') print('Transaction committed successfully.') except Exception as e: # 回滚事务 conn.execute('ROLLBACK') print(f'Transaction rolled back due to error: {e}') # 关闭连接 conn.close() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值