Stored Procedures and Remote Procedures Call (RPC)
Stored Procedures and Remote Procedures Call (RPC)
Call (RPC)
Stored
Procedures
Stored Procedures
• The major database vendors are now offering an RPC-like mechanism for
executing functions that are stored in their databases. This mechanism is
sometimes referred to as “TP-Lite” or “stored procedure”.
• A stored procedure is typically treated like any other database object and
registered in the SQL catalog. The access to the stored procedure is controlled
through the server’s security mechanisms.
Stored Procedures
They are:
1. Used to enforce business rules and data integrity
2. Perform system maintenance and administration functions
3. To extend the database server’s functions
4. Used to create the server side of application logic.
5. It is also well suited for creating performance-critical applications
known as Online Transaction Processing OLTP
Stored Procedures
• Reduced network traffic: When we use stored procedures instead of writing T-SQL queries at the
application level, only the procedure name is passed over the network instead of the whole T-SQL code.
• Reusable: Stored procedures can be executed by multiple users or multiple client applications without the
need of writing the code again.
• Security: Stored procedures reduce the threat by eliminating direct access to the tables. we can also
encrypt the stored procedures while creating them so that source code inside the stored procedure is not
visible. Use third-party tools like ApexSQL Decrypt to decrypt the encrypted stored procedures.
• Performance: The SQL Server stored procedure when executed for the first time creates a plan and stores
it in the buffer pool so that the plan can be reused when it executes next time.
Stored Procedures
Example: Database
Below is a selection from the "Customers" table in the Northwind sample database:
CustomerID CustomerName ContactName Address City PostalCode Country
3 Antonio Moreno Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
Taquería
4 Around the Horn Thomas Hardy 120 Hanover Sq. London WA1 1DP UK
2. When the procedure finishes and produces its results, its results are
transferred back to the calling environment, where execution resumes as if
returning from a regular procedure call.
Remote Procedures Call (RPC)
NOTE: RPC is especially well suited for client-server (e.g. query-response) interaction in which the flow
of control alternates between the caller and callee. Conceptually, the client and server do not both execute
at the same time. Instead, the thread of execution jumps from the caller to the callee and then back again.
Working of RPC
Remote Procedures Call (RPC)
ADVANTAGES :
1. RPC provides ABSTRACTION i.e message-passing nature of network
communication is hidden from the user.
2.RPC often omits many of the protocol layers to improve performance. Even a
small performance improvement is important because a program may invoke
RPCs often.
3.RPC enables the usage of the applications in the distributed environment, not
only in the local environment.
4.With RPC code re-writing / re-developing effort is minimized.
5.Process-oriented and thread oriented models supported by RPC.
Remote Procedures Call (RPC)
RPC ISSUES :
Issues that must be addressed:
1. RPC Runtime:
RPC run-time system is a library of routines and a set of services that handle the network
communications that underlie the RPC mechanism. In the course of an RPC call, client-side and
server-side run-time systems’ code handle binding, establish communications over an appropriate
protocol, pass call data between the client and server, and handle communications errors.
2. Stub:
The function of the stub is to provide transparency to the programmer-written application code.
•On the client side, the stub handles the interface between the client’s local procedure call and the run-
time system, marshaling and unmarshaling data, invoking the RPC run-time protocol, and if requested,
carrying out some of the binding steps.
•On the server side, the stub provides a similar interface between the run-time system and the local
manager procedures that are executed by the server.
Remote Procedures Call (RPC)
3. Binding: How does the client know who to call, and where the service resides?
The most flexible solution is to use dynamic binding and find the server at run time when
the RPC is first made. The first time the client stub is invoked, it contacts a name server to
determine the transport address at which the server resides.
4. The call semantics associated with RPC : It is mainly classified into following choices-
• Retry request message –Whether to retry sending a request message when a server has
failed or the receiver didnt receive the message.