After spending almost 2 months at work without actually doing anything that barely resembles programming, I was finally sent to a training at an outsourced training company in Makati.
The training is about Windows Communication Framework or WCF — Microsoft’s answer to building Service-Oriented Architectures (SOA). Gone were the days of Web Services, more so, socket programming! It’s all abstracted now. Yay for lame programmers!
But anyway, the training was GREAT! I was able to learn the ins and outs of WCF. But what’s interesting is that I learned a new “way” to do rollbacks automatically! It’s called…

Let’s say that you have a Point of Sales (POS) system that have 2 tables namely Inventory table and Sales table. And you have a method called CustomerBuysStuff(int ItemID). The said method will be called if a customer buys new stuff (obviously)… so the implementation would be something like:
- Deduct 1 unit from the Inventory table using the ItemID
- Add 1 unit from the Sales table using the ItemID
This means that this particular method must execute two (2) SQL statements and both of them must SUCCEED or you will have a decrepancy on your data. If you can remember from your Database Management Course, you have studied a concept called ACID. This scenerio represents the A in ACID — Atomic Operations. In our case, this means that “All statements in a group must execute, or no statement in a group must execute. “
Frequently in a database application you come across a situation where you need to execute two or more SQL commands in such a way that if any one of the statements fails, then no other statement will be able to change the database.
I’ll try to demonstrate Transactions using REAL code (Click the image to enlarge):

Let’s call the code snippet as “No Rollback”. This assumes that you are 101% sure that both operation will succeed that you did not even bother to write a rollback method if ever one of the two fails. This is a very bad practice in the real world because it will create a discrepancy in your data that cannot be traced — and worst, debugged.
The above code snippet is called “With Rollback”, because in this code snippet, when the second command fails, you cared enough to rollback the changes made by the first command. This is how I did it in the college days when I’m too lazy to use Stored Procedures (and TRANS/COMMIT) and I’m too stupid to not have researched about Transactions! Oh the dark ages. Mind you, this method is not 100% fool-proof. What will happen if your rollback command also failed? Discrepancy!
Now, let’s call this code snippet “Almighty Transaction”. In the above code, there is no extra method or SQL command to invoke to do the rollback when the pre-requisite part of your code fails. And oh, there’s also no extra code to detect an exception or an error. It just magically knows that something awful had happened and it will just magically rollback ALL the changes that happened before the error. Like, MAGICALLY! Isn’t it freakin great?!
Also, this will work on ANYTHING (Not only on Database Related Stuffs). During my thesis days, I have to write a method to perform rollbacks when my encryption/decyption algorithm fails. It took me a whole week and lots of messy code to make it work perfectly. If only I have the knowledge of Transactions then, it could have just taken me 5 minutes. DAMN!
So yeah… I love these TRAININGS (Have 6 more to go for this year)! You can learn lots of useful stuffs in a very short span of time (Like 3 days). They will even give you a certificate that is signed by Billy-G himself! Haha! Another additional line in my Resume.
What’s great about the training company that my company chose is that they are official Microsoft Partners… This means that I can take the official WCF Online Test and if I pass, I will then be a “Certified” MCP (Microsoft Certified Professional)!
Remember…
TransactionScope AtomicScope = new TransactionScope() using(AtomicScope) { //ATOMIC OPERATIONS AtomicScope.Complete(); }




























































Popular Posts