![]() | ![]() |
Home |
|
|
Access Service User's Guide DirectConnect |
|
| 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
This chapter covers the following topics:
When Sybase Adaptive Server issues an RPC event to the access service, it tries to establish a connection using the same service name as the identifier for the Sybase Adaptive Server in the sql.ini file. To set up Sybase Adaptive Server and DirectConnect connections for RPCs, see Chapter 8, "Issuing RPCs."
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 a SQL Server stored procedure that can execute a remote stored procedure (RSP). The SQL Server stored 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=@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 configure Sybase Adaptive Server for remote access, log in to Sybase Adaptive Server as an administrator and check the current sp_configure setting using the following command:
sp_configure 'remote access'
If the returned value is 1, Sybase Adaptive Server is configured for remote access.
If the returned value is 0, perform the following steps:
Enter:
sp_configure 'remote access',1
Restart Sybase Adaptive Server.
To define the DirectConnect service as a remote server, enter each DirectConnect service name in the Sybase Adaptive Server SYSSERVERS table using the following command:
sp_addserver service_name
where service_name is the name of the DirectConnect service you want to set up as a remote server. The name is case-sensitive and must match the name you used to define the connectivity between Sybase Adaptive Server and the DirectConnect service.
To verify that the DirectConnect service is successfully defined as a remote server, enter:
select service_name from sysservers
If data rows return, you successfully defined the DirectConnect service as a remote server.
When Sybase Adaptive Server issues an RPC to a DirectConnect access service, it attempts to connect using a service name identical to the Sybase Adaptive Server identifier in the interfaces file.
To support Sybase Adaptive Server RPC events, perform either one of the following procedures:
Define a service within the access service library using the same name as Sybase Adaptive Server.
Set up service name redirection to redirect the Sybase Adaptive Server to the appropriate service.
To execute the preceding Sybase Adaptive Server stored procedure example using ISQL, connect to Sybase 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 a SQL 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:
Sybase Adaptive Server determines if the remote server (in this case, directconnect) is configured as a remote server to Sybase Adaptive Server.
If the remote server is not configured, the request fails immediately.
If the remote server is configured, Sybase Adaptive Server checks for a site handler connection to the remote server.
Sybase Adaptive Server does one of the following:
If a site handler connection exists, Sybase Adaptive server connects to the remote server, triggering a connect event at the DirectConnect. If the connect event processes successfully, Sybase 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, Sybase Adaptive Server establishes one. This connection exists for the life of the RPC and is reused when Sybase Adaptive Server submits other RPCs.
Sybase 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 Sybase Adaptive Server RPC design to provide minimal network traffic.
ValidationThe user ID and password you use to sign on to Sybase 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 Sybase 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 example of a Sybase Adaptive Server stored procedure that initiates a bulk copy transfer statement. The access service executes the transfer statement against DB2, and DB2 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 Sybase Adaptive Server database and enter the following at the ISQL prompts:
C:>ISQL -Ssybone -Uuser -Ppasswrd1> execute replauth 2> go
When Sybase 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 Sybase Adaptive Server.
For more information about transfers, see Chapter 9, "Understanding the Transfer process"
You can set up any Sybase 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 Sybase Adaptive server, automatically calls an RSP named pchowdy. In turn, pchowdy updates the authors table on DB2, 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 DB2 update fails, the access service rolls back the Sybase 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.
|
|