vendredi 17 juin 2016

Two EntityManagerFactories, TransactionRequiredException: no transaction is in progress

The Spring MVC application uses SpringDataJPA and hibernate. The production mode leverages MySQL but for the unit tests I have set up H2DB to run, both with separate java configuration. The application also utilizes Spring Security but its configuration I omitted below.

The appliaction starts up with the unit tests but at the first test (testOIC() see in the below code) I got the "TransactionRequiredException: no transaction is in progress".

 testException = org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress, mergedContextConfiguration = [WebMergedContextConfiguration@1c581a0 testClass = OpenPositionsServiceTest, locations = '{}', classes = '{class our.dcollect.configuration.AppConfiguration, class our.dcollect.configuration.HibernateConfigurationTest}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]].

The unit test I run with Propagation.REQUIRES_NEW, so it should have a transaction context. I am afraid I messed up something in the appliaction context. The posts I found in connection with this topic did not help a lot.

The ApplicationContext contains the following beans: Spring Bean Definition Names in ApplicationContext (applicationContext.getBeanDefinitionNames()):

===================================================
Bean: appConfiguration
Bean: authenticationManagerBuilder
Bean: autowiredWebSecurityConfigurersIgnoreParents
Bean: beanNameHandlerMapping
Bean: dataSource
Bean: dataSourceTest
Bean: defaultServletHandlerMapping
Bean: delegatingApplicationListener
Bean: dtoConverter
Bean: emBeanDefinitionRegistrarPostProcessor
Bean: enableGlobalAuthenticationAutowiredConfigurer
Bean: entityManagerFactory
Bean: entityManagerFactoryTest
Bean: fileUploadController
Bean: fileUploadService
Bean: getInternalResourceViewResolverJsp
Bean: handlerExceptionResolver
Bean: hibernateConfiguration
Bean: hibernateConfigurationTest
Bean: hibernateProperties
Bean: httpRequestHandlerAdapter
Bean: initializeAuthenticationProviderBeanManagerConfigurer
Bean: initializeUserDetailsBeanManagerConfigurer
Bean: jpaContext
Bean: jpaMappingContext
Bean: jpaVendorAdapter
Bean: jpaVendorAdapterTest
Bean: multipartResolver
Bean: mvcContentNegotiationManager
Bean: mvcConversionService
Bean: mvcPathMatcher
Bean: mvcResourceUrlProvider
Bean: mvcUriComponentsContributor
Bean: mvcUrlPathHelper
Bean: mvcValidator
Bean: mvcViewResolver
Bean: objectPostProcessor
Bean: openIn....Repository
Bean: openPositionsService
Bean: org.springframework.aop.config.internalAutoProxyCreator
Bean: org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor
Bean: org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor
Bean: org.springframework.context.annotation.internalAutowiredAnnotationProcessor
Bean: org.springframework.context.annotation.internalCommonAnnotationProcessor
Bean: org.springframework.context.annotation.internalConfigurationAnnotationProcessor
Bean: org.springframework.context.annotation.internalPersistenceAnnotationProcessor
Bean: org.springframework.context.annotation.internalRequiredAnnotationProcessor
Bean: org.springframework.context.event.internalEventListenerFactory
Bean: org.springframework.context.event.internalEventListenerProcessor
Bean: org.springframework.data.jpa.repository.config.JpaRepositoryConfigExtension#0
Bean: org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport_Predictor
Bean: org.springframework.orm.jpa.SharedEntityManagerCreator#0
Bean: org.springframework.orm.jpa.SharedEntityManagerCreator#1
Bean: org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration
Bean: org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration
Bean: org.springframework.security.config.annotation.web.configuration.WebMvcSecurityConfiguration
Bean: org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration
Bean: org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration
Bean: org.springframework.transaction.config.internalTransactionAdvisor
Bean: org.springframework.transaction.config.internalTransactionalEventListenerFactory
Bean: org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration
Bean: privilegeEvaluator
Bean: push....Repository
Bean: requestDataValueProcessor
Bean: requestMappingHandlerAdapter
Bean: requestMappingHandlerMapping
Bean: resourceHandlerMapping
Bean: securityConfiguration
Bean: sessionFactory
Bean: sessionFactoryTest
Bean: simpleControllerHandlerAdapter
Bean: springSecurityFilterChain
Bean: transactionAttributeSource
Bean: transactionInterceptor
Bean: transactionManager
Bean: transactionManagerTest
Bean: viewControllerHandlerMapping
Bean: webSecurityExpressionHandler
===================================================

Could you help? The relevant code parts I posted below.

MVC configuration:

package our.dcollect.configuration;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.support.StandardServletMultipartResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;


@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "our.dcollect")
public class AppConfiguration {

