Spring Boot应用程序:jpa查询返回旧数据

ank*_*arg 5 spring jpa spring-data-jpa spring-boot

我们使用1.3.5版本创建了一个spring boot项目。我们的应用程序与 Mysql 数据库交互。我们创建了一组 jpa 存储库,在其中使用 findAll、findOne 和其他自定义查询方法。

我们面临着一个随机发生的问题。以下是重现它的步骤:

  1. 使用 spring-boot 应用程序在数据库上触发读取查询。

  2. 现在使用上述读取查询返回的记录的 mysql-console 手动更改 Mysql 中的数据。

  3. 使用应用程序再次触发相同的读取查询。

经过第3步,我们应该已经收到了第2步的修改结果,但我们得到的是修改前的数据。

现在,如果我们再次使用应用程序触发读取查询,它会为我们提供正确的值。

此问题是随机发生的。我们没有在我们的应用程序中使用任何类型的缓存。

在调试时我发现jpa-repository代码实际上正在调用mysql并且它也获取最新的结果,但是当这个调用返回到我们的应用程序服务时,令人惊讶的是返回值具有旧数据。

请帮助我们找出可能的原因。

JPA/数据源配置:

  • spring.datasource.driverClassName=com.mysql.jdbc.Driver
  • spring.datasource.url=jdbc:mysql://localhost:3306/dbname?autoReconnect=true
  • spring.datasource.username=root
  • spring.datasource.password=xxx
  • spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
  • spring.datasource.max-wait=15000
  • spring.datasource.max-active=100
  • spring.datasource.max-idle=20
  • spring.datasource.test-on-borrow=true
  • spring.datasource.remove-abandoned=true
  • spring.datasource.remove-abandoned-timeout=300
  • spring.datasource.default-auto-commit=false
  • spring.datasource.validation-query=SELECT 1
  • spring.datasource.validation-interval=30000
  • hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
  • hibernate.show_sql=false
  • hibernate.hbm2ddl.auto=更新

服务方式:

     @Override
    @Transactional
    public List<Event> getAllEvent() {
        return  eventRepository.findAll();
    }
Run Code Online (Sandbox Code Playgroud)

JPA 存储库:

public interface EventRepository extends JpaRepository<Event, Long> {
    List<Event> findAll();
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*aga 3

@Cacheable(false)
Run Code Online (Sandbox Code Playgroud)

例子:

@Entity
@Table(name="table_name")
@Cacheable(false)
public class EntityName {
    // ...
}
Run Code Online (Sandbox Code Playgroud)