LauJensen / clojureql

ClojureQL is superior SQL integration for Clojure
https://clojureql.sabrecms.com
Eclipse Public License 1.0
285 stars 39 forks source link

Add support for database transaction #58

Closed tendant closed 13 years ago

tendant commented 13 years ago

Support database transaction in cql or make cql work with clojure.contrib.sql/transaction.

For example:



create table users ( id int auto_increment, 
                                 name varchar(20), 
                                 city varchar(20) not null, 
                                 primary key (id));

(def db
 {:classname   "com.mysql.jdbc.Driver"
  :subprotocol "mysql"
  :user        "cql"
  :password    "cql"
  :subname     "//localhost:3306/cql"
  :auto-commit false})

(clojure.contrib.sql/with-connection
    db
    (clojure.contrib.sql/transaction
     @(conj! (table :users) {:name "test1" :city "city"})
     @(conj! (table :users) {:name "test2"})))  ; it will throw exception for missing :city column. 

Current behavior:

First conj! will insert a record into database, second conj! will throw an exception.

Designed behavior:

Rollback all db operation wrapped in clojure.contrib.sql/transaction. None of records can be inserted into database table.

tendant commented 13 years ago

Database transaction does work as expected in clojureql. But for mysql, only InnoDB storage engine supports transaction.

Update above table creation statement for mysql as follow. Now it works well.

create table users ( id int auto_increment, name varchar(20), city varchar(20) not null, primary key (id)) engine=InnoDB;