Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

GRAILS-9959 wrapping dataSourceUnproxied in dataSourceLazy, which is …

…the target for the dataSource bean
  • Loading branch information...
commit 1ef23f39e16662a6158813337d26a691e37e5b52 1 parent 94067c9
@burtbeckwith burtbeckwith authored
View
13 ...c/main/groovy/org/codehaus/groovy/grails/plugins/datasource/DataSourceGrailsPlugin.groovy
@@ -32,6 +32,7 @@ import org.codehaus.groovy.grails.orm.support.TransactionManagerPostProcessor
import org.springframework.beans.factory.BeanIsNotAFactoryException
import org.springframework.context.ApplicationContext
import org.springframework.jdbc.datasource.DriverManagerDataSource
+import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy
import org.springframework.jndi.JndiObjectFactoryBean
import org.springframework.util.ClassUtils
@@ -75,6 +76,7 @@ class DataSourceGrailsPlugin {
boolean isDefault = datasourceName == 'dataSource'
String suffix = isDefault ? '' : datasourceName[10..-1]
String unproxiedName = "dataSourceUnproxied$suffix"
+ String lazyName = "dataSourceLazy$suffix"
if (parentCtx?.containsBean(datasourceName)) {
return
@@ -85,7 +87,8 @@ class DataSourceGrailsPlugin {
jndiName = ds.jndiName
expectedType = DataSource
}
- "$datasourceName"(TransactionAwareDataSourceProxy, ref(unproxiedName))
+ "$lazyName"(LazyConnectionDataSourceProxy, ref(unproxiedName))
+ "$datasourceName"(TransactionAwareDataSourceProxy, ref(lazyName))
return
}
@@ -96,7 +99,10 @@ class DataSourceGrailsPlugin {
final String hsqldbDriver = "org.hsqldb.jdbcDriver"
if (hsqldbDriver.equals(driver) && !ClassUtils.isPresent(hsqldbDriver, getClass().classLoader)) {
- throw new GrailsConfigurationException("Database driver [$hsqldbDriver] for HSQLDB not found. Since Grails 2.0 H2 is now the default database. You need to either add the 'org.h2.Driver' class as your database driver and change the connect URL format (for example 'jdbc:h2:mem:devDb') in DataSource.groovy or add HSQLDB as a dependency of your application.")
+ throw new GrailsConfigurationException("Database driver [" + hsqldbDriver +
+ "] for HSQLDB not found. Since Grails 2.0 H2 is now the default database. You need to either " +
+ "add the 'org.h2.Driver' class as your database driver and change the connect URL format " +
+ "(for example 'jdbc:h2:mem:devDb') in DataSource.groovy or add HSQLDB as a dependency of your application.")
}
boolean defaultDriver = (driver == "org.h2.Driver")
@@ -157,7 +163,8 @@ class DataSourceGrailsPlugin {
bean.destroyMethod = "close"
}
- "$datasourceName"(TransactionAwareDataSourceProxy, ref(unproxiedName))
+ "$lazyName"(LazyConnectionDataSourceProxy, ref(unproxiedName))
+ "$datasourceName"(TransactionAwareDataSourceProxy, ref(lazyName))
}
String resolvePassword(ds, application) {
View
62 ...t/groovy/org/codehaus/groovy/grails/plugins/datasource/DataSourceGrailsPluginTests.groovy
@@ -2,6 +2,7 @@ package org.codehaus.groovy.grails.plugins.datasource
import grails.spring.BeanBuilder
import grails.util.Holders
+import groovy.sql.Sql
import javax.sql.DataSource
@@ -9,9 +10,9 @@ import org.apache.tomcat.jdbc.pool.DataSource as TomcatDataSource
import org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator
import org.codehaus.groovy.grails.commons.test.AbstractGrailsMockTests
import org.springframework.jdbc.datasource.DriverManagerDataSource
+import org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy
import org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy
import org.springframework.jndi.JndiObjectFactoryBean
-import org.codehaus.groovy.grails.plugins.PluginManagerHolder
class DataSourceGrailsPluginTests extends AbstractGrailsMockTests {
@@ -299,6 +300,9 @@ class DataSourceGrailsPluginTests extends AbstractGrailsMockTests {
def beanDef = bb.getBeanDefinition('dataSource')
assertEquals TransactionAwareDataSourceProxy.name, beanDef.beanClassName
+ beanDef = bb.getBeanDefinition('dataSourceLazy')
+ assertEquals LazyConnectionDataSourceProxy.name, beanDef.beanClassName
+
beanDef = bb.getBeanDefinition('dataSourceUnproxied')
assertEquals TomcatDataSource.name, beanDef.beanClassName
@@ -315,6 +319,9 @@ class DataSourceGrailsPluginTests extends AbstractGrailsMockTests {
beanDef = bb.getBeanDefinition('dataSource_ds2')
assertEquals TransactionAwareDataSourceProxy.name, beanDef.beanClassName
+ beanDef = bb.getBeanDefinition('dataSourceLazy_ds2')
+ assertEquals LazyConnectionDataSourceProxy.name, beanDef.beanClassName
+
beanDef = bb.getBeanDefinition('dataSourceUnproxied_ds2')
assertEquals 'pooled false, readOnly true', ReadOnlyDriverManagerDataSource.name, beanDef.beanClassName
@@ -331,6 +338,9 @@ class DataSourceGrailsPluginTests extends AbstractGrailsMockTests {
beanDef = bb.getBeanDefinition('dataSource_ds3')
assertEquals TransactionAwareDataSourceProxy.name, beanDef.beanClassName
+ beanDef = bb.getBeanDefinition('dataSourceLazy_ds3')
+ assertEquals LazyConnectionDataSourceProxy.name, beanDef.beanClassName
+
beanDef = bb.getBeanDefinition('dataSourceUnproxied_ds3')
assertEquals 'pooled default true, readOnly true', TomcatDataSource.name, beanDef.beanClassName
@@ -347,6 +357,9 @@ class DataSourceGrailsPluginTests extends AbstractGrailsMockTests {
beanDef = bb.getBeanDefinition('dataSource_ds4')
assertEquals TransactionAwareDataSourceProxy.name, beanDef.beanClassName
+ beanDef = bb.getBeanDefinition('dataSourceLazy_ds4')
+ assertEquals LazyConnectionDataSourceProxy.name, beanDef.beanClassName
+
beanDef = bb.getBeanDefinition('dataSourceUnproxied_ds4')
assertEquals TomcatDataSource.name, beanDef.beanClassName
@@ -370,6 +383,53 @@ class DataSourceGrailsPluginTests extends AbstractGrailsMockTests {
assertEquals DataSource, beanDef.propertyValues.getPropertyValue('expectedType').value
}
+ void testProxying() {
+
+ def config = new ConfigSlurper().parse('''
+ dataSource {
+ pooled = true
+ driverClassName = "org.h2.Driver"
+ url = "jdbc:h2:mem:testDb1"
+ username = "sa"
+ password = ""
+ }
+ dataSource_ds2 {
+ pooled = true
+ driverClassName = "org.h2.Driver"
+ url = "jdbc:h2:tcp://localhost:1234/~/test"
+ username = "sa"
+ password = ""
+ }
+ ''')
+
+ def ctx = createBeanBuilder(config).createApplicationContext()
+
+ def dataSource = ctx.dataSource
+ def dataSourceLazy = ctx.dataSourceLazy
+ def dataSourceUnproxied = ctx.dataSourceUnproxied
+ assert dataSource.targetDataSource.is(dataSourceLazy)
+ assert dataSourceLazy.targetDataSource.is(dataSourceUnproxied)
+
+ dataSource.getConnection().close()
+ Sql sql = new Sql(dataSource)
+ sql.execute('create table thing (foo int)')
+
+ def dataSource_ds2 = ctx.dataSource_ds2
+ def dataSourceLazy_ds2 = ctx.dataSourceLazy_ds2
+ def dataSourceUnproxied_ds2 = ctx.dataSourceUnproxied_ds2
+ assert dataSource_ds2.targetDataSource.is(dataSourceLazy_ds2)
+ assert dataSourceLazy_ds2.targetDataSource.is(dataSourceUnproxied_ds2)
+
+ // succeeds because no work is done
+ dataSource_ds2.getConnection().close()
+
+ String message = shouldFail() {
+ sql = new Sql(dataSource_ds2)
+ sql.execute('create table thing (foo int)')
+ }
+ assert message.contains('Connection is broken')
+ }
+
// doesn't actually test MVCC, mostly just that it's a valid URL
void testMvccUrlOption() {
def config = new ConfigSlurper().parse '''
Please sign in to comment.
Something went wrong with that request. Please try again.