{"id":3338,"date":"2011-03-14T19:52:11","date_gmt":"2011-03-14T14:22:11","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=3338"},"modified":"2011-03-14T19:52:11","modified_gmt":"2011-03-14T14:22:11","slug":"using-transactional-behaviour-with-gorm","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/using-transactional-behaviour-with-gorm\/","title":{"rendered":"Using transactional behaviour with Gorm"},"content":{"rendered":"<p>All communication between Hibernate and the database runs within the context of a database transaction but the Session itself is lazy in that it only ever initiates a database transaction at the last possible moment.Given that there is a transaction, you would think that if something went wrong, any problems would be rolled back. However, without specific transaction boundaries and if the Session is flushed, any changes are permanently committed to the database.<br \/>\nThis is a particular problem if the flush is beyond your control for instance, the result of a query. Then those changes will be permanently persisted to the database. This is a particular problem if the flush is beyond your control for instance, the result of a query. Then those changes will be permanently persisted to the database which may create problems.<\/p>\n<p>For example<br \/>\n[java]<br \/>\ndef save = {<br \/>\ndef album = Album.get(params.id)<br \/>\nalbum.title = &quot;Changed Title&quot;<br \/>\nalbum.save(flush:true)<br \/>\n&#8230;<br \/>\n\/\/ something goes wrong<br \/>\nthrow new Exception(&quot;Oh, sugar.&quot;)<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Now when some exception occurs, the database has already been updated, you cannot roll back at this stage.This problem can be solved by either placing code in service or adding transactional behaviour to GORM<br \/>\n[java]<br \/>\ndef save = {<br \/>\nAlbum.withTransaction {<br \/>\ndef album = Album.get(params.id)<br \/>\nalbum.title = &quot;Changed Title&quot;<br \/>\nalbum.save(flush:true)<br \/>\n&#8230;<br \/>\n\/\/ something goes wrong<br \/>\nthrow new Exception(&quot;Oh, sugar.&quot;)<br \/>\n}<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>If an exception is thrown, all changes made within the scope of the transaction will be rolled back as expected.<\/p>\n<p>Grails uses Spring\u2019s PlatformTransactionManager abstraction layer under the covers. In this case, if an exception is thrown, all changes made within the scope of the transaction will be rolled back as expected. The first argument to the withTransaction method is a Spring TransactionStatus object, which also allows you to programmatically roll back the transaction by calling the setRollbackOnly() method.<\/p>\n<p>[java]<\/p>\n<p>def save = {<br \/>\nAlbum.withTransaction { status -&gt;<br \/>\ndef album = Album.get(params.id)<br \/>\nalbum.title = &quot;Changed Title&quot;<br \/>\nalbum.save(flush:true)<br \/>\n&#8230;<br \/>\n\/\/ something goes wrong<br \/>\nif(hasSomethingGoneWrong()) {<br \/>\nstatus.setRollbackOnly()<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>[\/java]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>All communication between Hibernate and the database runs within the context of a database transaction but the Session itself is lazy in that it only ever initiates a database transaction at the last possible moment.Given that there is a transaction, you would think that if something went wrong, any problems would be rolled back. However, [&hellip;]<\/p>\n","protected":false},"author":57,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[7],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3338"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/57"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=3338"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/3338\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=3338"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=3338"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=3338"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}