|

楼主 |
发表于 2014-11-19 15:38:45
|
显示全部楼层
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(?,?,?,?) }");
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
executeProcedure(proc, "123", sdf.format(new Date()), getFirstDayType(new Date()), "321");
int abc = 2/0;
executeProcedure(proc, "456", sdf.format(new Date()), getFirstDayType(new Date()), "654");
tx.commit();
} catch (Exception e) {
try {
tx.rollback();
System.out.println("execute exception:"+e.getMessage());
} catch (SQLException ex) {
System.out.println("rollback exception:"+ex.getMessage());
}
} finally {
try {
if (proc != null) {
proc.close();
}
if(conn != null){
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("close exception" + e.getMessage());
}
}
}
private static void executeProcedure(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();
}
一样写法,我执行,俩条插库操作,有一条能入库,第二条操作异常,不入库,事务没有回滚。
我不知道,你的update,是否真正检测过值变化,我这里是清楚的看到,库记录数的变化。
或者麻烦使用我的俩个方法检测。存储过程如下:
create procedure HR_Interface_Generate1
(
@EmpNo varchar(255) ,
@Date datetime,
@Type varchar(255) ,
@No varchar(255)
) as
begin
insert into HR_Leave
(EmpNo, Date, Type, No)
values
(@EmpNo, @Date, @Type, @No);
commit;
end;
|
|