Spring Boot发出警告指定了自定义隔离级别但未启动实际事务

时间:2018-03-31 01:03:06

标签: spring hibernate spring-boot spring-data-jpa

我收到警告我已经指定了自定义隔离级别,但是当我使用带有隔离和传播的@Transactional注释时没有启动实际事务。但我的查询执行正常。为什么方法调用不在事务边界下。

    @SpringBootApplication(scanBasePackages= {"com.mytest.txntest"})
    @EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
    public class Application {

         public static void main( String[] args )
            {
                SpringApplication.run(Application.class, args);        
            }
    }

    My Data Source Configuration looks like below.

     @Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
        @EnableJpaRepositories(basePackages = { "com.mytest.txntest" }, entityManagerFactoryRef = "testEntityManagerFactory", transactionManagerRef = "testTransactionManager")
        @EnableTransactionManagement
        public class TestDBConfig {

            @Resource
            private Environment env;



            @Bean(name="testEntityManagerFactory")
            public LocalContainerEntityManagerFactoryBean testEntityManager(){

                LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();

                entityManager.setDataSource(testDataSource());

                Properties properties = new Properties();


                properties.setProperty("hibernate.dialect", "org.hibernate.dialect.DB2Dialect");
                properties.setProperty("persistenceProviderClassName", "org.hibernate.ejb.HibernatePersistence");

                entityManager.setJpaProperties(properties);
                entityManager.setPersistenceUnitName("Test_DB");
                entityManager.setPackagesToScan("com.mytest.txntest.entities");
                entityManager.setJpaVendorAdapter(jpaVendorAdapter());

                return entityManager;

            }

            @Bean(name="testDataSource")
            public DataSource testDataSource() {
                DataSource dataSource = DataSourceBuilder
                        .create()
                        .username(env.getProperty("test.username"))
                        .password(env.getProperty("test.password"))
                        .url(env.getProperty("test.url"))
                        .driverClassName(env.getProperty("test.datasource.driverClassName"))                
                        .build();
                return dataSource;
            }

            @Bean
            public HibernateJpaVendorAdapter jpaVendorAdapter() {

                HibernateJpaVendorAdapter hibernateAdatper = new HibernateJpaVendorAdapter();
                hibernateAdatper.setDatabasePlatform("org.hibernate.dialect.DB2Dialect");
                hibernateAdatper.setShowSql(true);
                return hibernateAdatper; 
            }

            @Bean("testTransactionManager")
            public JpaTransactionManager jpaTransactionManager(@Qualifier("testEntityManagerFactory") EntityManagerFactory entityMangerFactory) {

                JpaTransactionManager jpsTxnManager = new JpaTransactionManager();
                jpsTxnManager.setEntityManagerFactory(entityMangerFactory);


                return new JpaTransactionManager();
            }

        }

我的DAO课程如下所示。

public TestDAO {

@PersistenceContext
    public void setEntityManager(EntityManager entityManager) 
    {
        this.em = entityManager;
    }

    private EntityManagerFactory entityManagerFactory;

    public void setEntityManagerFactory(EntityManagerFactory emf) 
    {
        this.entityManagerFactory = emf;
        em = entityManagerFactory.createEntityManager();        
    }


    @PersistenceContext
    protected EntityManager em;

@Transactional(propagation=Propagation.NOT_SUPPORTED,isolation=Isolation.READ_UNCOMMITTED,rollbackFor=Throwable.class)
    public Webacct getUserNameInfo(String userName,String dob) 
    {
    Query q = em.createNamedQuery("testQuery");
}
}
}

我错过了任何配置。使用Spring Boot 1.5.9

1 个答案:

答案 0 :(得分:1)

您正在使用“NOT_SUPPORTED”传播级别,它基本上在没有事务的情况下执行该方法内的代码。您应该使用任何其他传播级别,例如REQUIRED或REQUIRES_NEW。