    private static final Logger log = Logger.getLogger(AppConfiguration.class);

    @Bean
    public InternalResourceViewResolver getInternalResourceViewResolverJsp(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/view/");
        viewResolver.setSuffix(".jsp");
        viewResolver.setOrder(0);
        log.info("####   Internal view resolver 0 called...");
        return viewResolver;
    }

    @Bean
    public StandardServletMultipartResolver multipartResolver(){
        log.info("####   Multipart resolver called...");
        return new StandardServletMultipartResolver();
    }        
}

Hibernate Configuration:

package our.dcollect.configuration;

import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@ComponentScan(basePackages = "our.dcollect")
@EnableJpaRepositories(basePackages = {"our.dcollect.repository"},  
        entityManagerFactoryRef = "entityManagerFactory",
        transactionManagerRef = "transactionManager")
@PropertySource(value = { "classpath:application.properties" })
@EnableTransactionManagement
//@PropertySource({ "/resources/hibernate.properties" })
public class HibernateConfiguration {

    @Autowired
    private Environment environment;

    @Bean(name="sessionFactory")
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSource());
        sessionFactory.setPackagesToScan(new String[] { "our.dcollect" });
        sessionFactory.setHibernateProperties(this.hibernateProperties());
        return sessionFactory;
     }

    @Bean(name="dataSource")
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
        dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
        dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
        return dataSource;
    }

    @Bean(name="hibernateProperties")
    public Properties hibernateProperties() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
        properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
        properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
        properties.put("hibernate.hbm2ddl.auto", "create-drop");
        return properties;        
    }

    @Bean(name="transactionManager")
    @Autowired
    public HibernateTransactionManager transactionManager(@Qualifier("sessionFactory") SessionFactory s) {
       HibernateTransactionManager txManager = new HibernateTransactionManager();
       txManager.setSessionFactory(s);
       return txManager;
    }

    @Bean(name="jpaVendorAdapter")
    public JpaVendorAdapter jpaVendorAdapter() {
            HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
            hibernateJpaVendorAdapter.setShowSql(true);
            hibernateJpaVendorAdapter.setGenerateDdl(true);
            hibernateJpaVendorAdapter.setDatabase(Database.MYSQL);
            return hibernateJpaVendorAdapter;
    }

    @Bean(name="entityManagerFactory")
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
            LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
            lef.setDataSource(this.dataSource());
            lef.setJpaProperties(this.hibernateProperties());
            lef.setJpaVendorAdapter(this.jpaVendorAdapter());
            lef.setPackagesToScan(new String[] { "our.dcollect.model"});
            return lef;
    }

}

Hibernate Configuration for the Unit Tests:

package our.dcollect.configuration;

import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

/***************************************************************************************
* The same as the real HibernateConfiguration but this works with H2 DB instead of MySQL.
* In addition, the properties are not read from a property file.
****************************************************************************************/
@Configuration
@ComponentScan(basePackages = "our.dcollect")
@EnableJpaRepositories(basePackages = {"our.dcollect.repository"},  
        entityManagerFactoryRef = "entityManagerFactoryTest",
        transactionManagerRef = "transactionManagerTest")
@PropertySource(value = { "classpath:application.properties" })
@EnableTransactionManagement
public class HibernateConfigurationTest {


    @Bean(name = "sessionFactoryTest")
    public LocalSessionFactoryBean sessionFactoryTest() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(dataSourceTest());
        sessionFactory.setPackagesToScan(new String[] { "our.dcollect" });
        sessionFactory.setHibernateProperties(hibernatePropertiesTest());
        return sessionFactory;
    }

    @Bean(name = "dataSourceTest")
    public DataSource dataSourceTest() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
        dataSource.setUsername("...");
        dataSource.setPassword("...");
        return dataSource;
    }

    private Properties hibernatePropertiesTest() {
        Properties properties = new Properties();
        properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        properties.put("hibernate.hbm2ddl.auto", "create-drop");
        return properties;
    }

    @Bean(name = "transactionManagerTest")
    @Autowired
    public HibernateTransactionManager transactionManagerTest(@Qualifier("sessionFactoryTest") SessionFactory s) {
        HibernateTransactionManager txManager = new HibernateTransactionManager();
        txManager.setSessionFactory(s);
        return txManager;
    }


    @Bean(name = "jpaVendorAdapterTest")
    public JpaVendorAdapter jpaVendorAdapterTest() {
            HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
            hibernateJpaVendorAdapter.setShowSql(true);
            hibernateJpaVendorAdapter.setGenerateDdl(true);
            hibernateJpaVendorAdapter.setDatabase(Database.H2);
            return hibernateJpaVendorAdapter;
    }

    @Bean(name = "entityManagerFactoryTest")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryTest() {
            LocalContainerEntityManagerFactoryBean lef = new LocalContainerEntityManagerFactoryBean();
            lef.setDataSource(this.dataSourceTest());
            lef.setJpaProperties(this.hibernatePropertiesTest());
            lef.setJpaVendorAdapter(this.jpaVendorAdapterTest());
            lef.setPackagesToScan(new String[] { "our.dcollect.model"});
            return lef;
    }

}    

