【问题标题】:Spring Integration JDBC inbound channel adapter not updating recordsSpring Integration JDBC入站通道适配器不更新记录
【发布时间】:2018-02-22 05:55:10
【问题描述】:

我的 Spring Boot 应用的应用上下文是:

<context:component-scan
base-package="org.mycompany.myproject.polling" />

<int:channel id="fromdb" />
<jdbc:embedded-database id="dataSource" type="H2" />
<int:service-activator input-channel="fromdb" ref="jdbcMessageHandler" />
<int-jdbc:inbound-channel-adapter
    channel="fromdb" data-source="dataSource"
    query="select * from Books where status = 0"
    update="update Books set status = 1">
    <int:poller fixed-delay="1000"/>
</int-jdbc:inbound-channel-adapter>

我在资源目录中有 schema.sql 和 data.sql,它们创建表并在启动时插入数据,状态列中的所有记录的值为 0。入站通道适配器的更新查询不运行,因为我看到 H2 中的状态列的值仍然为 0。

我错过了什么?

【问题讨论】:

  • 可能是默认测试框架的回滚性质?
  • 我没有写任何测试代码。 ApplicationTests 类只有一个空的 contextLoads 方法。我只是运行mvn spring-boot:run
  • 请打开org.springframework.jdbc 类别的调试日志记录,以调查对您的数据库调用的内容和方式。如果您通过 Maven 运行您的应用程序,不确定如何检查嵌入式数据库的状态...
  • 我在 pom.xml 中有 spring-boot-starter-web 依赖项,在 application.properties 中有 spring.h2.console.enabled=true。我在 localhost:8080/h2-console/ 检查数据库虽然我添加了调试日志记录,但我仍然没有从消息中得到任何帮助。
  • 好的!有没有机会从您那里获得一个简单的 Spring Boot 项目,让我们在本地使用它来理解问题?谢谢

标签: spring-integration


【解决方案1】:

经过简单的修改后,您的应用程序很适合我:

@SpringBootApplication
@ImportResource("application-context.xml")
public class DbpollerApplication {

    public static void main(String[] args) {
        SpringApplication.run(DbpollerApplication.class, args);
    }

}

如您所见,我删除了 ClassPathXmlApplicationContext 的可疑代码,并真正让 Spring Boot 加载所有内容,包括嵌入式数据源。

application-context.xml 中,我删除了 &lt;context:component-scan&gt;&lt;jdbc:embedded-database&gt;,只是因为它们是由 Spring Boot 本身提供的。

启动应用程序后,我在日志中看到:

Row
column: ITEM_ID value: Book_id99
column: DESCRIPTION value: Book_description99
column: STATUS value: 0
Row
column: ITEM_ID value: XXX
column: DESCRIPTION value: last book
column: STATUS value: 0

我还从那里复制了数据库的 URL:

2018-02-21 16:49:40.357  INFO 10576 --- [           main] o.s.j.d.e.EmbeddedDatabaseFactory        : Starting embedded database: url='jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'

jdbc:h2:mem:testdb 部分就够了。

然后我在localhost:8080/h2-console 上打开H2 Web 控制台,连接到提到的URL 并从BOOKS 表中执行SELECT 并得到以下结果:

我错过了什么吗?

【讨论】:

  • 帮助很大,谢谢。看起来问题行 &lt;jdbc:embedded-database id="dataSource" type="H2" /&gt; 在适配器代码运行后重新创建数据库和/或重新运行启动 data.sql。
猜你喜欢
  • 1970-01-01
  • 2023-02-09
  • 1970-01-01
  • 2014-06-29
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 2016-07-19
相关资源
最近更新 更多