JPARepository and Vaadin, transaction error

Hello everyone,

Finally I decided to try Spring-Vaadin integration using Spring Boot. So far everything has been exceptionally easy, I also followed the webinar and it has been very useful to get me started.

My problem, though, comes when trying to update an entity in the database within a @SpringView. At first I made this method

@Autowired private ProjectDAO projectDAO;

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

Where projectDAO is a JPARepository<Project, Long>.

When trying to call that method via a button click

[code]

btnSave.addClickListener(e → saveCurrent());

private void saveCurrent() {
// … here I do some filesystem operations, nothing that requires DB connection
setModified();
}
[/code]I get the exception:[font=courier new]
org.springframework.dao.InvalidDataAccessApiUsageException: no transaction is in progress; nested exception is javax.persistence.TransactionRequiredException: no transaction is in progress.

[font=arial]
So I tried at first to make the method @Transactional, but it didn’t work. I then tried to encapsulate the setModified into a

@Service
public class ProjectService {
    @Autowired private ProjectDAO dao;

    @Transactional
    public void setUpdated(Project project) {
        project.setLastAccess(new Date());
        dao.saveAndFlush(project);
    }
}

[/font]

Then of course I autowired the service and tried again, but still same exception

[/font]
I think I might be missing something obvious, data retrieval works like a charm…

If you have some solutions and maybe also some examples on how to use this feature, I would really appreciate it!

Thanks for your work
Best regards

I have new information: this problem only occurs when trying to use an external server: using the embedded server provided by Spring Boot this doesn’t happen and everything works correctly.

I’ve been using the pivotal TC server included in the Springsource Tool Suite to test the application since later this will be published on another server, and I can’t make it a “stand alone” application.
I also tried Tomcat 8 and the result is the same.

Any @OneToMany relationship in your project entity ?

In the particular entity there is a @ManyToOne, but I also tried to make a smaller project with one plain entity (just a string property other than the @Id and @Version fields) and one repository to try step by step, and the result is still the same, so I don’t think it is related to that. If you wish to have a look I can upload it for testing purposes.
Thanks for the reply!

Hi,
Might be usefull to upload sample code for testing. I would have some time in the coming days.

Cheers,
Koenraad

Hi, after a week I finally managed to find out what the problem was:

After downloading the project from Spring Initializr I followed this guide:
https://spring.io/blog/2014/03/07/deploying-spring-boot-applications
, and added the
@Configuration @ComponentScan @EnableAutoConfiguration
annotations to my application class as suggested, and made it extend
SpringBootServletInitializer
. But I didn’t realize that there was already one class extending
SpringBootServletInitializer
, so the application in Tomcat was started two times, thus allocating the Persistence Unit twice. I think that was the problem, because after reverting the project status to what it was when I first downloaded it everything worked fine.

Thanks for the reply anyway! :slight_smile: