GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing docs.voltactivedata.com — 5.2. The Anatomy of a VoltDB Stored Procedure Using VoltDB5.2. The Anatomy of a VoltDB Stored ProcedureYou can write VoltDB stored procedures as Java classes. The following code sample illustrates the basic structure of a VoltDB java stored procedure. import org.voltdb.*;
public class Procedure-name extends VoltProcedure {
// Declare SQL statements ...
public datatype run ( arguments ) throws VoltAbortException {
// Body of the Stored Procedure ...
}
}The key points to remember are to:
It is important to understand the details of how to design and develop stored procedures for your application as described in the following sections. However, for simple data access, the following techniques may suffice for some of your stored procedures:
The following sections describe the components of a stored procedure in more detail. 5.2.1. The Structure of the Stored ProcedureThe stored procedures themselves are written as Java classes, each procedure being a separate class. Example 5.1, “Components of a VoltDB Java Stored Procedure” shows the stored procedure that looks up a flight to see if there are any available seats. The callouts identify the key components of a VoltDB stored procedure. Example 5.1. Components of a VoltDB Java Stored Procedure package fadvisor.procedures; import org.voltdb.*;
5.2.2. Passing Arguments to a Stored ProcedureYou specify the number and type of the arguments that the stored procedure accepts in the public long run(int maxContestants, String contestants) { . . .VoltDB stored procedures can accept parameters of any of the following Java and VoltDB datatypes:
The arguments can be scalar objects or arrays of any of the preceding types. For example, the following
import org.voltdb.*;
public class LogMessagesByEvent extends VoltProcedure {
public long run (
long eventType,
org.voltdb.types.TimestampType[] eventTimeStamps,
String[] eventMessages
) throws VoltAbortException {The calling client application can use any of the preceding datatypes when invoking the
5.2.3. Creating and Executing SQL Queries in Stored ProceduresThe main function of the stored procedure is to perform database queries. In VoltDB this is done in two steps:
Queuing SQL Statements The first argument to The SQL statements must be declared as The remaining arguments to v SELECT CustomerID FROM Customer WHERE FirstName=? AND LastName=?; At runtime, you want the questions marks replaced by values passed in as arguments from the calling application. So
the actual public final SQLStmt getcustid = new SQLStmt( "SELECT CustomerID FROM Customer " + "WHERE FirstName=? AND LastName=?;"); ... voltQueueSQL(getcustid, firstnm, lastnm); Your stored procedure can call VoltTable[] queryresults = voltExecuteSQL(); Cycles of Queue and ExecuteYour procedure can queue and execute SQL statements in as many cycles as necessary to complete the transaction. For example, if you want to make a flight reservation, you may need to access the database and verify that the flight exists before creating the reservation in the database. One way to do this is to look up the flight, verify that a valid row was returned, then insert the reservation, like so: Example 5.2. Cycles of Queue and Execute in a Stored Procedure final String getflight = "SELECT FlightID FROM Flight WHERE FlightID=?;"; This stored procedure code to make a reservation is described as follows:
5.2.4. Interpreting the Results of SQL QueriesWith the The Figure 5.1. Array of VoltTable Structures ![]() For example, if you queue two SQL SELECT statements, one looking for the destination of a specific flight and the second looking up the ReserveID and Customer name (first and last) of reservations for that flight, the code for the stored procedure might look like the following: public final SQLStmt getdestsql = new SQLStmt(
"SELECT Destination FROM Flight WHERE FlightID=?;");
public final SQLStmt getressql = new SQLStmt(
"SELECT r.ReserveID, c.FirstName, c.LastName " +
"FROM Reservation AS r, Customer AS c " +
"WHERE r.FlightID=? AND r.CustomerID=c.CustomerID;");
...
voltQueueSQL(getdestsql,flightnum);
voltQueueSQL(getressql,flightnum);
VoltTable[] results = voltExecuteSQL();The array returned by
Assuming the stored procedure call input was a FlightID value of 134, the data returned for the second array element might be represented as follows: Figure 5.2. One VoltTable Structure is returned for each Queued SQL Statement ![]() VoltDB provides a set of convenience methods for accessing the contents of the Table 5.1. Methods of the VoltTable Classes
It is also possible to retrieve the column values by name. You can invoke any of the getDatatype() methods and pass a string argument specifying the name of the column, rather than the numeric index. Accessing the columns by name can make code easier to read and less susceptible to errors due to changes in the SQL schema (such as changing the order of the columns). On the other hand, accessing column values by numeric index is potentially more efficient under heavy load conditions. Example 5.3, “Displaying the Contents of VoltTable Arrays” shows a generic routine for “walking” through the return results of a stored
procedure. In this example, the contents of the Example 5.3. Displaying the Contents of VoltTable Arrays public void displayResults(VoltTable[] results) {
int table = 1;
for (VoltTable result : results) {
System.out.printf("*** Table %d ***\n",table++);
displayTable(result);
}
}
public void displayTable(VoltTable t) {
final int colCount = t.getColumnCount();
int rowCount = 1;
t.resetRowPosition();
while (t.advanceRow()) {
System.out.printf("--- Row %d ---\n",rowCount++);
for (int col=0; col<colCount; col++) {
System.out.printf("%s: ",t.getColumnName(col));
switch(t.getColumnType(col)) {
case TINYINT: case SMALLINT: case BIGINT: case INTEGER:
System.out.printf("%d\n", t.getLong(col));
break;
case STRING:
System.out.printf("%s\n", t.getString(col));
break;
case DECIMAL:
System.out.printf("%f\n", t.getDecimalAsBigDecimal(col));
break;
case FLOAT:
System.out.printf("%f\n", t.getDouble(col));
break;
}
}
}
}For further details on interpreting the VoltTable structure, see the Java documentation that is provided online in
the 5.2.5. Returning Results from a Stored ProcedureStored procedures can return the following types:
You can return all of the query results by returning the Whatever value the stored procedure returns, make sure the public long run( int flightid) public VoltTable[] run ( String lastname, String firstname) Note that you can interpret the results of SQL queries either in the stored procedure or in the client application.
However, for performance reasons, it is best to limit the amount of additional processing done by the stored procedure to
ensure it executes quickly and frees the queue for the next stored procedure. So unless the processing is necessary for
subsequent SQL queries, it is usually best to return the query results (in other words, the 5.2.6. Rolling Back a TransactionFinally, if a problem arises while a stored procedure is executing, whether the problem is anticipated or unexpected, it is important that the transaction rolls back. Rollback means that any changes made during the transaction are undone and the database is left in the same state it was in before the transaction started. VoltDB is a fully transactional database, which means that if a transaction (stored procedure) fails, the transaction is automatically rolled back and the appropriate exception is returned to the calling application. Exceptions that can cause a rollback include the following:
The atomicity of the stored procedure depends on VoltDB being able to roll back incomplete database changes. VoltDB relies on Java exception handling outside the stored procedure to perform the roll back. Therefore, you should not attempt to alter any exceptions thrown by the voltExecuteSql method. If your procedure code does catch exceptions thrown as a result of executing SQL statements, make sure that the exception handler re-throws the exception to allow VoltDB to perform the necessary roll back activities before the stored procedure returns to the calling program. On the other hand, there may be situations where an exception occurs in the program logic. The issue might not be
one that is caught by Java or VoltDB, but still there is no practical way for the transaction logic to complete. In these
situations, you can force a rollback by explicitly throwing the if (!flightid) { throw new VoltAbortException(); }See Section 7.3, “Verifying Expected Query Results” for another way to roll back procedures when queries do not meet necessary conditions. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |