0
votes

My problem is quite the other way round, most questions here are for test data not being inserted, mine persists in tests but not when called thru the controller.

I'm still new at this so may have made some silly mistakes. Any help is appreciated!

This is my setup.

Project 1: Business Logic Layer: Entities, Services, Repo (Spring Data), Oracle => bll.jar (Test saves, can see insert statements in log). Files: AppConfig, HibernateConfig, PersistenceConfig

Project 2: Spring MVC 4.3.3, Thymeleaf 3, import bll.jar, controller calls BLL Service to save. No insert statements in log, just select statements. Oracle sequence number being incremented. The returned object after save() shows the new object with new Id. No error. Deployed in JBoss Wildfly.

What I tried so far

  1. Thinking it must be a commit or flush issue, added saveAndFlush() in repo and service. Works in BLL Test but when called from MVC it gives error No transaction is in progress.
  2. Checked MVC save() and looked for bindingResult errors, there were none
  3. Log seems to show transaction commit so don't know why it isn't persisting
  4. Added @Transactional(propagation = Propagation.REQUIRED) above my controller save() following Data not inserted to db but no effect

Controller

@PostMapping(value="/create", params={"save"}) //
public String save(final Cat cat
            , final BindingResult bindingResult, final ModelMap model){
    if(bindingResult.hasErrors()){
        logger.info(bindingResult.getAllErrors().toString());
        return "cat/create";
    }       
    Cat updatedCat = catService.save(cat); //saveAndFlush       
    model.clear();
    return "redirect:/cat/create";      
}

Edit 1

Note my controller does not have @Transaction, just my BLL Service method

Also, my entity Cat has related entities but I omited for brevity

Service

@Transactional
public Cat save(Cat cat){
    return catRepo.save(cat);
}

Relevant log

2017-02-27 08:52:22 DEBUG SQL:109 - select catdts0_.CAT_ID as CAT_ID6_0_0_, .. from CAT_DT catdts0_ where catdts0_.CAT_ID=? 2017-02-27 08:52:22 TRACE BasicBinder:81 - binding parameter [1] as [NUMERIC] - [1] .. 2017-02-27 08:52:22 TRACE BasicExtractor:78 - extracted value ([CAT_ID6_0_0_] : [NUMERIC]) - [1] 2017-02-27 08:52:22 TRACE BasicExtractor:78 - extracted value ([ID1_1_0_] : [NUMERIC]) - [1] 2017-02-27 08:52:22 DEBUG EntityManagerFactoryUtils:435 - Closing JPA EntityManager 2017-02-27 08:52:22 DEBUG HibernateTransactionManager:759 - Initiating transaction commit 2017-02-27 08:52:22 DEBUG HibernateTransactionManager:580 - Committing Hibernate transaction on Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=org.hibernate.engine.spi.ExecutableList@63cd240d updates=org.hibernate.engine.spi.ExecutableList@6262ff06 deletions=org.hibernate.engine.spi.ExecutableList@72d1cb79 orphanRemovals=org.hibernate.engine.spi.ExecutableList@3ff23a08 collectionCreations=org.hibernate.engine.spi.ExecutableList@35158cb7 collectionRemovals=org.hibernate.engine.spi.ExecutableList@407acfdc collectionUpdates=org.hibernate.engine.spi.ExecutableList@3c0c4ea9 collectionQueuedOps=org.hibernate.engine.spi.ExecutableList@1200015a unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] 2017-02-27 08:52:22 DEBUG DataSourceUtils:222 - Resetting read-only flag of JDBC Connection [oracle.jdbc.driver.T4CConnection@1dfb6d07] 2017-02-27 08:52:22 DEBUG HibernateTransactionManager:669 - Closing Hibernate Session [SessionImpl(PersistenceContext[entityKeys=[],collectionKeys=[]];ActionQueue[insertions=org.hibernate.engine.spi.ExecutableList@63cd240d updates=org.hibernate.engine.spi.ExecutableList@6262ff06 deletions=org.hibernate.engine.spi.ExecutableList@72d1cb79 orphanRemovals=org.hibernate.engine.spi.ExecutableList@3ff23a08 collectionCreations=org.hibernate.engine.spi.ExecutableList@35158cb7 collectionRemovals=org.hibernate.engine.spi.ExecutableList@407acfdc collectionUpdates=org.hibernate.engine.spi.ExecutableList@3c0c4ea9 collectionQueuedOps=org.hibernate.engine.spi.ExecutableList@1200015a unresolvedInsertDependencies=UnresolvedEntityInsertActions[]])] after transaction 2017-02-27 08:52:22 DEBUG DispatcherServlet:1251 - Rendering view [org.thymeleaf.spring4.view.ThymeleafView@13f5fc54] in DispatcherServlet with name 'appServlet' 2017-02-27 08:52:22 DEBUG ReloadableResourceBundleMessageSource:440 - No properties file found for [/resources/i18n/messages_en_GB] - neither plain properties nor XML 2017-02-27 08:52:22 DEBUG ReloadableResourceBundleMessageSource:410 - Re-caching properties for filename [/resources/i18n/messages_en] - file hasn't been modified 2017-02-27 08:52:22 DEBUG DispatcherServlet:1000 - Successfully completed request

If I missed any important info then let me know what to post, I'm still groping in the dark.

1
Are you sure your configuration for your application is setting up transaction management where-as perhaps your test configuration is? - Naros
Thanks @Naros, your comment was helpful. Although I had just one context, turned out my config was messed up. Posting a solution, in case it helps others. - Philip YW

1 Answers

1
votes

Answering my own question, with help from @Naros

Turns out my PersistenceConfig was messed up.

PersistenceConfig

public class PersistenceConfig {
    ...

    @Bean
    public PlatformTransactionManager transactionManager()
    {
        EntityManagerFactory factory = entityManagerFactory().getObject();
        return new JpaTransactionManager(factory);
    }

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

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory()
    {
        HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        vendorAdapter.setGenerateDdl(false); //was true
        vendorAdapter.setShowSql(true);

        LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
        factory.setDataSource(dataSource());
        factory.setJpaVendorAdapter(vendorAdapter);
        factory.setJpaProperties(jpaProperties());
        factory.setPackagesToScan("ae.tbits.atn.aiwacore.common.model");
        return factory;
    }

    @Bean
    public DataSource dataSource() {
        ...
        return dataSource;
    }
}

Had 2 transactionManager(), I suspect one was called in Test while the other in my web controller. Removed the below to solve the problem.

public HibernateTransactionManager transactionManager(SessionFactory s)