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

  • Home
  • SEARCH
  • 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 7440275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T10:49:35+00:00 2026-05-29T10:49:35+00:00

My users are uploading a csv or xls or whatever and each line is

  • 0

My users are uploading a csv or xls or whatever and each line is going to be an instance of a domain object I save. If any of the lines fail I want the whole thing rolled back, but I also want to return errors for any lines that will fail later. Let’s make an example:

Domain class:

MyDomainClass{
  String fieldOne
  BigDecimal fieldTwo
}

Input:

ThisLineWorks,4.4
ThisLineFails,BecauseOfThis
How would I also get an error, for this line as well considering the last one would have rolled back the transaction already?

Fantasy Output:

OK|ThisLineWorks,4.4
field 2 isn't a number|ThisLineFails,BecauseOfThis
field 2 isn't a number|How would I also get an error, for this line as well considering the last one would have rolled back the transaction already?
  • 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-29T10:49:36+00:00Added an answer on May 29, 2026 at 10:49 am

    You can validate the objects without having to save them: ( http://grails.org/doc/2.0.x/guide/validation.html#validatingConstraints). So in a service you can create all of the objects, then validate all of the objects, then save all of the objects. Something similar to:

    def serviceMethod(data) {
        def listOfObjects = createObjectsFromData(data)
        listOfObjects*.validate()
        def anErrorOccurred = listOfObjects.find {it.hasErrors()} != null
        if(anErrorOccurred) {
            return listOfObjects
        }
        listOfObjects*.save(validate: false) //you could use the validate:false or leave it out.  I figure since we've already validated that you could do without re-validating.
    }
    

    This way you can collect all of your errors and not have to worry about rolling back the transaction. Problem with this setup is you’ll be creating N number of objects and holding onto all of them. If your file is longer than 100k rows (a slightly educated guess on where you’ll start to suffer) then this might cause some performance issues. If you don’t like the above method you could handle the transaction manually:
    ( http://grails.org/doc/2.0.x/ref/Domain%20Classes/withTransaction.html)

    def serviceMethod(data) {
        MyDomainClass.withTransaction { status ->
            def listOfObjects = []
            data.each {
                def domainObject = createObjectFromData(it)
                lisOfObjects << domainObject.save()
            }
            def anErrorOccurred = lisOfObjects.find {it.hasErrors()} != null
            if(anErrorOccurred) {
               status.setRollbackOnly()  //will roll back all of the transactions surrounded by the .withTransaction {}
            }
        }
    }
    

    You’re still holding onto all of the objects here (since you want to retrieve ALL errors that occur). One way I can think of to avoid holding onto all of the objects would be to create the objects one at a time and validate them one by one adding errors to a list when applicable, but then you’d have to recreate all of the objects when they all pass validation which doesn’t seem very efficient either.

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

Sidebar

Related Questions

I want to allow users to put in am img url instead of uploading
My user is uploading an image of any size XX x YY. I want
My project supports users uploading their own icons to be used for various entities
We have a few 100 users logging in via FTP and uploading files. Our
I need to let users upload videos in GWT site, and after uploading I
I have some data to export from DB and save as CSV file, and
Users are uploading fotos to our php build system. Some of them we are
I want to be able to prevent users from uploading low quality images to
I have been having lot of problems with users uploading images on my website.
I'm using PHP and Graphicsmagick and I have a bunch of users uploading batches

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.