13.07.2015 Views

Kentico CMS Transactions Native transactions

Kentico CMS Transactions Native transactions

Kentico CMS Transactions Native transactions

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>CMS</strong> <strong>Transactions</strong><strong>Kentico</strong> <strong>CMS</strong> <strong>Transactions</strong>This document describes how to work with <strong>transactions</strong> in <strong>Kentico</strong> <strong>CMS</strong> 2.x. <strong>Transactions</strong> are usually used to ensuredatabase consistency.<strong>Native</strong> <strong>transactions</strong><strong>Kentico</strong> <strong>CMS</strong> uses SQL <strong>transactions</strong> to ensure the system database consistency within a complex operations to handlethe errors. In such cases the whole operation and its sub-operations are included in a single block of SQL commands thatcan be rolled back when error occurs or commited to the database if the operation was successful. Transaction handlingis provided by DataEngine API within class GeneralConnection.Please note that you should always use single connection for every operation within the open transaction toavoid deadlocks.The process of data access in <strong>Kentico</strong> <strong>CMS</strong> with <strong>transactions</strong> is following:Get the connectionOpen the connection ifclosedOpen transaction ifnot yet openRun the DBoperationsCommit thetransaction if localIn case of errors rollbacktransaction if openClose the connectionif localYou should always follow the default scheme and use same access when using <strong>transactions</strong>.Pass the existing connection as a method parameter if you call the method that uses DB access within transaction.1


<strong>CMS</strong> <strong>Transactions</strong>ExamplePlease use following example as a template in all the methods that work with <strong>transactions</strong>:/// /// Sample method/// /// Data connection to usepublic void DoSomethingInTransaction(GeneralConnection conn){bool closeConnectionAtTheEnd = false;bool commitTransactionAtTheEnd = false;try{// Open connection is necessaryif (!conn.DataConnection.IsOpen()){conn.DataConnection.Open();closeConnectionAtTheEnd = true;}// Start transaction if necessaryif (!conn.DataConnection.IsTransaction()){conn.DataConnection.BeginTransaction();commitTransactionAtTheEnd = true;}// --- HERE YOU CAN USE YOUR DATABASE ACCESS CODE LIKE: ----DataSet ds = conn.ExecuteQuery("cms.user.selectall", null, null, null);// --- IF YOU NEED TO USE TREEPROVIDER, ALWAYS INSTATIATE IT WITH EXISTING CONNECTION ---TreeProvider tree = new TreeProvider(null, conn);// --- ALWAYS USE METHOD OVERRIDES THAT USE CONNECTION (TREEPROVIDER) PARAMETER ---AttachmentInfo ai = DocumentHelper.GetAttachment(Guid.NewGuid(), 1, tree);// Commit transaction if necessaryif (commitTransactionAtTheEnd){conn.DataConnection.CommitTransaction();commitTransactionAtTheEnd = false;}}catch (Exception ex){// Rollback transaction if necessarryif (commitTransactionAtTheEnd){conn.DataConnection.RollbackTransaction();commitTransactionAtTheEnd = false;}throw ex;}finally{// Close the connection if necessarryif (closeConnectionAtTheEnd){conn.DataConnection.Close();closeConnectionAtTheEnd = false;}}}You may also work with TreeProvider object instead of connection object. In that case, use its Connection property thesame way like the GeneralConnection in this example and use the object in all the operations encapsulated within thetransaction.2

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!