Use JDBC to batch modify Oracle sequence step size

Scenes

After the data migration is completed, the sequence in the library needs to be reset.

method

Three common ways

delete--rebuild

Simple sequence reconstruction in this way will affect the business logic processing and cannot adapt to the inconsistency of the original sequence parameter rules, but if the sequence parameters are consistent, this method is relatively simple and efficient.

Temporarily modify the sequence step size, and then restore (PL/SQL uses this method)

In this way, the target value is obtained by first modifying the sequence step size and then through the NEXTVAL auto-increment sequence, and finally restoring the sequence step size to the original value, as follows SQL

  1. ALTER SEQUENCE SEQ_ACCOUNT_BALANCE INCREMENT BY 1000;  
  2. SELECT SEQ_ACCOUNT_BALANCE.NEXTVAL FROM DUAL;  
  3. ALTER SEQUENCE SEQ_ACCOUNT_BALANCE INCREMENT BY 1; 
  4. This method is often used when modifying a specific sequence, but if the sequence is operated in batches, the operation step is one step more than the previous one.

    Cyclic incrementing sequence value

    This method is very safe but inefficient, the following SQL:

  5. FOR I IN 1 .. 1000 LOOP  
  6. SELECT SEQ_ACC_ID.NEXTVAL INTO SAI FROM DUAL;  
  7. END LOOP; 

 

My project needs:

To migrate data, the initial value of the sequence is the maximum value of the primary key ID corresponding to the sequence in the data table. Since the table and sequence are not related, I correspond the table, sequence and primary key value in the configuration file. As shown in the figure:


The primary key ID, sequence and table fields are encapsulated into an entity class, then read and stored in List.

Convert the value of the List into an executed SQL statement. Here, if a sequence changes the initial value, the deletion sequence and the creation sequence are executed at the same time. I put the two statements that execute a sequence in an array, and multiple sequences are collection of arrays

Controller.java

//start changes the initial value of the sequence

try {

//retrieve data

List<SequenceMessage> seqmessageList = ParseXml.getSequenceList();

Map<String, List<String>> resultmap = baseservice.doWork(seqmessageList);

 

logsb.append("The initial value of the sequence changes <BR>Total number of sequences"+seqmessageList.size()+"<br>");

 

 

List<String> falseResult = resultmap.get("false");

List<String> trueResult = resultmap.get("true");

logsb.append("There are "+falseResult.size()+" execution sequence errors, "+trueResult.size()+" execution sequences are correct, no data table"+(seqmessageList.size()-falseResult.size ()-trueResult.size())+"<br>");

 

if(falseResult.size() > 0){

logsb.append("Failed to execute the following statement:<br>");

for (int i = 0; i < falseResult.size(); i++) {

logsb.append(falseResult.get(i)+"<br>");

}

}

if(trueResult.size() >0){

logsb.append("The following statements are executed correctly: <br>");

for (int i = 0; i < trueResult.size(); i++) {

logsb.append(trueResult.get(i)+"<br>");

}

}

 

 

} catch (Exception e) {

logsb.append("An exception occurs when changing the initial value of the sequence: "+e.toString()+"<br><br>");

e.printStackTrace ();

}

baseservice.java

public Map<String, List<String>> doWork(List<SequenceMessage> seqmessageList){

Connection conn = getConnection();

Map<String, List<String>> resultmap = new HashMap<String, List<String>>();

List<String> falseResult = new ArrayList<String>();

List<String> trueResult = new ArrayList<String>();

 

//start to get the relationship between table and sequence and field

List<String[]> seqDDLArray = new ArrayList<String[]>();

for (int i = 0; i < seqmessageList.size(); i++) {

SequenceMessage seqmsg = seqmessageList.get(i);

String id = seqmsg.getId();

String tname = seqmsg.getTablename();

String seqname = seqmsg.getSeqname();

String maxsql = "select max(" + id + ") from " + tname;

// Execute to get the maximum value in the corresponding table according to the primary key ID

Object[] obj = null;

try {

obj = (Object[]) basedao.queryForArrayList(maxsql).get(0);

String objstr = obj[0] + "";

if (!"null".equals(objstr)) {

seqDDLArray.add(rebuildSEQ(seqname,Integer.parseInt(objstr)));

}

} catch (Exception e) {

e.printStackTrace ();

falseResult.add(e.toString());

continue;

}

 

}

 

//start loop to execute delete sequence and create sequence

for (int i = 0; i < seqDDLArray.size(); i++) {

//The same sequence delete and create are executed at the same time

String[] sqlattr=seqDDLArray.get(i);

try {

boolean flag =basedao.updateBatch(sqlattr, conn);

if(!flag){

for (int j = 0; j <sqlattr.length; j++) {

falseResult.add(sqlattr[j]);

}

}else{

for (int j = 0; j <sqlattr.length; j++) {

trueResult.add(sqlattr[j]);

}

}

} catch (Exception e) {

e.printStackTrace ();

continue;

}

 

}

 

resultmap.put("false", falseResult);

resultmap.put("true", trueResult);

DbUtils.closeQuietly(conn);

return resultmap;

}

 basedao.java

/**

* Execute batch

* @param sqlArray

* @throws SQLException

*/

public boolean updateBatch(String[] sqlArray, Connection conn)

throws SQLException {

boolean flag = false;

 

conn.setAutoCommit(false);

Statement stmt = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,

ResultSet.CONCUR_READ_ONLY);

for (String tmpSql : sqlArray) {

stmt.addBatch(tmpSql);

}

int[] rtnArr = stmt.executeBatch();

conn.commit();

if (rtnArr.length > 0) {

flag = true;

}

 

return flag;

 

}

 

The return type is because I need to return the background code to the front-end display, so I spliced ​​the code directly in the background. The code should be understandable

My code is to perform a sequence of operations unsuccessfully, continue to the next step, and return the error and correct execution to the client for display

The interface is not displayed

 

See http://blog.csdn.net/a316212802/article/details/40297115

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326391128&siteId=291194637