|

楼主 |
发表于 2014-11-18 09:30:57
|
显示全部楼层
private static void synchronousHr(Iterator<Row> rows) {
// 创建连接
java.sql.Connection conn = null;
com.justep.system.data.Transaction tx = null;
CallableStatement proc = null;
try {
// 取得数据库连接
tx = new com.justep.system.data.Transaction();
//conn = ModelUtils.getConnectionInTransaction("/dngt/hr/data");
conn = tx.getConnection("/dngt/hr/data");
tx.begin();
// 存储过程对象
proc = conn.prepareCall("{ call HR_Interface_Generate1(?,?,?,?) }");
if (rows.hasNext()) {
Row row = rows.next();
String empNo = row.getString("fApplyPersonID"); //员工工号
Date startDate = row.getDateTime("fStartTime"); // 请假开始时间
Date endDate = row.getDateTime("fEndTime"); // 请假结束时间
String no = row.getString("fNo"); // 请假单号
// 日期相差天数
int dayTime = (int) ((endDate.getTime() / 86400000L) - (startDate.getTime() / 86400000L));
System.out.println("时间差" + dayTime);
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
if (dayTime == 0) {
// 同步HR
executeProcedure(conn, proc, empNo, sdf.format(startDate), getFirstDayType(startDate), no);
} else if (dayTime == 1) {
// 同步HR
executeProcedure(conn, proc, empNo, sdf.format(startDate), getFirstDayType(startDate), no);
executeProcedure(conn, proc, empNo, sdf.format(endDate), getEndDayType(endDate), no);
} else {
Calendar calendar = new GregorianCalendar();
calendar.setTime(startDate);
for (int i = 0; i < dayTime; i++) {
calendar.add(calendar.DATE, i);
if (i == 0) {
executeProcedure(conn, proc, empNo, sdf.format(startDate), getFirstDayType(calendar.getTime()), no);
} else {
executeProcedure(conn, proc, empNo, sdf.format(calendar.getTime()), "全天", no);
}
}
// 异常情况
int abc = 2 / 0;
executeProcedure(conn, proc, empNo, sdf.format(endDate), getEndDayType(endDate), no);
}
tx.commit();
}
} catch (Exception e) {
System.out.println("execute exception");
try {
tx.rollback();
} catch (SQLException ex) {
System.out.println("rollback exception");
}
} finally {
try {
if (proc != null) {
proc.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
System.out.println("close exception");
}
}
}
private static void executeProcedure(Connection conn, CallableStatement proc, String empNo, String date, String type, String no) throws Exception {
//赋值参数
proc.setString(1, empNo); //@EmpNo 工号
proc.setString(2, date); //@Date 请假时间
proc.setString(3, type); //@Type 请假半天类型
proc.setString(4, no); //@No 请假单号
proc.execute();
}
上述代码是事务管理模块,执行到异常情况时(背景蓝色部分),数据仍能入库,事务不会滚?而且finally里的关闭,也有异常,提示:
java.sql.SQLException: Connection is closed.
|
|