CREATE PROCEDURE FROM CLASS — Defines a stored procedure associated with a Java class.
CREATE PROCEDURE
[PARTITION ON TABLE table-name COLUMN column-name [PARAMETER
position]]
[ALLOW role-name [,...]]
FROM CLASS class-name
CREATE PROCEDURE DIRECTED
[ALLOW role-name [,...]]
FROM CLASS class-name
You must declare stored procedures to make them accessible to client applications and the sqlcmd utility. CREATE PROCEDURE FROM CLASS lets you declare stored procedures that are written as Java classes.The class-name is the name of the Java class.
Before you declare the stored procedure, you must create, compile, and load the associated Java class. It is usually easiest to do this by compiling all of your Java stored procedures and packaging the resulting class files into a single JAR file that can be loaded once. For example:
$ javac -d ./obj src/procedures/*.java $ jar cvf myprocs.jar –C obj . $ sqlcmd 1> load classes myprocs.jar; 2> CREATE PROCEDURE FROM CLASS procedures.AddCustomer;
You can create three types of stored procedures:
Multi-Partition Procedures — By default, the CREATE PROCEDURE statement declares a multi-partition procedure. A multi-partition procedure runs as a single transaction and has access to data from the entire database. However, it also means that the procedure will access all of the partitions at once, blocking the transaction queues until the procedure is done.
Single-Partition Procedures — If you include the PARTITION ON clause, the procedure is partitioned and runs on only one partition of the database. The partition it runs on is determined by the value of one of the parameters you pass to the procedure at runtime, as described below.
Directed Procedures — if you include the DIRECTED clause, the procedure is a directed
procedure and will run separate transactions on each of the partitions. However, the individual transactions are not
coordinated. Directed procedures must be invoked as a scheduled task or using the
callAllPartitionProcedure
method. See Section 7.5, “Directed Procedures: Distributing Transactions to Every Partition” and the description of
the CREATE TASK statement for more information on directed procedures.
When creating single-partitioned procedures, you specify the partitioning in the PARTITION ON clause. Partitioning a stored procedure means that the procedure executes within a unique partition of the database. The partition in which the procedure executes is chosen at runtime based on the table and column specified by table-name and column-name. By default, VoltDB uses the first parameter to the stored procedure as the partitioning value. However, you can use the PARAMETER clause to specify a different parameter. The position value specifies the parameter position, counting from zero. (In other words, position 0 is the first parameter, position 1 is the second, and so on.)
The specified table must be a partitioned table and cannot be an export stream or replicated table.
If security is enabled at runtime, only those roles named in the ALLOW clause (or with the ALLPROC or ADMIN permissions) have permission to invoke the procedure. If security is not enabled at runtime, the ALLOW clause is ignored and all users have access to the stored procedure.
The following example declares a stored procedure matching the Java class MakeReservation. Note that the class name includes its location within the current class path (in this case, as a child of flight and procedures). However, the name itself, MakeReservation, must be unique within the schema because at runtime stored procedures are invoked by name only.
CREATE PROCEDURE FROM CLASS flight.procedures.MakeReservation;