camel中的JPA组件不能自动重新连接到数据库 [英] JPA component in camel cannot reconnect automatically to database
问题描述
当我的应用程序运行时重新启动数据库时, JPA组件无法自动重新连接。
When I reboot my database while my application is running, JPA components cannot reconnect automatically.
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请求。
But still I am able to execute SQL request with spring-data in my CXF routes.
我在context.xml中的数据源定义:
My datasource definition in 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组件的样子:
And here is what my JPA component looks like :
@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。
After some deep debugging I finally figured out my JPA components weren't using the EntityManagerFactory declared in the Spring context.
两种方法可以解决这个问题:
Two ways to solve that :
-
修改JPA uri在每个JPA组件上添加sharedEntityManager选项:
Modify the JPA uri on each JPA component and add the sharedEntityManager option :
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";
Spring中的Instanciate JPA组件:
Instanciate JPA component in Spring :
@Bean
public JpaComponent jpa() {
final JpaComponent jpa = new JpaComponent();
jpa.setSharedEntityManager(true);
jpa.setJoinTransaction(false);
return jpa;
}
这篇关于camel中的JPA组件不能自动重新连接到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!