在Spring Boot中了解@Transactional-尽管异常更改不会在数据库中回滚

时间:2019-08-08 12:07:50

标签: java spring spring-data-jpa spring-data

我试图在春季理解@Transactional-为了对其进行测试,我创建了一个简单的项目:

型号:

Magento_Checkout

存储库:

@Entity
public class Model {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    String name;
    Integer age;

    public Model(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

}

服务:

    @Repository
    public interface ModelRepository extends CrudRepository<Model,Long> {
    }

控制器:

@Service
public class ExampleService {

    @Autowired
    private ModelRepository modelRepository;

    @Transactional
    public String doSomething(Integer number) {
        Model test = modelRepository.save(new Model("test", number));
        if(number>4) throw new RuntimeException();

        return test.name;
    }
}

当我进入 localhost:8080 / search / 1 时-一切顺利,对象保存在数据库中。

但是当我按照自己的意愿去 localhost:8080 / search / 10 时,它会抛出Exception异常,但是对象仍然保存在数据库中。

这里不应该回滚吗?用@Transactional注释的方法是公共的,并且在其他bean中。

编辑: application.properties:

@RestController
public class Controller {

    @Autowired
    private ExampleService exampleService;

    @GetMapping(path = "/search/{number}")
    public String search(@PathVariable Integer number){

        return exampleService.doSomething(number);
    }
}

build.gradle:

spring.datasource.url=jdbc:mysql://localhost:3306/testtt?createDatabaseIfNotExist=true&useSSL=false&serverTimezone=Europe/Paris
spring.datasource.username=root
spring.datasource.password=root
spring.jpa.hibernate.ddl-auto=update

主类:

plugins {
    id 'org.springframework.boot' version '2.1.7.RELEASE'
    id 'java'
}

apply plugin: 'io.spring.dependency-management'

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '12'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa'
    compile 'mysql:mysql-connector-java'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

1 个答案:

答案 0 :(得分:0)

带有spring,jpa,hibernate和c3p0的配置示例。

    @Configuration
    @ComponentScan("com.company")
    @EnableTransactionManagement
    public class PersistenceConfig {

        @Value("${hibernate.showSql}")
        private boolean showSql;

        @Bean
        @Primary
        @ConfigurationProperties("c3p0.named-configs.pool")
        @Qualifier("myDataSource")
        public DataSource myDataSource() {
            return new ComboPooledDataSource();
        }

        @Bean
    public PlatformTransactionManager esTransactionManager(@Qualifier("em") EntityManagerFactory em) {
        JpaTransactionManager transactionManager = new JpaTransactionManager(em);
        return transactionManager;
    }

        @Bean
        public LocalContainerEntityManagerFactoryBean em(@Qualifier("myDataSource") DataSource myDataSource, JpaVendorAdapter jpaVendorAdapter) {
            LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
            entityManagerFactoryBean.setDataSource(myDataSource);
            entityManagerFactoryBean.setJpaDialect(new HibernateJpaDialect());
            entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
            entityManagerFactoryBean.setPersistenceUnitName("myPersistenceUnit");
            entityManagerFactoryBean.setPackagesToScan("com.company");
            return entityManagerFactoryBean;
        }

        @Bean
        public JpaVendorAdapter jpaVendorAdapter(@Value("${hibernate.dialect}") String hibernateDialect,
                                                 @Value("${hibernate.showSql}") boolean showSql) {
            HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
            jpaVendorAdapter.setDatabasePlatform(hibernateDialect);
            jpaVendorAdapter.setShowSql(showSql);
            return jpaVendorAdapter;
        }
    }