我有一个adhoc作业,我在其中有多个jdbc连接用于select查询,然后结果将导出为csv文件。
下面的代码运行并导出csv文件。当我运行adhoc作业一旦完美运行,当我第二次运行时,它停在中间,它不会更进一步。我必须再次重新启动服务器以运行作业。
@RequestMapping(value = "/ADHOCJob", method = RequestMethod.GET)
public void ABData(HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
final String Batch_id = request.getParameter("Batch_id");
Statement stmt = null;
try {
StringBuffer invitationBuffer = new StringBuffer();
invitationBuffer.append("Batch Name")
.append(",")
.append("Campaign Id")
.append(",")
.append("Type")
.append(",")
.append("Subject Name")
.append(",")
.append("Subject Line")
.append(",")
.append("Body Template")
.append(",")
.append("Sent")
.append(",")
.append("Delivered")
.append(",")
.append("Bounced")
.append(",")
.append("Opens")
.append(",")
.append("Clicks")
.append(",")
.append("Landing Page Hits")
.append("\n");
BasicDataSource dataSource = (BasicDataSource) SpringApplicationContext.getBean("dataSource");
String sql = "select b.batch_name, "+
"c.campaign_id, "+
"ab.ab_test_name, "+
"abtxt.subject_line, "+
"abtxt.body_template, "+
"b.batch_meta_data_id, "+
"cpi.campaign_id, "+
"cpi.ab_test_id, "+
"cpi.user_id, "+
"cpi.invitation_id, "+
"cpi.date_send, "+
"cpi.comm_schedule_id, "+
"cpi.comm_type_id "+
"from COMM_PROCESS_INFO cpi, "+
"BATCHMETADATA b, "+
"CAMPAIGN c, "+
"ABTEST ab, "+
"ABTESTTEXT abtxt "+
"where ab.ab_test_id = abtxt.ab_test_id "+
"and cpi.ab_test_id = abtxt.ab_test_id "+
"and cpi.batch_meta_data_id in ("+Batch_id+") "+
"and b.batch_meta_data_id = cpi.batch_meta_data_id "+
"and c.batch_meta_data_id = b.batch_meta_data_id "+
"group by cpi.campaign_id, "+
"cpi.ab_test_id, "+
"cpi.batch_meta_data_id";
stmt = dataSource.getConnection().createStatement();
ResultSet rs = stmt.executeQuery(sql);
while(rs.next()){
Statement stmtABMetrics = null;
try{
String sqlABMetrics = "select abmetrics.sent, "+
"abmetrics.open, "+
"abmetrics.click, "+
"abmetrics.signup, "+
"abmetrics.bounce, "+
"abmetrics.delivered,"+
"abmetrics.unsubscribe,"+
"abmetrics.spam, "+
"abmetrics.landing_page_hits, "+
"abmetrics.completions "+
"from ABEMAILMETRICS abmetrics "+
"where abmetrics.ab_test_id = "+rs.getString("ab_test_id")+
"and abmetrics.campaign_id = "+rs.getString("campaign_id")+
"and abmetrics.batch_meta_data_id = "+rs.getString("batch_meta_data_id");
stmtABMetrics= dataSource.getConnection().createStatement();
ResultSet rsABMetrics = stmtABMetrics.executeQuery(sqlABMetrics);
if (!rsABMetrics.next()) {
invitationBuffer.append(rs.getString("sent")).append(",");
}
}
catch (Exception e ) {
logger.debug(e,e);
}
finally {
if (stmtABMetrics != null) { stmtABMetrics.close(); }
}
invitationBuffer.append(rs.getString("batch_name"))
.append(",");
invitationBuffer.append(rs.getString("body_template"))
.append(",")
.append("\n");
}
ByteArrayInputStream boas = new ByteArrayInputStream(invitationBuffer.toString().getBytes("UTF-8"));
byte [] b = new byte[boas.available()];
boas.read(b);
Properties props = PropertiesUtil.getApplicationProperties();
StringBuffer strbufMessage = new StringBuffer();
AlertsUtil.notifyWithCSVAttachment(PropertiesUtil.getApplicationProperties().getProperty("alerts.sendUserForApplicationAlerts"), "sdsdsgamail.com", "Invitation Record ",strbufMessage ,invitationBuffer , "Sample.csv", props ,false);
} catch (Exception e) {
logger.debug(e,e);
} finally {
if (stmt != null) { stmt.close(); }
}
System.out.println("Job is running it will take few mins.You will receive an email to shortly once done ");
} catch (Exception re) {
logger.debug(re, re);
}
}
请帮我解决这个问题。
答案 0 :(得分:2)
您泄漏与dataSource.getConnection().createStatement(...)
等结构的连接。您需要将连接存储到变量中,然后将其关闭。此方法中您不需要多一个Connection
。
同样地,你也在泄漏ResultSets
,只是因为没有做任何相反的事情。
通过try-with-resources解决所有问题的简便方法:
try (Connection conn = dataSource.getConnection();
Statement stmnt = conn.createStatement();)
{
// ...
try (ResultSet rs = stmnt.executeQuery(sqlABMetrics))
{
// ...
}
// At this point the ResultSet is closed.
}
// At this point both the Statement and the Connection are closed.
答案 1 :(得分:-1)
这样的事情怎么样:
public void ABData(HttpServletRequest request, HttpServletResponse response) throws Exception {
try {
final String Batch_id = request.getParameter("Batch_id");
try {
StringBuffer invitationBuffer = new StringBuffer();
invitationBuffer.append("Batch Name").append(",").append("Campaign Id").append(",").append("Type").append(",").append("Subject Name").append(",").append("Subject Line").append(",").append("Body Template").append(",").append("Sent").append(",").append("Delivered").append(",").append("Bounced").append(",").append("Opens").append(",").append("Clicks").append(",").append("Landing Page Hits").append("\n");
BasicDataSource dataSource = (BasicDataSource) SpringApplicationContext.getBean("dataSource");
try (Connection connection = dataSource.getConnection();
Statement stmt = connection.createStatement()) {
String sql = "select b.batch_name,c.campaign_id, ab.ab_test_name, abtxt.subject_line, abtxt.body_template, b.batch_meta_data_id, cpi.campaign_id, cpi.ab_test_id, cpi.user_id, cpi.invitation_id, cpi.date_send, cpi.comm_schedule_id, cpi.comm_type_id from COMM_PROCESS_INFO cpi, BATCHMETADATA b, CAMPAIGN c, ABTEST ab, ABTESTTEXT abtxt where ab.ab_test_id = abtxt.ab_test_id and cpi.ab_test_id = abtxt.ab_test_id and cpi.batch_meta_data_id in (" + Batch_id + ") and b.batch_meta_data_id = cpi.batch_meta_data_id and c.batch_meta_data_id = b.batch_meta_data_id group by cpi.campaign_id,cpi.ab_test_id,cpi.batch_meta_data_id";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
try (Statement stmtABMetrics = connection.createStatement()) {
String sqlABMetrics = "select abmetrics.sent, abmetrics.open, abmetrics.click, abmetrics.signup, abmetrics.bounce, abmetrics.delivered, abmetrics.unsubscribe, abmetrics.spam, abmetrics.landing_page_hits, abmetrics.completions from ABEMAILMETRICS abmetrics where abmetrics.ab_test_id = " + rs.getString("ab_test_id") + " and abmetrics.campaign_id = " + rs.getString("campaign_id") + " and abmetrics.batch_meta_data_id = " + rs.getString("batch_meta_data_id");
ResultSet rsABMetrics = stmtABMetrics.executeQuery(sqlABMetrics);
if (!rsABMetrics.next()) {
invitationBuffer.append(rs.getString("sent")).append(",");
}
} catch (Exception e) {
logger.debug(e, e);
}
invitationBuffer.append(rs.getString("batch_name")).append(",");
invitationBuffer.append(rs.getString("body_template")).append(",").append("\n");
}
}
byte[] b = invitationBuffer.toString().getBytes("UTF-8");
Properties props = PropertiesUtil.getApplicationProperties();
StringBuffer strbufMessage = new StringBuffer();
AlertsUtil.notifyWithCSVAttachment(PropertiesUtil.getApplicationProperties().getProperty("alerts.sendUserForApplicationAlerts"), "sdsdsgamail.com", "Invitation Record ", strbufMessage, invitationBuffer, "Sample.csv", props, false);
} catch (Exception e) {
logger.debug(e, e);
}
System.out.println("Job is running it will take few mins.You will receive an email to shortly once done ");
} catch (Exception re) {
logger.debug(re, re);
}
}