We have developed a Camel bundle (deployed in Karaf), which is expected to pull data from MySQL every 24 hour and push it to S3. But as MySQL internally close the connection if it is idle for 8 hours, hence on next scheduled execution it starts throwing an error. Please see below snippets from our code.
Properties:
MySqlDriver=com.mysql.jdbc.Driver
MySqlDatabaseURL=jdbc:mysql://x.x.x.x/dbname?autoReconnect=true
MySqlUsername=sm*****
MySqlPassword=*******
Activator:
public class Activator implements BundleActivator {
public CamelContext context = null;
public void start(BundleContext bundleContext) throws Exception {
DataSource dataSource = UDMSUtils.createDataSource(UDMSUtils.getProperty(UDMSConstants.MYSQL_DATABASE_URL));
SimpleRegistry simpleRegistry = new SimpleRegistry();
simpleRegistry.put(UDMSConstants.UDMS_DATA_SOURCE, dataSource);
context = new OsgiDefaultCamelContext(bundleContext, simpleRegistry);
context.addRoutes(new CreativeRoutes());
context.start();
}
}
Building Data Source:
public static DataSource createDataSource(String connectURI) {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName(getProperty(UDMSConstants.MYSQL_DRIVER));
ds.setUsername(getProperty(UDMSConstants.MYSQL_USERNAME));
ds.setPassword(getProperty(UDMSConstants.MYSQL_PASSWORD));
ds.setUrl(connectURI);
ds.setMaxWait(-1); // Waits indefinately
return ds;
}
Routes:
from("timer://Timer?repeatCount=1").to("direct:record_count").end();
from("direct:record_count")
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
exchange.getIn().setBody(query);
}
})
.routeId("record_count")
.to("jdbc:" + UDMSConstants.UDMS_DATA_SOURCE)
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
// ...
}
);
Can anyone please suggest, what changes needs to be done in the above code so the connection remains active for as long as we need.
Please Note: We do not have permissions to change the mysql.properties
, hence we need to handle this in our code.