gpt4 book ai didi

java - Camel 中的 JPA 组件无法自动重新连接到数据库

转载 作者:行者123 更新时间:2023-11-29 04:34:42 25 4
gpt4 key购买 nike

当我在应用程序运行时重新启动数据库时,JPA 组件 无法自动重新连接。

2017-02-09 17:45:08,400 ERROR o.h.e.j.spi.SqlExceptionHelper(131) - Connection closed. - [Camel (camel-1) thread #99 - jpa://com.toto.Toto  ]

但仍然我能够在我的 CXF 路由中使用 spring-data 执行 SQL 请求。

我在 context.xml 中的数据源定义:

<!-- JDBC connection -->
<Resource name="jdbc/oracle"
auth="Container"
type="javax.sql.DataSource"
username="toto"
password="toto"
factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@toto:1568:SID"
maxTotal="100"
maxIdle="10"
testOnBorrow="true"
validationQuery="select 1 from dual"/>

这是我的 JPA 组件的样子:

    @Component
public class TotoPollerRoute extends RouteBuilder {

private final String uri;

private static final String POLLING_REQUEST = "select tt from Toto tt where tt.key = 1";

public TotoPollerRoute() {
super();
final StringBuilder uriBuilder = new StringBuilder();
uriBuilder.append("jpa://");
uriBuilder.append(TotoPollerRoute.class.getName());
uriBuilder.append("?");
uriBuilder.append("consumeDelete=false");
uriBuilder.append("&consumeLockEntity=true");
uriBuilder.append("&consumer.SkipLockedEntity=true");
uriBuilder.append("&maximumResults=10");
uriBuilder.append("&consumer.query=");
uriBuilder.append(POLLING_REQUEST);
this.uri = uriBuilder.toString();
}

@Override
public void configure() {
// @formatter:off
from(uri)
.to("TotoMainRoute");
// @formatter:on
}
}

有什么想法吗?

最佳答案

经过一些深度调试后,我终于发现我的 JPA 组件没有使用在 Spring 上下文中声明的 EntityManagerFactory。

解决这个问题的两种方法:

  1. 修改每个 JPA 组件上的 JPA uri 并添加 sharedEntityManager 选项:

    private final String uri = "jpa://TotoPollerRoute?consumeDelete=false"
    + "&consumeLockEntity=true"
    + "&consumer.SkipLockedEntity=true"
    + "&maximumResults=10"
    + "&sharedEntityManager=true"
    + "&joinTransaction=false"
    + "&consumer.query=select tt from Toto tt where tt.key = 1";
  2. 在 Spring 中实例化 JPA 组件:

    @Bean
    public JpaComponent jpa() {
    final JpaComponent jpa = new JpaComponent();
    jpa.setSharedEntityManager(true);
    jpa.setJoinTransaction(false);
    return jpa;
    }

关于java - Camel 中的 JPA 组件无法自动重新连接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42143914/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com