带有注释配置的@Transactional不会回滚

时间:2016-04-27 16:28:51

标签: java spring-mvc spring-transactions

我尝试了很多东西而且没有用。我希望我的数据库中插入的数据在vacationsDao.requestVacations发生错误时回滚。这是我的代码:

VacationsService:

import java.text.ParseException;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.autozone.web.dao.VacationsDao;
import com.autozone.web.exception.InvalidVacationsException;

@Service

public class VacationsServiceImpl implements VacationsService {
    private ValidationService validationService;
    private VacationsDao vacationsDao;

    @Autowired
    public VacationsServiceImpl(ValidationService validationService,
            VacationsDao vacationsDao) {
        this.validationService = validationService;
        this.vacationsDao = vacationsDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void requestVacations(String username, Set<String> days)
            throws InvalidVacationsException {
        if (validationService.validateUsername(username)) {
            for (String day : days) {
                try {
                    if (!validationService.validateDate(day)) {
                        throw new InvalidVacationsException(
                                "Wrong date format. Must use 'yyyy-MM-dd'.");
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                    throw new InvalidVacationsException(
                            "Wrong date format. Must use 'yyyy-MM-dd'.");
                }
            }

            try {
                vacationsDao.requestVacations(username, days);
            } catch (DuplicateKeyException e) {
                throw new InvalidVacationsException(
                        "Dates already requested from this user.");
            }
        } else {
            throw new InvalidVacationsException(
                    "Wrong username format. Must only contains alphanumeric and starts with letters.");
        }
    }
}

VacationsDao

import java.util.Set;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class VacationsDaoImpl implements VacationsDao {
    JdbcTemplate jdbcTemplate;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        jdbcTemplate = new JdbcTemplate(dataSource);
    }

    // It inserts all data when an exception occurs. Check later.
    @Override
    public void requestVacations(String username, Set<String> days)
            throws DuplicateKeyException {
        StringBuilder insert = new StringBuilder(
                "INSERT INTO cc_vacations(username, day) VALUES ");

        String prefix = "";
        for (String day : days) {
            insert.append(prefix);
            insert.append("('");
            insert.append(username);
            insert.append("', '");
            insert.append(day);
            insert.append("')");
            prefix = ",";
        }

        jdbcTemplate.update(insert.toString());
    }
}

DBCONFIG

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class DbConfig {
    @Bean
    public DataSource getDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://callcenter.autozone.com/callcenter");
        dataSource.setUsername("callcenter");
        dataSource.setPassword("h3lp1n5");
        return dataSource;
    }

    @Bean
    public PlatformTransactionManager transactionManager() {
        return new DataSourceTransactionManager(getDataSource());
    }
}

感谢您的帮助! :)

0 个答案:

没有答案