Java 这实际上是资源泄漏吗?
我有一个在MySQL数据库中建模的“邀请”对象。此对象有一个列表(“treatmentPlanIDsToCopyf”),并在数据库中与第二个表一起维护。下面是我编写的方法,将插入到主表中,然后在列表中循环,并将列表中每个项目的记录插入到第二个表中。在Java 这实际上是资源泄漏吗?,java,mysql,Java,Mysql,我有一个在MySQL数据库中建模的“邀请”对象。此对象有一个列表(“treatmentPlanIDsToCopyf”),并在数据库中与第二个表一起维护。下面是我编写的方法,将插入到主表中,然后在列表中循环,并将列表中每个项目的记录插入到第二个表中。在ps=cn.preparest陈述(sql)行Eclipse给了我一个警告,说“资源泄漏:'ps'在此位置未关闭”。我正在关闭finally子句中准备好的语句,因此我想知道是否确实存在需要修复的资源泄漏。这是我第一次使用带有准备语句的批处理,所以我不
ps=cn.preparest陈述(sql)行代码>Eclipse给了我一个警告,说“资源泄漏:'ps'在此位置未关闭”。我正在关闭finally
子句中准备好的语句,因此我想知道是否确实存在需要修复的资源泄漏。这是我第一次使用带有准备语句的批处理,所以我不是很确定。谢谢
public void invitationCreate(Connection cn, Invitation invitation) throws SQLException{
PreparedStatement ps = null;
try {
//first insert primary invitation data into the invitation table
String sql = "INSERT INTO invitiation (invitation_code, recipient_email, sender_user_id_fk, date_intived, date_accepted, accepted, recipient_first_name, recipient_last_name) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
ps = cn.prepareStatement(sql);
ps.setString(1, invitation.getInvitationCode());
ps.setString(2, invitation.getRecipientEmail());
ps.setInt(3, invitation.getSenderUserID());
ps.setTimestamp(4, convertLocalTimeDateToTimstamp(invitation.getDateInvited()));
ps.setTimestamp(5, convertLocalTimeDateToTimstamp(invitation.getDateAccepted()));
ps.setBoolean(6, invitation.isAccepted());
ps.setString(7, invitation.getRecipientFirstName());
ps.setString(8, invitation.getRecipientLastName());
int success = ps.executeUpdate();
//now loop through all the treatmentPlanIDs in the invitation that are to be copied into the invitees account when the register
sql = "INSERT INTO invitation_treatment_plans (invitation_code_fk, invitation_treatment_plan_id_fk) VALUES (?, ?)";
ps = cn.prepareStatement(sql);//TODO confirm this if this is actually a resource leak
for(int treatmentPlanID : invitation.getTreatmentPlanIDsToCopy()){
ps.setString(1, invitation.getInvitationCode());
ps.setInt(2, treatmentPlanID);
ps.addBatch();
}
ps.executeBatch();
} finally {
DbUtils.closeQuietly(ps);
}
}
我相信泄漏是在第一份准备好的声明中
在int success=ps.executeUpdate()之后代码>在将变量分配给新的准备语句之前,需要关闭该准备语句。我相信泄漏在第一个准备语句中
在int success=ps.executeUpdate()之后代码>在将变量分配给新的准备语句之前,需要关闭该准备语句