create database day23db ;
use day23db;
create table `account` (
`id` int(8) not null primary key auto_increment,
`name` varchar(64) default null,
`money` double default null
)
insert into account values(null,'jack',1000);
insert into account values(null,'rose',1000);
# 转账
update account set money=money-500 where `name`='jack';
update account set money=money+500 where `name`='rose';
<dependencies>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.46version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.2.9.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.2.9.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.2.9.RELEASEversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.7.2version>
dependency>
dependencies>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestAccountServiceImpl {
@Autowired
IAccountService service ;
@Test
public void test01(){
//业务对象
//IAccountService service = new AccountServiceImpl();
//调用方法 translate(一个人转出,另一个转入,金额)
service.translate("jack","rose",500.00);
}
}
@Service
public class AccountServiceImpl implements IAccountService{
@Autowired
IAccountDao dao ;
@Override
public void translate(String from, String to, double money) {
//创建AccountDaoImpl
//AccountDaoImpl dao = new AccountDaoImpl();
//调用方法
dao.translateOut(from,money);
System.out.println(1/0); //断电
dao.translateIn(to,money);
System.out.println("--translate");
}
}
public interface IAccountService {
//转账本身就是从一个账户减钱,另一个账户加钱
void translate(String from, String to, double money);
}
@Repository
public class AccountDaoImpl implements IAccountDao {
@Autowired
JdbcTemplate jdbcTemplate;
@Override
public void update(String account, double v) {
System.out.println(account+" "+v);
jdbcTemplate.update("update account set money=money+? where `name`=?",v,account);
}
@Override
public void translateOut(String account, double v) {
jdbcTemplate.update("update account set money=money-? where `name`=?",v,account);
}
@Override
public void translateIn(String account, double v) {
jdbcTemplate.update("update account set money=money+? where `name`=?",v,account);
}
}
public interface IAccountDao {
@Deprecated
void update(String account, double v);
void translateOut(String account, double v);
void translateIn(String account, double v);
}
<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"
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">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/day23db"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg name="dataSource" ref="dataSource"/>
bean>
<context:component-scan base-package="com.wzx"/>
<aop:aspectj-autoproxy/>
beans>
AccountServiceImpl中 transfer 方法中 int i = 1/0; 发现问题,转出成功. 转入失败,没有收到事务控制,数据前后不一致.
解决方式是使用spring基于xml的事务管理.
步骤:
一.在bean.xml中配置3个内容
1.配置平台事务管理器(注入dataSource)
2.编写一个tx:Advice 设置事务的增强
3.配置aop (配置切点, 切点和增强的组合);
因为要使用tx标签
<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/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/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
bean>
<tx:advice id="adviceId" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="translate" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false" timeout="-1" />
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut id="translate" expression="execution(* com.wzx.service.AccountServiceImpl.translate(..))"/>
<aop:advisor advice-ref="adviceId" pointcut-ref="translate"/>
aop:config>
beans>