![]() | ![]() |
Home |
|
|
Access Service User's Guide DirectConnect Anywhere |
|
| Chapter 8 Issuing RPC events |
Chapter 8
This chapter describes how any CT-Library API can generate remote procedure call (RPC) events.
This chapter covers the following topics:
The RPC feature allows a stored procedure to initiate an event in a remote database. A client API can invoke an RPC to perform the following:
Invoke an external stored procedure
Execute a language statement as an RPC
Execute a transfer request
All references to Adaptive Server Enterprise version 12.0 include Sybase SQL Server version 11.1 and later, unless otherwise specified.
Use the following example to create an Adaptive Server Enterprise stored procedure that executes a remote stored procedure (RSP). The new procedure must specify an existing RSP and provide any arguments that the RSP requires.
create procedure newcust @custname varchar(nn), @custno varchar (nn) as begin execute servername. . .addcust @addname=custname, @addno=custnoend
where:
@custname is the customer name to be added
@custno is the customer number to be added
servername specifies the access service instance to use. The three periods following servername are required.
addcust is the stored procedure name on the host.
@addname is the stored procedure representing the new customer name
@addno is the stored procedure representing the new customer number
To execute the stored procedure in the preceding example using ISQL, perform the following steps.
Connect to Adaptive Server Enterprise.
At the prompt, enter the following:
c:>ISQL -Ssybase -Uuser -Ppasswrd 1> execute newcust xxxx,yyyy2> go
where:
xxxx is the new customer name.
yyyy is the new customer number.
To execute a SQL language statement to the DirectConnect for Informix Access Service through an RPC, use the following syntax:
C:> isql -Sadaptiveserver -Uuser -Ppassword 1> execute directconnect. . .dcon "select * from user.authors" 2> go
where:
directconnect is the name of the remote server. The three periods following directconnect are required.
dcon is the keyword of the RPC.
Warning!
When using the keyword dcon, the access service will insert a space between each variable.
The access service RPC event handler is sensitive to several RPC keywords, including the keyword dcon used in this example. RPC can have many parameters. Before the resulting string is executed, all parameters are concatenated. The access service translates the first parameter into a dynamic SQL statement, submits it to the target database, and returns the result set to the client application.
For backward compatibility with the MDI Database Gateway, pcsql is recognized in place of the dcon keyword. For backward compatibility with Net-Gateway, syrt is recognized in place of the dcon keyword.
The event sequence is as follows:
Adaptive Server Enterprise determines whether the remote server directconnect is configured as a remote server.
If the remote server is not so configured, the request fails.
If the remote server is configured correctly, Adaptive Server Enterprise checks for a site handler connection to the remote server.
Adaptive Server Enterprise does one of the following:
If the site handler connection exists, Adaptive Server Enterprise connects to the remote server, triggering a connect event in DirectConnect for Informix.
If the connect event processes successfully, Adaptive Server Enterprise triggers an RPC event in DirectConnect for Informix and submits the RPC dcon. The first parameter to the RPC is the dynamic SQL language statement that is executed.
If the site handler connection does not exist, Adaptive Server Enterprise establishes one.
This connection exists for the term of the RPC. It is reused if Adaptive Server Enterprise submits other RPCs.
Adaptive Server Enterprise has a strict model for processing language statements as RPC events:
It connects to the remote server (DirectConnect for Informix) and submits the RPC.
After it processes the results, it disconnects.
These quick connects and disconnects provide minimal network traffic.
Additional rules follow in this section.
The user ID and password that you use to log on to Adaptive Server Enterprise must be a valid DirectConnect for Anywhere target database user ID and password.
All SQL transformation rules apply, including the following:
If DirectConnect for Informix is configured for passthrough mode, the SQL within the double quotes must comply with the target SQL syntax.
If DirectConnect for Informix is configured for sybase mode, the SQL must be in Sybase Transact-SQL dialect.
The following rules apply:
If the access service is configured for long transactions, the SQL submitted must not be sensitive to a commit. For example, if the SQL is an insert statement that does not batch a commit into the statement, the insert rolls back using long transaction rules.
If the access service is configured for short transactions, the SQL submitted is bound by short transactions that supply a commit by default.
The client application can create a stored procedure within Adaptive Server Enterprise that invokes the DirectConnect Server library transfer function. The service library receives the transfer command as an RPC event with the following arguments:
Name of the RPC: "transfer"
Argument 1: The secondary connection information ({to | from} "server userid pw")
Argument 2: Either the bulk copy target command or the destination-template sourceselect statement
Argument 3: Either the bulk copy sourceselect statement or the destination-template sourceselect statement.
The following example shows a stored procedure that initiates a bulk copy transfer statement. The access service executes the transfer statement against Informix, which receives it as an RPC.
create procedure replauth as begin execute servername. . .transfer "to 'servername2 userid password';", "with replace into authors;", "select * from authors;" end
where:
replauth is the name of the stored procedure
servername specifies the access service to handle the transfer. The three periods following servername are required.
The access service library recognizes anything other than "transfer" in the next position as the name of an ODBC stored procedure.
servername2 specifies the secondary database for the transfer command.
The RPC can have any number of parameters since they are concatenated and executed as a TRANSFER command.
A client may log in to the Adaptive Server Enterprise on which this procedure is defined and invoke it as follows:
execute replauth
When Adaptive Server Enterprise executes replauth, it passes an RPC to the access service. The access service returns any result rows or messages to the client application, not to Adaptive Server Enterprise.
You can set up any Adaptive Server Enterprise stored procedure as a trigger that executes automatically when the triggering condition is met.
The following example shows a trigger that calls an RSP named pcrsp when the phone column is updated in the Adaptive Server Enterprise authors table. In turn, pcrsp updates the authors table on Informix, using au_id to specify the row to update.
create trigger updatephone on authors as if update (phone) begin declare @ph varchar(14) declare @id varchar(14) declare @err int select @ph = inserted.phone from inserted select @id = inserted.au_id from inserted execute servername. . .pcrsp @phone=@ph, @au_id=@id select @err = @@error if (@err >> 0) begin print 'error _ rolling back' rollback tran end else commit tran end
Once it is created, updatephone starts up whenever phone is updated, as shown in the following example:
C:>ISQL -Ssybase -Uuser -Ppasswrd 1> update authors 2> set phone='xxx-xxx-xxxx' 3> where au_id like 'yyy-yy-yyyy' 4> go
If the Informix update fails, the DirectConnect for Informix Access Service rolls back the Adaptive Server Enterprise transaction and shows the following message:
@ERR >> 0
For more information about RPCs, see the following sources:
Sybase Open Server Server-Library/C Reference Manual
Sybase Open Client Client-Library/C Reference Manual
|
|