The Unit Test class abbriviated for legibility:

package our.dcollect.service;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.junit.After;
import org.junit.AfterClass;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import our.dcollect.configuration.AppConfiguration;
import our.dcollect.configuration.HibernateConfigurationTest;
import our.dcollect.model.OpenIC;
import our.dcollect.repository.OpenICRepository;

@WebAppConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfiguration.class, HibernateConfigurationTest.class})
public class OpenPositionsServiceTest {

    @Autowired
    private OpenICRepository OpenICDAO;

    @PersistenceContext(unitName  = "entityManagerFactoryTest")
    private EntityManager entityManager;



    @Test
    @Transactional(value="transactionManagerTest", propagation = Propagation.REQUIRES_NEW)
    public void testOIC() {
        System.out.println("getOpenPositionsNotRegisteredInPushBanch");

        OpenIC oic = new OpenIC();
        oic.setBaId("111");
        oic.setBezeichnung("abc");

        // clear the persistence context so we don't return the previously cached location object
        // this is a test only thing and normally doesn't need to be done in prod code
        entityManager.clear();

        OpenICDAO.saveAndFlush(oic);
        List<OpenIC> list = OpenICDAO.findAll();
        assertEquals(list.size(), 1);

        OpenIC oicReadBack = list.get(0);
        OpenICDAO.delete(oicReadBack.getOpenICId());

    }
[...]    
}

Steps Towards the Solution:


The posts from Funtik helped a lot. The following setps I carried out:

  1. @DependsOn("transactionManagerTest") to tell Spring that the
    EntityManagerFactory needs to be loaded after the TransactionManager but it did not solve the problem.
  2. Introducing @Profile("test") for HibernateConfigurationTest and @Profile("!test") for HibernateConfiguration. On the test class I put @ActiveProfiles("test"). This separated the beans; only one flavour was loaded during the test: the ones in HibernateConfigurationTest. This however did not solve the problem either.

  3. Then I completely removed one profile in a separate branch to see whether the two configuration for the DB cause the issue. I
    experienced the same problem with one DB configuration as before.

  4. I added @TestExecutionListeners({TransactionalTestExecutionListener.class,
    DependencyInjectionTestExecutionListener.class}) to the unit test
    class
    and @Rollback(false) to the test method to see whether anything is saved in the MySQL database. I also changed
    "hibernate.hbm2ddl.auto" to "create", to preserve the DB after the
    test. It changed the situation as the message appeared in the log:
    "[...] Began transaction (1) for test context
    [DefaultTestContext@a15b73 [...]". So one transaction context has
    definitely been created and has had 1 transaction. The problem was
    however that the entity was not persisted in the database if I call OpenICDAO.save(oic) in the test. In addition, calling
    OpenICDAO.saveAndFlush(oic) causes the nearly the exception below:

"[...] 4671 [main] WARN org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@45c9b3] to process 'after' execution for test: method [public void our.dcollect.service.OpenPositionsServiceTest.testGetOpenPositionsNotRegisteredInPushBanch()], instance [our.dcollect.service.OpenPositionsServiceTest@dc7b7d], exception [org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress] org.springframework.transaction.UnexpectedRollbackException: Transaction rolled back because it has been marked as rollback-only [...]"

Tests in error:

  testOIC(our.dcollect.service.OpenPositionsServiceTest): no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress
  testOIC(our.dcollect.service.OpenPositionsServiceTest): Transaction rolled back because it has been marked as rollback-only

Tests run: 3, Failures: 0, Errors: 2, Skipped: 0

However, I only have 2 methods not 3 from which testOIC is reported twice.

Aucun commentaire:

Enregistrer un commentaire