CF and Stored Procedures Lei Wang ALP International
Main Topics Database Locks How Cold Fusion perform DB locks Stored Procedure Stress test to compare Cftransaction and stored procedure How to use stored procedure Common Mistakes
Database Locks “Lost update problem” Multiple users accessing the same data Read from database Update record User1 User2
Database Locks Blocking access for all other users User1 User2 Begin Transaction Commit Transaction Begin Transaction Commit Transaction Lock Read Value Update Value Lock Read Value Update Value Waiting
Types of Locks Table-level: supported by all database. The entire table is locked when a user is posting a transaction that utilizes a row from that table Row-level:supported by higher-end database. Only records being utilized by a transaction are locked.
How Cold Fusion perform DB Locks Use CFTRANSACTION to group multiple queries into a single unit. CFTRANSACTION also provides commit and rollback processing. ACTION="BEGIN" or "COMMIT" or "ROLLBACK" ISOLATION="Read_Uncommitted" or "Read_Committed" or "Repeatable_Read" CFLOCK
Standard usage of cftransaction(from CF Studio) INSERT INTO Courses (Number, Descript) VALUES ('#myNumber#', '#myDescription#') ……………………………………… Ending tag within the cftry block should be a cfcatch tag
Standard usage of cftransaction INSERT INTO Courses (Number, Descript) VALUES ('#myNumber#', '#myDescription#') ………………………………………
How DB tools perform Lock Transaction A logical unit of work. Begin the transaction Commit the transaction or rollback transaction
Standard usage of transaction BEGIN TRANSACTION MyTransaction SELECT * FROM roysched WHERE title_id LIKE ‘Pc%’ UPDATE roysched SET royalty = royalty * 1.10 WHERE title_id LIKE 'Pc%' COMMIT TRANSACTION MyTransaction
Stored Procedure A stored procedure is basically a precompiled program that is stored at the server site. Stored procedure is not written in SQL PL/SQL Oracle Transact-SQL Sybase & SQL Server
Advantage of Stored Procedure It enables you to write and maintain a single set of code that is utilized by all current and future applications that utilize the database It insulates the application developers from the structure of the database It can provide better performance.
Stress test LoadRunner is used to conduct 3 levels of stress test, 10, 50, 100 users. Internal network 100 M bps Cold Fusion server 5 on Windows 2000 server (1.5 Ghz CPU, 512 MB memory) SQL server 7 on the same Windows 2000 server 5 Pentium 430 Mhz Dell machines as client
Stress test results Userscftransaction Stored Procedure
What determines which tool to use Less traffic, simple web page CFTRSACTION Heavy traffic, complicated web pages Stored procedure
How to write stored procedure CREATE PROCEDURE int output AS BEGIN TRANSACTION from T_Invoice Insert into T_Invoice values( IF <>0 ROLLBACK TRANSACTION ELSE COMMIT TRANSACTION
How to use stored procedure Cfstoredproc tag <CFSTOREDPROC PROCEDURE ="procedure name" DATASOURCE ="ds_name" USERNAME="username" PASSWORD="password" DBSERVER="dbms" DBNAME="database name" BLOCKFACTOR="blocksize" PROVIDER="COMProvider" PROVIDERDSN="datasource" DEBUG="Yes/No" RETURNCODE="Yes/No">
How to user Stored Procedure Cfprocparam tag <CFPROCPARAM TYPE="IN/OUT/INOUT" VARIABLE="variable name" DBVARNAME="DB variable name" VALUE="parameter value“ CFSQLTYPE="parameter datatype“ MAXLENGTH="length" SCALE="decimal places" NULL="yes/no">
CFPROCPARAM Type: IN/OUT/INOUT CFSQLTYPE 17 total CF_SQL_INTEGER CF_SQL_CHAR CF_SQL_VARCHAR CF_SQL_DATE CF_SQL_MONEY
CFPROCPARAM DBVARNAME VARIABLE CF variable name
How to use Stored Procedure
Common mistakes when using Stored Procedure Declaration order in stored procedure is different from CFPROCPARAM order Error message Operand type clash: int is incompatible with text
Variables are not in the same int output
Common Mistakes Every In variable need a value create procedure varchar(60) as Begin FROMT_Student UPDATET_Invoice END
Every Variable need a value
Conflict with other DB tools CFTRANSACTION and TRIGGER Triggers are specialized stored procedures that are executed automatically when a particular event occurs and are used to enforce data integrity A lot of triggers have Rollback transaction command
Trigger CREATE TRIGGER Insert_T_ContactsON T_Contacts FOR int, FROMINSERTED FROMT_Contacts IF > 0 ROLLBACK TRANSACTION End
Q & A