Reference Guide > Custom Procedure Examples > Example 2: Simple Update
 
Example 2: Simple Update
This custom procedure participates in the parent transaction, and performs an update using the execution environment.
package proc;
 
import com.compositesw.extension.*;
import java.sql.*;
 
public class SimpleUpdate
 
implements CustomProcedure
{
 
private ExecutionEnvironment qenv;
private int numRowsUpdated = -1;
 
public SimpleUpdate() { }
 
/**
* This is called once just after constructing the class. The
* environment contains methods used to interact with the server.
*/
 
public void initialize(ExecutionEnvironment qenv) {
this.qenv = qenv;
}
 
/**
* Called during introspection to get the description of the input
* and output parameters. Should not return null.
*/
 
public ParameterInfo[] getParameterInfo() {
 
return new ParameterInfo[] {
new ParameterInfo("Id", Types.INTEGER, DIRECTION_IN),
new ParameterInfo("FirstName", Types.VARCHAR, DIRECTION_IN),
new ParameterInfo("LastName", Types.VARCHAR, DIRECTION_IN),
new ParameterInfo("CompanyName", Types.VARCHAR, DIRECTION_IN),
new ParameterInfo("PhoneNumber", Types.VARCHAR, DIRECTION_IN),
};
}
/**
* Called to invoke the stored procedure. Will only be called a
* single time per instance. Can throw CustomProcedureException or
* SQLException if there is an error during invoke.
*/
 
public void invoke(Object[] inputValues)
throws CustomProcedureException, SQLException
{
// Update in the first data source using a SQL statement
numRowsUpdated = qenv.executeUpdate(
"UPDATE /shared/tutorial/sources/ds_orders/customers" +
" SET ContactFirstName='" + inputValues[1] +
"', ContactLastName='" + inputValues[2] +
"', CompanyName='" + inputValues[3] +
"', PhoneNumber='" + inputValues[4] +
"' WHERE CustomerID=" + inputValues[0],
null);
}
 
/**
* Called to retrieve the number of rows that were inserted,
* updated, or deleted during the execution of the procedure. A
* return value of -1 indicates that the number of affected rows is
* unknown. Can throw CustomProcedureException or SQLException if
   * there is an error when getting the number of affected rows.
*/
 
public int getNumAffectedRows() {
return numRowsUpdated;
}
 
/**
* Called to retrieve the output values. The returned objects
* should obey the Java to SQL typing conventions as defined in the
* table above. Output cursors can be returned as either
* CustomCursor or java.sql.ResultSet. Can throw
* CustomProcedureException or SQLException if there is an error
* when getting the output values. Should not return null.
*/
 
public Object[] getOutputValues() {
return new Object[] { };
}
 
/**
* Called when the procedure reference is no longer needed. Close
* can be called without retrieving any of the output values (such
* as cursors) or even invoking, so this needs to do any remaining
* cleanup. Close can be called concurrently with any other call
* such as "invoke" or "getOutputValues". In this case, any pending
* methods should immediately throw a CustomProcedureException.
*/
 
public void close() { }
//
// Introspection methods
//
 
/**
* Called during introspection to get the short name of the stored
* procedure. This name can be overridden during configuration.
* Should not return null.
*/
 
public String getName() {
return "SimpleUpdate";
}
 
/**
* Called during introspection to get the description of the stored
* procedure. Should not return null.
*/
 
public String getDescription() {
return "This procedure performs a simple update operation";
}
 
//
// Transaction methods
//
 
/**
* Returns true if the custom procedure uses transactions. If this
* method returns false then commit and rollback will not be called.
*/
 
public boolean canCommit() {
return false;
}
 
/**
* Commit any open transactions.
*/
 
public void commit() { }
 
/**
* Rollback any open transactions.
*/
 
public void rollback() { }
 
 
/**
* Returns true if the transaction can be compensated.
*/
 
public boolean canCompensate() {
return false;
}
 
/**
* Compensate any committed transactions (if supported).
*/
 
public void compensate(ExecutionEnvironment qenv) { }
}