事物的447:
1.4大特性:
原子性,一致性,持久性,隔离性
2.事务的隔离级别:
ISOLATION_DEFAULT
ISOLATION_READ_UNCOMMITTED
ISOLATION_READ_COMMITTED
ISOLATION_REPEATABLE_READ
ISOLATION_SERIALIZABLE
下面是一个示例:
基于maven项目的pom.xml配置
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.1.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
bean
Account.java
package com.bean;
import java.io.Serializable;
public class Account implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private int balance;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public int getBalance() {
return balance;
}
public void setBalance(int balance) {
this.balance = balance;
}
@Override
public String toString() {
return "Account [id=" + id + ", balance=" + balance + "]";
}
}
dao
AccountDao.java
package com.dao;
public interface AccountDao {
/**
* 转出
* @param outId 转出账号
* @param money 转出金额
*/
public void transOut(Integer outId, int money);
/**
* 转入
* @param inId 转入账号
* @param money 转入金额
*/
public void transIn(Integer inId, int money);
}
AccountDaoImpl.java
package com.dao.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.dao.AccountDao;
@Repository
public class AccountDaoImpl implements AccountDao {
@Autowired
private JdbcTemplate template;
@Override
public void transOut(Integer outId, int money) {
template.update("UPDATE tb_account SET balance=balance-? WHERE id=?", money, outId);
}
@Override
public void transIn(Integer inId, int money) {
template.update("UPDATE tb_account SET balance=balance+? WHERE id=?", money, inId);
}
}
AccountService.java
package com.service;
public interface AccountService {
/**
* 转账
* @param outId 转出账号
* @param inId 转入账号
* @param money 金额
*/
public void trans(Integer outId, Integer inId, int money);
}
AccountServiceImpl.java
package com.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dao.AccountDao;
import com.service.AccountService;
/**
* @Transactional是事务注解,它可以标注在类上,也可标注在方法上。
* 如果是标注在类上表示该类中所有方法都需要事务,
* 如果是标注在方法上,则表示该方法可能需要事务。
*/
@Service
@Transactional
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
/**
* 转账
* @param outId 转出账号
* @param inId 转入账号
* @param money 金额
*/
public void trans(Integer outId, Integer inId, int money) {
accountDao.transOut(outId, money);
//int a = 1/0;
//System.out.println(a);
accountDao.transIn(inId, money);
}
/*
@Transactional(propagation=Propagation.SUPPORTS, readOnly=true)//表示不用事务管理器
public Object get() {
}
*/
}
beans.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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
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">
<!-- 配置连接属性 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置IoC解析器 -->
<context:component-scan base-package="com" />
<!-- DI解析器 -->
<context:annotation-config />
<!-- 配置DataSource -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<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>
<!-- JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置事务解析器 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
db.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
AccountServiceTest.java
package com.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import com.service.AccountService;
@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:beans.xml")
public class AccountServiceTest {
@Autowired
private AccountService service;
@Test
public void testTrans() {
service.trans(1000, 1001, 1000);
}
}