67,513
社区成员
发帖
与我相关
我的任务
分享
public class LockDaoEx extends JdbcDaoSupport {
private static final Log log = LogFactory.getLog(LockDaoEx.class);
private UpdateLock updateLock;
protected void initDao() throws Exception {
updateLock = new UpdateLock(getDataSource());
}
public int batchUpdate(Object[] params) {
log.debug("BatchingBatcher#batchUpdate() start.");
int ret = 0;
try {
ret = updateLock.batchUpdate(params);
} catch (DataAccessException _ex) {
throw _ex;
} finally {
log.debug("BatchingBatcher#batchUpdate() end.");
}
return ret;
}
private static class UpdateLock extends SqlUpdate {
private static final String sql = ResourceBean.getResourceStr(
MPEConstant.QUERY_PROP_LOCATION, "UPDATE_LOCK");
public UpdateLock(DataSource ds) {
super(ds, sql);
// SQL実行タイムアウトの設定
this.setJdbcTemplate(new JdbcTemplate(ds) {
protected void applyStatementSettings(Statement state)
throws SQLException {
state.setQueryTimeout(MPEConstant.QUERY_TIMEOUT);
}
});
declareParameter(new SqlParameter(Types.CHAR));
declareParameter(new SqlParameter(Types.CHAR));
declareParameter(new SqlParameter(Types.CHAR));
compile();
}
protected int batchUpdate(Object[] params) throws DataAccessException {
if (params != null) {
String[] sqlArray = new String[params.length];
for (int i = 0; i < params.length; i++) {
sqlArray[i] = MPEDBUtil.getFullSqlString(
(Object[]) params[i], sql);
}
return flush(sqlArray);
}
return -1;
}
private int flush(String[] sql) {
int[] affected = getJdbcTemplate().batchUpdate(sql);
int totalRowsAffected = 0;
for (int i = 0; i < affected.length; i++) {
totalRowsAffected += affected[i];
}
return totalRowsAffected;
}
}
public static void main(String[] args) {
LockDaoEx updateLock = (LockDaoEx) SpringWrpCmp.getComponent("updateLock");
Object[] orgs1 = new Object[] { "T_SGYOTEI ", "yinwh ",
MPEDBUtil.fillingAfterStr("1", " ", 20) };
Object[] orgs2 = new Object[] { "T_SGYOTEI ", "yinwh ",
MPEDBUtil.fillingAfterStr("2", " ", 20) };
Object[] orgs3 = new Object[] { "T_SGYOTEI ", "yinwh ",
MPEDBUtil.fillingAfterStr("3", " ", 20) };
System.out.println(""
+ updateLock.batchUpdate(new Object[] { orgs1, orgs2, orgs3 }));
}
}