hibernate.connection.isolation被忽略了吗?

时间:2017-01-18 22:39:30

标签: spring hibernate spring-boot

我正在尝试将隔离级别READ_UNCOMMITED设置为我所有应用程序的默认隔离级别。

我的配置:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "myappEntityManagerFactory", transactionManagerRef = "myappTransactionManager", basePackages = {
        "com.example.dao" })
public class MyappDatabaseConfig {

    @Value("${myapp.datasource.db.driver}")
    private String DB_DRIVER;

    (...)

    @Bean(name = "myappDataSource")
    @Primary
    @ConfigurationProperties(prefix = "myapp.datasource")
    public DataSource myappDataSource() {

        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(DB_DRIVER);
        dataSource.setUrl(DB_URL);
        dataSource.setUsername(DB_USERNAME);
        dataSource.setPassword(DB_PASSWORD);

        return dataSource;
    }

    @Bean(name = "myappEntityManagerFactory")
    @Primary
    public LocalContainerEntityManagerFactoryBean myappEntityManagerFactory(EntityManagerFactoryBuilder builder,
            @Qualifier("myappDataSource") DataSource myappDataSource) {

        Properties props = new Properties();
        props.put("hibernate.dialect", HIBERNATE_DIALECT);
        props.put("hibernate.show_sql", HIBERNATE_SHOW_SQL);
        props.put("hibernate.hbm2ddl.auto", HIBERNATE_HBM2DDL_AUTO);
        props.put("hibernate.connection.isolation", Isolation.READ_UNCOMMITTED);

        LocalContainerEntityManagerFactoryBean factoryBean = builder.dataSource(myappDataSource)
                .packages(ENTITYMANAGER_PACKAGES_TO_SCAN).persistenceUnit("myapp").build();

        factoryBean.setJpaProperties(props);

        return factoryBean;
    }

    @Bean(name = "myappTransactionManager")
    @Primary
    public PlatformTransactionManager myappTransactionManager(
            @Qualifier("myappEntityManagerFactory") EntityManagerFactory myappEntityManagerFactory) {
        return new JpaTransactionManager(myappEntityManagerFactory);
    }

但我仍然需要单独设置隔离级别

示例:

@Controller
public class MyappController {

    @RequestMapping(value = "/items", method = RequestMethod.GET)
    @ResponseBody
    @Transactional
    public List<Items> getItems() {

    }

除非我再次添加隔离级别,否则不会读取uncommited

@Controller
public class MyappController {

    @RequestMapping(value = "/items", method = RequestMethod.GET)
    @ResponseBody
    @Transactional(value = "myappTransactionManager", isolation = Isolation.READ_UNCOMMITTED)
    public List<Items> getItems() {

    }

我在配置中遗漏了什么?

    props.put("hibernate.connection.isolation", Isolation.READ_UNCOMMITTED);

还不够吗?

0 个答案:

没有答案