![]() | ![]() |
Home |
|
|
Access Service User's Guide DirectConnect for AS/400 |
|
| Chapter 8 Issuing Remote Procedure Calls |
Chapter 8
This chapter describes how any CT-Library application can generate Remote Procedure Call (RPC) events. The RPC feature allows a stored procedure to initiate an event in a remote database, that is, to make a remote procedure call.
A client application can invoke an RPC to:
Invoke an external stored procedure
Execute a language statement as an RPC
Execute a transfer request
Any reference to Adaptive Server version 12.0 in this chapter applies to SQL Server 11.1 and later, unless otherwise specified.
This chapter covers the following topics:
To set up Adaptive Server for remote procedure calls (RPCs) against the DirectConnect service, perform the following steps:
Create an Adaptive Server user ID and password that matches a DirectConnect service host user ID and password.
For instructions on creating the ID and password, see your Adaptive Server documentation.
Configure Adaptive Server for remote access.
Define each DirectConnect service as a remote server.
Define connectivity between Adaptive Sever and the DirectConnect service.
To set up connectivity for a remote server, see the appropriate Adaptive Server documentation for the platform on which your Adaptive Server resides.
The following example shows an Adaptive Server stored procedure that can execute an AS/400 stored procedure. The Adaptive Server stored procedure must specify an existing AS/400 stored procedure and provide any arguments that the AS/400 stored procedure requires.
create procedure newcust @custname varchar(nn), @custno varchar (nn) as
begin
execute servername...addcust
@addname=custname, @addno=@custnoendwhere:
@custname is the variable representing the customer name to be added.
@custno is the variable representing the customer number to be added.
servername specifies the access service instance to use. The three periods (. . .) following the servername are required.
addcust is the stored procedure name on the host.
@addname is the stored procedure variable representing the new customer name.
@addno is the stored procedure variable representing the new customer number.
To creat an AS/400 Stored procedure refer to Chapter 14, "Using AS/400 Stored Procedures"
To execute the preceding Adaptive Server stored procedure example using ISQL, connect to Adaptive Server and enter the following at the prompts:
C:>ISQL -Ssybone -Uuser -Ppasswrd 1> execute newcust xxxx,yyyy 2> go
where:
xxxx is the new customer name, such as Ajax Printing Company.
yyyy is the new customer number, such as 1234.
For backward compatibility, the following syntax is acceptable: use procedure newcust xxxx,yyyy
To execute a SQL language statement to the access service through an Adaptive Server RPC, use the following syntax:
C:> isql -Ssqlserver -Uuser -Ppassword
1> execute directconnect...dcon "select * from
user.authors"
2> gowhere:
directconnect is the name of the remote server. The three periods (. . .) following directconnect are required.
dcon is the special name, or keyword, of the RPC.
The access service RPC event handler is sensitive to several key RPC names. In this case, the RPC keyword dcon is a special name that DirectConnect recognizes. As a result, the access service translates the first parameter into a dynamic SQL statement, submits it to the target database, and then 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.
To execute a language statement as an RPC:
Adaptive Server determines if the remote server (in this case, directconnect) is configured as a remote server to Adaptive Server.
If the remote server is not configured, the request fails immediately.
If the remote server is configured, Adaptive Server checks for a site handler connection to the remote server.
Adaptive Server does one of the following:
If a site handler connection exists, Adaptive Server connects to the remote server, triggering a connect event at the DirectConnect. If the connect event processes successfully, Adaptive Server triggers an RPC event at DirectConnect and submits the RPC dcon. The first parameter to the RPC is the dynamic SQL language statement that is executed.
If a site handler connection does not exist, Adaptive Server establishes one. This connection exists for the life of the RPC and is reused when Adaptive Server submits other RPCs.
Adaptive Server has a strict model for processing language statements as RPC events: It connects to the remote server (DirectConnect) and submits the RPC. After results process, it disconnects.
These quick connects and disconnects are a basic part of the Adaptive Server RPC design to ensure minimal network traffic.
ValidationThe user ID and password you use to sign on to Adaptive Server must be a valid DirectConnect target database user ID and password.
Transformation mode and syntaxAll SQL transformation rules apply:
If DirectConnect is configured for Passthrough mode, the SQL within the double quotes must comply with target SQL syntax.
If DirectConnect is configured for Sybase mode, the SQL must be in Sybase Transact-SQL dialect.
Consider the following:
If DirectConnect is configured for long transactions, the SQL submitted must not be sensitive to a commit. In other words, 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, which supply a commit by default.
The client application can create a stored procedure within Adaptive Server that invokes the access service transfer function. In this case, the access service receives the transfer command as an RPC event with the arguments shown in the following table:
Argument | Definition |
Argument 1 | The secondary connection information ({to | from} "server userid pw") |
Argument 2 | Either the bulk copy target command (for example, with replace into) or the destination-template sourceselectstatement |
Argument 3 | Either the bulk copy sourceselectstatement or the destination-template sourceselectstatement |
Following is an ex ample of an Adaptive Server stored procedure that initiates a bulk copy transfer statement. The access service executes the transfer statement against the AS/400, and the AS/400 receives it as an RPC.
create procedure replauth as
begin
execute servername...transfer "to 'servername2 userid password';",
"with replace into authors;",
"select * from authors;"
endwhere:
replauth is the name of the stored procedure.
servername specifies the access service that handles the transfer. The three periods (. . .) following the servername are required. The access service recognizes anything other than transfer in the next position as the name of a stored procedure.
transfer is the name of the RPC.
servername2 is the secondary database for the transfer command.
As shown in the following example, to execute the replauth stored procedure using ISQL, connect to the Adaptive Server database and enter the following at the ISQL prompts:
C:>ISQL -Ssybone -Uuser -Ppasswrd1> execute replauth 2> go
When Adaptive Server executes replauth, it passes an RPC to the access service.
The access service then returns any result rows or messages to the client application, not to Adaptive Server.
For more information about transfers, see Chapter 9, "Understanding the Transfer process"
You can set up any Adaptive Server stored procedure as a trigger that executes automatically when the triggering condition is met.
The following example shows a trigger, which, when the phone column is updated in the authors table in Adaptive Server, automatically calls an AS/400 stored procedure named pchowdy. In turn, pchowdy updates the authors table on the AS/400, using au_id to specify the row to update.
create trigger updphone 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...pchowdy @phone=@ph,
@au_id=@id
select @err = @@error
if (@err >> 0)
begin
print 'error _ rolling back'
rollback tran
end
else
commit tran
endOnce it is created, the updphone trigger starts up whenever phone is updated, as shown:
C:>ISQL -Ssybone -Uuser -Ppasswrd 1>update authors 2>set phone='xxx-xxx-xxxx' 3>where au_id like 'yyy-yy-yyyy' 4>go
The access service does not support a two-phase commit. The update can succeed and be committed on either platform, independently of the success or failure of the update on the other platform.
In the previous example, if the AS/400 update fails, the access service rolls back the Adaptive Server transaction and shows the following message:
@ERR >> 0
For more information about RPCs, see the Sybase Open Server Server-Library/C Reference Manual or the Sybase Open Client Client-Library/C Reference Manual.
|
|