Skip to content

Instantly share code, notes, and snippets.

@jeffsheets
Created August 6, 2013 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffsheets/6168245 to your computer and use it in GitHub Desktop.
Save jeffsheets/6168245 to your computer and use it in GitHub Desktop.
Rollback transactions from multiple Datasource in Grails Integration Tests Prior to Grails 2.3.x, and with Grails 1.3.x on datasources plugin Fixed in 2.3.x
/*
Sample Datasources.groovy file for datasources 0.5 plugin with grails 1.3.x
*/
datasources = {
datasource(name: 'db2') {
domainClasses([com.sheetsj.domain.db2.MyObj, com.sheetsj.domain.db2.MyObj2])
services(['myDb2Stuff'])
driverClassName('com.ibm.db2.jcc.DB2Driver')
dialect(org.hibernate.dialect.DB2Dialect)
url('jdbc:db2://server:port/dbname:currentSchema=schemaName')
username('jdbcUser')
password('jdbcPass')
logSql(true)
hibernate {
cache {
use_second_level_cache(false)
use_query_cache(false)
}
}
}
}
/*
Grails will not rollback transactions on secondary datasources in integration tests
on versions prior to version 2.3.x. (http://jira.grails.org/browse/GRAILS-9771)
But using the solution from the JIRA ticket, you can get this to work on
Grails < 2.3.x, and on Grails 1.3.x with the datasources plugin
*/
class GrailsMultipleDatasourcesWithRollbackIntTests extends GroovyTestCase {
/**
* for a second datasource named 'db2', Grails creates this transactionManager_db2
*/
def transactionManager_db2
def transactionStatus
/** Just some service that uses the db2 datasource */
def myDb2StuffService
void setUp() {
super.setUp()
transactionStatus = transactionManager_db2.getTransaction(new DefaultTransactionDefinition())
}
void tearDown() {
super.tearDown()
transactionManager_db2.rollback(transactionStatus)
}
void testSomeServiceMethod() {
//Do your test and it will now rollback
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment