Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 827527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:35:19+00:00 2026-05-15T03:35:19+00:00

I have been updating all my services to be transactional by using Grail’s ability

  • 0

I have been updating all my services to be transactional by using Grail’s ability to rollback when a RuntimeException is thrown in the service. I have, in most cases, doing this:

def domain = new Domain(field: field)
if (!domain.save()) {
   throw new RuntimeException()
}

Anyways, I wanted to verify that this indeed will rollback the transaction… it got me thinking as to whether at this point it’s already been committed.. Also, if not, would setting flush:true change that? I am not very familiar with how Spring/Hibernate does all of this 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T03:35:19+00:00Added an answer on May 15, 2026 at 3:35 am

    Yep that’ll do it.

    Transactions in Grails are by default handled at a Service method level. If the method returns normally then the transaction will be committed, if a RuntimeException is thrown the transaction will be rolled back.

    Note this means even if you use flush:true on while saving an object in the server method the db changes will still be rolled back if you throw a RuntimeException.

    For example:

    class MyService {
    
     def fiddle(id,id2){
       def domain = Domain.findById(id)
    
       domain.stuff = "A change"
       domain.save( flush:true ) // will cause hibernate to perform the update statements
    
       def otherDomain = OtherDomain.findById(id2)      
    
       otherDomain.name = "Fiddled"
    
       if( !otherDomain.save( flush:true )){ // will also write to the db
         // the transaction will be roled back 
         throw new RuntimeException("Panic what the hell happened")
       }                                                           
     }
    }
    

    What I’m not 100% clear on with Grails is what happens if a checked exception is thrown in straight java/spring world the default behaviour is for the transaction inceptor to commit the transaction, althrough this can be overriden in the config.

    Note: there is a caveat, and that is that your db has to support transactions on the tables you are updating. Yes, this is poke at MySQL 🙂

    This also applies to the Domain.withTransaction method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to use one View for updating an object and all
We have been using CruiseControl for quite a while with NUnit and NAnt. For
I have been working on a web services related project for about the last
I have been working with Visual Studio (WinForm and ASP.NET applications using mostly C#)
I have been using PHP and JavaScript for building my dad's website. He wants
I have been consuming a service for some time in development, and have been
I have a database in which all of the tables have been generated by
Using MVC 2 I have been trying to make this record store project. Creating
This question is long winded because I have been updating the question over a
The project I have been managing has ran into an issue of updating contents

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.