[英]Vaadin and JPARepository: transaction errors
I'm having troubles in integrating Vaadin and spring-data (JPARepositories in particular). 我在集成Vaadin和spring-data(尤其是JPARepository)方面遇到麻烦。
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. 在上次Vaadin团队的指导下进行的最后一次网络研讨会之后,我设法使用Spring Starter(Spring Boot)配置我的应用程序,添加了Vaadin,JPA和一个Postgresql数据库。 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. 我已经尝试了许多不同的方法,例如将
setModified
方法标记为@Transactional
,或者将setModified
封装到服务类中,但是所有内容始终会引发此异常。
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 编辑12/05/2015
It seems that this problem only appears when using an external server, not the embedded one provided by spring-boot. 看来,仅当使用外部服务器时才出现此问题,而不是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?
Tomcat的配置?
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 . 在部署应用程序之前,我对Spring Initializr(应用程序之一)生成的主类进行了一些配置,并使其扩展了
SpringBootServletInitializer
因为我遵循的是在spring官方官方博客中找到的指南。 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. 事实证明,实际上这不是必需的,因为我从一开始就在Spring Initializr中设置了“ WAR”,因此该应用程序已经具有扩展该类的类,结果,将其部署到Tomcat时,该Web应用程序启动了两次,因此该应用程序配置了两次,并实例化了两个持久性单元。 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. TL; DR:如果使用Spring Initializr创建WAR应用程序,则不要触摸任何内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.