小弟是往oracle数据库里插入数据,Connection 是从DBCP里拿到的,在插入后对Connection和PreparedStament都有作finally{.close()}处理,但是发现oracle中v$session 存在很多inactive session。是我没正确关闭吗?还忘高人指点
public class TempDAO {
private Connection con;
private PreparedStatement ptst;
public TempDAO() {
init();
}
private void init() {
DBCP dbcp = new DBCP();
con = dbcp.getCon();
}
public void insertTempa(List<Tempa> tempaLists) {
String sql = "insert into gps_gsp_test(gps_time,temp1,temp2,vehicle_number,entry_time) values(?,?,?,?,?)";
try {
/**
* 这里不能con.prepareStatement(sqlStr,Statement.RETURN_GENERATED_KEYS)赋值
* Statement.RETURN_GENERATED_KEYS是获取最大主键值,若数据库表没有主键,批量处理会有问题!
*/
ptst = con.prepareStatement(sql);
for(Tempa temp : tempaLists) {
ptst.setTimestamp(1,new Timestamp(temp.getCollectTime().getTime()));
ptst.setDouble(2,temp.getTemp1());
ptst.setDouble(3,temp.getTemp2());
ptst.setString(4,temp.getCarNo());
ptst.setTimestamp(5,new Timestamp(temp.getEntryTime().getTime()));
ptst.addBatch();
// ptst.executeUpdate();
}
ptst.executeBatch();
con.commit();
ptst.clearBatch();
System.out.println("数据插入成功!");
} catch (SQLException e) {
e.printStackTrace();
try {
con.rollback();
System.out.println("数据插入失败!");
} catch (SQLException e1) {
e1.printStackTrace();
}
} finally{
if(ptst != null){
try {
ptst.close();
ptst = null;
} catch (SQLException e) {
e.printStackTrace();
} finally {
if(con != null){
try {
con.close();
con = null;
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}