@Transactional在@Autowired适用时不起作用



我编写了一个示例应用程序来测试Spring(@Transactional(中的注释事务管理。

上下文文件;

<?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"
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-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<tx:annotation-driven />
<context:component-scan base-package="com.test" /> 
<bean id="playerService" class="com.test.service.PlayerServiceImpl" />
<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>
<bean id="dbUtil" class="com.test.util.DbUtil" init-method="initialize">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<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>
<context:property-placeholder location="jdbc.properties"/>

玩家道豆

import com.test.model.Player;
import com.test.model.Team;
public interface PlayerDao {
public void insertPlayer(Player player);
public void insertTeam(Team team);
}

玩家道普尔的实现

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.test.model.Player;
import com.test.model.Team;
@Repository
public class PlayerDaoImpl implements PlayerDao{ 
private JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Transactional (propagation=Propagation.REQUIRED)
public void insertPlayer(Player player){
String insertSql ="INSERT INTO PLAYERS (PLAYER_NAME, DOB, AGE, TEAM_ID) VALUES(?,?,?,?);";
jdbcTemplate.update(insertSql,new Object[]{player.getPlayerName(),player.getDob(), player.getAge(), player.getTeamId()});
}
@Transactional (propagation=Propagation.REQUIRED)
public void insertTeam(Team team){
String insertSql ="INSERT INTO TEAMS (TEAM_ID, TEAM_NAME) VALUES(?,?);";
jdbcTemplate.update(insertSql,new Object[]{team.getTeamId(),team.getTeamName()});
}
}

玩家服务

import com.test.model.Team;
public interface PlayerService {
public void createTeam1(Team team) throws Exception;
public void createTeam2(Team team) throws Exception;
}

玩家服务实现

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.test.model.Team;
import com.test.persistence.PlayerDao;
@Service
public class PlayerServiceImpl implements PlayerService{
@Autowired
private  PlayerDao playerDao;
public void createTeam1(Team team) throws Exception{
playerDao.insertPlayer(team.getPlayers().get(0));
playerDao.insertPlayer(team.getPlayers().get(1));
playerDao.insertPlayer(team.getPlayers().get(2));
playerDao.insertTeam(team);
}
public void createTeam2(Team team) throws Exception{
playerDao.insertPlayer(team.getPlayers().get(0));
playerDao.insertPlayer(team.getPlayers().get(1));
playerDao.insertTeam(team);
throw new Exception();
}
}

主类;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.model.Player;
import com.test.model.Team;
import com.test.service.PlayerService;
public class TestMain {

public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
PlayerService playerService = (PlayerService) context.getBean("playerService");
Team t1 = new Team();
t1.setTeamId(1);
t1.setTeamName("Team-1");
Player p1 = new Player("Player 1", LocalDate.of(1981,05,02), 1);        
Player p2 = new Player("Player 2", LocalDate.of(1983,02,15), 1);
Player p3 = new Player("Player 3", LocalDate.of(1980,12,31), 1);
List<Player> players1 = new ArrayList<Player>();
players1.add(p1);
players1.add(p2);
players1.add(p3);
t1.setPlayers(players1);
Team t2 = new Team();
t2.setTeamId(2);
t2.setTeamName("Team-2");
Player p4 = new Player("Player 4", LocalDate.of(1989,05,02), 1);
Player p5 = new Player("Player 5", null, 1);
List<Player> players2 = new ArrayList<Player>();
players2.add(p4);
players2.add(p5);
t2.setPlayers(players2);
try {
playerService.createTeam1(t1);
playerService.createTeam2(t2);
} catch (Exception e) {
e.printStackTrace();
}
}
}

当我在上下文文件中将玩家道设置为玩家服务的属性之一时,事务运行良好,如下所示(没有为玩家道设置@Autowired(;

<bean id="playerService" class="com.slpl.service.PlayerServiceImpl" >
<property name="playerDao" ref="playerDao" />
</bean>
<bean id="playerDao" class="com.slpl.persistence.PlayerDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate" />
</bean>

但是,当我将玩家道@Autowired到PlayerServiceImpl类时,事务不适用(事务不起作用(。

我在这里犯了什么错误?

此示例应用程序将使用注释演示 Spring 事务。 PlayerServiceImpl 类中的 createTeam1(( 方法将创建 3 名球员和一个团队。因此,一旦它完成运行,它将在 PLAYER 表中创建 3 行,在 TEAMS 表中创建 1 行。createTeam2(( 方法尝试创建两个玩家和一个团队。在此方法结束时,它会抛出一个异常,该异常应回滚球员和球队创建,并且不得在球员和球队表中创建行。

在 PlayerServiceImpl 类中使用@Autowired注释是正确的。但是,交易的适用是不正确的。正如@M.Deinum清楚地解释的那样,当前的代码通过在DAO方法(在本例中为insertPlayer((和insertTeam((方法(直接应用事务来创建7个单独的事务。因此,应用事务的正确方法是服务方法(在本例中为 createTeam1(( 和 createTeam2(( 方法(。

为了在抛出异常时回滚事务,必须使用正确的异常设置事务的 rollbackfor 事务属性(作为@Transactional注释的属性(。然后 spring 事务在抛出异常时回滚更改。因此,正确的服务方法实现如下;

@Transactional (propagation=Propagation.REQUIRED, rollbackFor = {Exception.class})
public void createTeam1(Team team) throws Exception{
playerDao.insertPlayer(team.getPlayers().get(0));
playerDao.insertPlayer(team.getPlayers().get(1));
playerDao.insertPlayer(team.getPlayers().get(2));
playerDao.insertTeam(team);
}
@Transactional (propagation=Propagation.REQUIRED, rollbackFor = {Exception.class})
public void createTeam2(Team team) throws Exception{
playerDao.insertPlayer(team.getPlayers().get(0));
playerDao.insertPlayer(team.getPlayers().get(1));
playerDao.insertTeam(team);
throw new Exception();
}

最新更新