Batching insert and update statements is a technique available in Java for reducing the number of database calls. Unfortunately, this technique is not available in EGL.
java.sql.PreparedStatement's addBatch() method adds a set of parameters to a batch. Subsequently, the batch is submitted to the database by invoking the executeBatch() method on the prepared statement object. The protocol is illustrated by the implementation of the following EGL external type:
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import com.ibm.javart.JavartException;
import com.ibm.javart.resources.Program;
public class DntPreparedStatement extends PreparedStatementWrapper {
private PreparedStatement pstmt;
public DntPreparedStatement(){
}
public DntPreparedStatement(Program prog){
try {
ezeProgram = prog;
Connection con = com.ibm.javart.sql.Sql.begin(ezeProgram,
"EXECUTE", null).getConnection();
pstmt = con
.prepareStatement(
"insert into BCDB.TB_BCDB_DNT (ID_BCDB_DTH,CNT,DNM,OPERATOR_I,CPN_ID)values"
+ "(?,?,?,?,?)");
} catch (JavartException e) {
e.printStackTrace();
System.exit(-1);
} catch (SQLException e) {
e.printStackTrace();
System.exit(-1);
}
}
public void addDnt(BigInteger dth_id, BigDecimal cnt, String dnm,
String op, String cpn_id) {
try {
BigDecimal dnm_bd = null;
BigDecimal user_Cd = null;
BigDecimal cpn = null;
try {
dnm_bd = new BigDecimal(dnm);
} catch (Exception e) {
}
try {
user_Cd = new BigDecimal(op);
} catch (Exception e) {
}
try {
cpn = new BigDecimal(cpn_id);
} catch (Exception e) {
}
pstmt.setBigDecimal(1, new BigDecimal(dth_id));
pstmt.setBigDecimal(2, cnt);
pstmt.setBigDecimal(3, dnm_bd);
pstmt.setBigDecimal(4, user_Cd);
pstmt.setBigDecimal(5, cpn);
pstmt.addBatch();
} catch (SQLException e) {
e.printStackTrace();
System.exit(-1);
}
}
public void do_it(){
try {
pstmt.executeBatch();
int count = pstmt.getUpdateCount();
com.ibm.javart.sql.Sql.end( ezeProgram,
"EXECUTE",
null,
count,
pstmt,
true,
true,
false,
false
);
} catch (JavartException e) {
e.printStackTrace();
System.exit(-1);
} catch (SQLException e) {
e.printStackTrace();
System.exit(-1);
}
}
}
Error handling is certainly not production-grade, but suffices for my performance tests.
The EGL code makes use of DntPreparedStatement as follows:
ExternalType Dnt extends Serializable type JavaObject
{
packageName="com.kbc.n01.ftn.pvt",
javaName="DntPreparedStatement"
}
constructor();
function addDnt(
id num(20) in,
cnt num(15,2) in,
dnt_Id unicode(10) in,
user_Cd unicode(10) in,
cpn_Cst_No unicode(10) in);
function do_it();
end
?
function b020_Str_Dnts()
//--------------------------
dnt Dnt = new Dnt();
for (wn0f49.wix.dnt_Lst_Idx from 1 to in0f49.ipt_Zn.data.dnt_Lst.getSize())
if (in0f49.ipt_Zn.data.dnt_Lst[wn0f49.wix.dnt_Lst_Idx].cnt > 0)
if (in0f49.ipt_Zn.data.dnt_Lst[wn0f49.wix.dnt_Lst_Idx].dnt_Id > " ")
cnt decimal(15,2) =
in0f49.ipt_Zn.data.dnt_Lst[wn0f49.wix.dnt_Lst_Idx].cnt;
dnt.addDnt(
wn0f49.wwv.dth_Id,
cnt,
in0f49.ipt_Zn.data.dnt_Lst[wn0f49.wix.dnt_Lst_Idx].dnt_Id,
in0f49.ipt_Zn.hdr.user_Cd,
in0f49.ipt_Zn.hdr.cpn_Cst_No);
else
z120_Add_Am_Ext();
end
end
end
dnt.do_it();
end
Due to processing by IBM, this request was reassigned to have the following updated attributes:
Brand - Servers and Systems Software
Product family - Programming Languages
Product - Business Developer
For recording keeping, the previous attributes were:
Brand - Rational
Product family - Design & development
Product - Business Developer
The feature has been implemented in 9.0
shipped in RBD 9.0
Thank you for this suggestion. We agree that this would improve runtime performance. This is a candidate for implementation in our next release.