Skip to content

GRAILS-10580 Rollback of transaction with checked exception #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
project.ext {
springVersion = "3.2.4.RELEASE"
grailsVersion = "2.3.0"
grailsVersion = "2.3.1.BUILD-SNAPSHOT"
slf4jVersion = "1.7.2"
groovyVersion = System.getProperty('groovyVersion') ?: '2.1.5'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package grails.gorm.tests

import groovy.transform.InheritConstructors

/**
* Transaction tests.
*/
Expand Down Expand Up @@ -40,14 +42,13 @@ class WithTransactionSpec extends GormDatastoreSpec {
results.size() == 0
}

void "Test rollback transaction with Exception"() {
void "Test rollback transaction with Runtime Exception"() {
given:
def ex
try {
TestEntity.withNewTransaction { status ->
new TestEntity(name:"Bob", age:50, child:new ChildEntity(name:"Bob Child")).save()
throw new RuntimeException("bad")
new TestEntity(name:"Fred", age:45, child:new ChildEntity(name:"Fred Child")).save()
}
}
catch (e) {
Expand All @@ -64,4 +65,31 @@ class WithTransactionSpec extends GormDatastoreSpec {
ex instanceof RuntimeException
ex.message == 'bad'
}

void "Test rollback transaction with Exception"() {
given:
def ex
try {
TestEntity.withNewTransaction { status ->
new TestEntity(name:"Bob", age:50, child:new ChildEntity(name:"Bob Child")).save()
throw new TestCheckedException("bad")
}
}
catch (e) {
ex = e
}

when:
int count = TestEntity.count()
def results = TestEntity.list()

then:
count == 1
results.size() == 1
ex instanceof TestCheckedException
ex.message == 'bad'
}
}

@InheritConstructors
class TestCheckedException extends Exception {}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import grails.gorm.PagedResultList
import groovy.transform.CompileStatic
import groovy.transform.TypeCheckingMode

import org.codehaus.groovy.grails.orm.support.GrailsTransactionTemplate
import org.codehaus.groovy.runtime.InvokerHelper
import org.grails.datastore.gorm.async.GormAsyncStaticApi
import org.grails.datastore.gorm.finders.DynamicFinder
Expand All @@ -40,8 +41,6 @@ import org.springframework.beans.factory.config.AutowireCapableBeanFactory
import org.springframework.transaction.PlatformTransactionManager
import org.springframework.transaction.TransactionDefinition
import org.springframework.transaction.support.DefaultTransactionDefinition
import org.springframework.transaction.support.TransactionCallback
import org.springframework.transaction.support.TransactionTemplate
import org.springframework.util.Assert
import org.springframework.validation.Errors

Expand Down Expand Up @@ -684,7 +683,7 @@ class GormStaticApi<D> extends AbstractGormApi<D> {
return
}

new TransactionTemplate(transactionManager).execute(callable as TransactionCallback)
new GrailsTransactionTemplate(transactionManager).execute(callable)
}

/**
Expand All @@ -700,9 +699,9 @@ class GormStaticApi<D> extends AbstractGormApi<D> {
return
}

def transactionTemplate = new TransactionTemplate(transactionManager,
def transactionTemplate = new GrailsTransactionTemplate(transactionManager,
new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRES_NEW))
transactionTemplate.execute(callable as TransactionCallback)
transactionTemplate.execute(callable)
}

/**
Expand All @@ -718,7 +717,7 @@ class GormStaticApi<D> extends AbstractGormApi<D> {
return
}

new TransactionTemplate(transactionManager, definition).execute(callable as TransactionCallback)
new GrailsTransactionTemplate(transactionManager, definition).execute(callable)
}

/**
Expand Down