Chapter 9. Database Transactions

In the previous chapter, we continued our use of Migratus migration scripts to create the bleeding-edge hipstr catalog (the artists and albums tables). We also created some YeSQL functions that pulled the catalog data from the database, and rendered it to the screen with such beauty and poise that I wouldn't be surprised if we got a job tomorrow at a rather massive Silicon Valley company.

This chapter will continue along the same vein as the previous: more coding, fewer fun ogre boring things. In this chapter we will cover the following topics:

  • Learning what a database transaction is, and why it's important
  • How database transactions look in Clojure, and how they can be applied when using YeSQL
  • Creating a form that accepts an artist name, album name, and release date, and how to store it in the database using a transaction

This chapter will use everything we've learned so far in the book, including validations using Validateur, routes using Compojure, and YeSQL for more goodness. But before we get started we should probably review what a database transaction is.

Introduction to Database Transactions

Sometimes I like to see what the internet says about a particular topic, and then spend a few hours trying to make sense of it. Let's take a look at what Wikipedia says about database transactions:

 

"A transaction comprises a unit of work performed within a database management system (or similar system) against a database, and treated in a coherent and reliable way independent of other transactions… A database transaction, by definition, must be atomic, consistent, isolated and durable."

 
 --http://en.wikipedia.org/wiki/Database_transaction

I'm not going to lie. I hate computer jargon. It's always a mouthful, and, more often than not, leaves me confused. The essence of the preceding quote can be put simply thus: A database transaction is a bunch of work which is either completed in its entirety or isn't performed at all, and, in the process, doesn't screw anything else up. It's all-or-nothing, while respecting its neighbors. That's the easy version. Read on for the more in-depth version.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.134.17