Search code examples
javaspring-bootvaadinspring-data-jpavaadin7

Vaadin and JPARepository: transaction errors


I'm having troubles in integrating Vaadin and spring-data (JPARepositories in particular).

After following the guidance of Vaadin's team with their last webinar I managed to configure my application using Spring starter (spring boot), adding Vaadin, JPA and a Postgresql database. Using this method was really straightforward and loading entities to a table works out of the box by simply having this

@Autowired private ProjectDAO projects;

// other code here ...
grid.setContainerDataSource(new BeanItemContainer<Project>(Project.class, projects.findAll()));

My repository is just

@Repository
public class ProjectDAO implements JPARepository<Project, Long> {

}

And, as I said, this works flawlessly. The problem is when I try to save something: when trying to save a project via a button click, for example

btnSave.addClickListener(e -> saveCurrent());

private void saveCurrent() {
// ... here I do some filesystem operations, nothing that requires DB connection
    setModified();
}

public void setModified() {
    project.setLastAccess(new Date());
    projects.saveAndFlush(project);
}

I get an exception:

org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress.

I've tried many different things, such as marking the setModified method as @Transactional, or encapsulating the setModified into a service class, but everything always throws this exception.

I find it rather strange that this doesn't work "out of the box", and I'd rather not work with transactions myself, since there are already the instruments to do that.

Any help would be really appreciated

EDIT 12/05/2015

It seems that this problem only appears when using an external server, not the embedded one provided by spring-boot. For test purposes I can manage to use the built-in, but eventually I will have to deploy the application on an existing server, so what could the issue be? Tomcat's configuration?


Solution

  • If anyone needs the answer I'll leave here what I found out after a few days of breaking my head on it.

    Before deploying the application I changed some configurations on the main class that was generated by Spring Initializr (The application one) and made it extend SpringBootServletInitializer because I was following the guide I found in the official spring blog. Turns out that this was in fact not necessary because I set up "WAR" in Spring Initializr from the beginning, thus the application already had a class extending that one, and as a result when deploying it on Tomcat, the web application started twice, so the application was configured twice and two persistence units were instantiated. This probably was the error, because after reverting the changes I made to the application everything worked fine.

    TL;DR: if you use Spring Initializr to create a WAR application don't touch anything.