13.07.2015 Views

Kentico CMS Transactions Native transactions

Kentico CMS Transactions Native transactions

Kentico CMS Transactions Native transactions

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<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!