Transaction Example

323 views
Skip to first unread message

Jonathan M.

unread,
Mar 26, 2013, 12:55:12 PM3/26/13
to sqlk...@googlegroups.com
Hello,

I am using korma with mysql and InnoDB and I am having trouble understanding the example shown here.
Are the two pieces of code different examples or are they used together? 

Can someone post a simple example of doing two inserts(or updates), checking if they succeeded and rolling back if not?

Thank you for your time

Dennis Roberts

unread,
Mar 26, 2013, 1:15:24 PM3/26/13
to sqlk...@googlegroups.com
Hi Jonathan,

The transaction macro automatically rolls back if it catches any Throwable, so you don't have to explicitly roll back if there's a problem as long as you don't handle any exceptions that should cause a rollback from within the transaction macro (or you throw another exception after handling an exception that should cause a rollback).

If you need to force a rollback, then you can call (rollback).

Here are some examples of transactions that will be rolled back:

(transaction
  (insert :foo (values {:level 1}))
  (insert :bar (values {:level 42}))
  (throw (Exception.)))

(transaction
  (insert :foo (values {:level 1}))
  (insert :bar (values {:level 42}))
  (rollback))

Let me know if you have any questions.

Thanks,
Dennis

--
You received this message because you are subscribed to the Google Groups "Korma" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlkorma+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jonathan M.

unread,
Mar 26, 2013, 1:40:37 PM3/26/13
to sqlk...@googlegroups.com
Hi Dennis,

Thanks for your reply. Much better now.

I do have one more question. What if I want to run some code before or after the rollback has been executed? Can you please provide a small example for this case as well? I assume that the code needs to be wrapped in try-catch block or is there another way with korma?

Jonathan

Dennis Roberts

unread,
Mar 26, 2013, 1:56:28 PM3/26/13
to sqlk...@googlegroups.com
Hi Jonathan,

If you want to perform some action after the rollback has completed, you can do something like this:

(transaction
  (insert :foo (values {:level 1}))
  (insert :bar (values {:level 42}))
  (rollback))
(when (is-rollback?)
  (do-something))

I don't currently know of a way to do something before the rollback is performed other than to both catch exceptions and check is-rollback? within the transaction:

(tranaction
  (try
    (insert :foo (values {:level 1}))
    (insert :bar (values {:level 42}))
    (catch Throwable t
      (rollback)))
    (if (is-rollback?)
      (do-something))))

Disclaimer: I haven't tried this yet.

Dennis

Jonathan M.

unread,
Mar 26, 2013, 2:00:30 PM3/26/13
to sqlk...@googlegroups.com
Thanks Dennis.

The "after rollback" case which I've asked is probably pointless but still good to know about the try-catch :)

Jonathan

Jonathan M.

unread,
May 28, 2013, 11:34:23 AM5/28/13
to sqlk...@googlegroups.com
Sorry for bringing this back up but, this example:

(transaction
  (insert :foo (values {:level 1}))
  (insert :bar (values {:level 42}))
  (rollback))
(when (is-rollback?)
  (do-something))

throws:

java.lang.NullPointerException: null
 at clojure.core$deref.invoke (core.clj:2080)
    clojure.java.jdbc$rollback.invoke (jdbc.clj:340)
    clojure.java.jdbc$is_rollback_only.invoke (jdbc.clj:405)
    korma.db$is_rollback_QMARK_.invoke (db.clj:186) 

Are you sure the example is correct?

Jonathan 

Jonathan M.

unread,
May 28, 2013, 12:52:28 PM5/28/13
to sqlk...@googlegroups.com
I think the correct is:

(transaction
  (insert :foo (values {:level 1}))
  (insert :bar (values {:level 42}))
  (rollback)
  (when (is-rollback?)
    (do-something)))

Jonathan
Reply all
Reply to author
Forward
0 new messages