Sunday, June 10, 2012

Transactions

I knew that it would take more than 7 weeks to write this blog :-)

Today I would like to focus on another aspect of those databases - how do they handle transactions?

For an RDBMS it is quite common to support transactions, but I haven't read about transactions in the 7db7w book yet that those key-value databases support them.

Let's write a test for transactions:

package sevendatabases

import static org.junit.Assert.*
import org.junit.*

class TransactionTests extends GroovyTestCase {

    static transactional = false; // 1
   
    void setUp() {
    }
   
    @Test
    void testSomething() {
        def numBooks = Book.count()
        Book.withTransaction { status ->
            new Book (name:'test',isbn:'test').save(flush:true,failOnError:true)
        }
        assert Book.count() == numBooks+1
        Book.withTransaction { status ->
            new Book (name:'test',isbn:'test').save(flush:true,failOnError:true)
            status.setRollbackOnly()
        }
        assert Book.count() == numBooks+1
    }
}


Have you noticed the line marked with "// 1"? In grails, integration tests are executed in a transactional context by default. This causes some problems while testing the transaction itself. So I disabled the default transactional context.

Now I run this test against all of my configured databases:

MS SQL Server: works
MySQL: works
Oracle Express: works
Riak: fail! No transactionManager bean configured

...ok. I could have used the overview chart from the book to get the same results. But this way I had to figure out how to test transactiosn ins Grails :-)

No comments:

Post a Comment