tags:

views:

26

answers:

2

I have some groovy code like this:

    def dest = destSql.dataSet('destination_table')
    sourceSql.eachRow('select * from source_table'){row ->

        try {
            dest.add(ID: row.id)
        } catch (SQLException) { //A FK constraint will case some inserts to fail 
            dest.add(ID: 1)
        }
    }

I'm running this as a command line script. Everything works fine, but the console outputs the SQLExceptions no matter what. I'd like them to not show up when I handle them, as they only pollute the output. How could I specify this (programmatically, if possible)?

TIA.

A: 

Isn't adding something (possibly multiple times) with ID:1 going to fire a fk constraint exception?

It's probably this exception you are seeing, not the one you're catching.

tim_yates
The code is just an example. And the exception is properly caught, or the program would crash (which it does not, it only outputs the stacktraces to the log, but continues looping through all the records and exits normally).
Andrei
Just found this: http://jira.codehaus.org/browse/GROOVY-3808 Looks like logging was added for Groovy 1.7 and 1.6.6, so I would go for @ataylors solution http://stackoverflow.com/questions/3864970/how-to-suppress-exception-logging-for-groovy-dataset-operations/3868295#3868295
tim_yates
Upgrading to groovy 1.7.4 did change the output of the log (to a more civilized one), but it was still there. The logging level setting is what I was hoping to find. Thanks for the jira link.
Andrei
+2  A: 

You probably just want to turn down the logging level of groovy sql. Try putting this in your program before you try adding to the dataset:

Sql.LOG.level = java.util.logging.Level.SEVERE
ataylor
Perfect, exactly what I was hoping for. Thx.
Andrei