question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Spring JDBC DataSource Initializer fails for schema with stored procedure definitions

See original GitHub issue

Spring boot - 1.3.5; 1.4.0.M3 The Spring boot application crashes when schema.sql in the root of the classpath contains definitions of stored procedures.

The schema.sql.txt is created using the standard PostgreSQL dump tool. A similar crash happens when using Oracle RDBMS schema dump files with stored procedure definitions.

What is expected? The application loads the schema during the boot process.

What is actually happening? The application crashes with the following stack trace:

2016-06-24 10:23:16.468 ERROR 8404 --- [           main] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$JdbcTemplateConfiguration': Unsatisfied dependency expressed through constructor parameter 0: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceInitializer': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #11 of URL [file:/Users/pivotal/workspace/db_initialize_fail/build/resources/main/schema.sql]: CREATE FUNCTION count_clients() RETURNS integer LANGUAGE plpgsql AS $$begin select count(*) from clients; nested exception is org.postgresql.util.PSQLException: ERROR: unterminated dollar-quoted string at or near "$$begin select count(*) from clients"
  Position: 69; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceInitializer': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #11 of URL [file:/Users/pivotal/workspace/db_initialize_fail/build/resources/main/schema.sql]: CREATE FUNCTION count_clients() RETURNS integer LANGUAGE plpgsql AS $$begin select count(*) from clients; nested exception is org.postgresql.util.PSQLException: ERROR: unterminated dollar-quoted string at or near "$$begin select count(*) from clients"
  Position: 69
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:189) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1143) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1046) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:775) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861) ~[spring-context-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541) ~[spring-context-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:765) [spring-boot-1.4.0.M3.jar:1.4.0.M3]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.4.0.M3.jar:1.4.0.M3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.4.0.M3.jar:1.4.0.M3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1182) [spring-boot-1.4.0.M3.jar:1.4.0.M3]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1171) [spring-boot-1.4.0.M3.jar:1.4.0.M3]
    at io.pivotal.db_initialize_fail.Application.main(Application.java:9) [main/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_60]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_60]
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) [idea_rt.jar:na]
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceInitializer': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #11 of URL [file:/Users/pivotal/workspace/db_initialize_fail/build/resources/main/schema.sql]: CREATE FUNCTION count_clients() RETURNS integer LANGUAGE plpgsql AS $$begin select count(*) from clients; nested exception is org.postgresql.util.PSQLException: ERROR: unterminated dollar-quoted string at or near "$$begin select count(*) from clients"
  Position: 69
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:553) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:187) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1208) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1048) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1018) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:834) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    ... 23 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSourceInitializer': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #11 of URL [file:/Users/pivotal/workspace/db_initialize_fail/build/resources/main/schema.sql]: CREATE FUNCTION count_clients() RETURNS integer LANGUAGE plpgsql AS $$begin select count(*) from clients; nested exception is org.postgresql.util.PSQLException: ERROR: unterminated dollar-quoted string at or near "$$begin select count(*) from clients"
  Position: 69
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:136) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:408) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:220) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:352) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:333) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerPostProcessor.postProcessAfterInitialization(DataSourceInitializerPostProcessor.java:62) ~[spring-boot-autoconfigure-1.4.0.M3.jar:1.4.0.M3]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    ... 34 common frames omitted
Caused by: org.springframework.jdbc.datasource.init.ScriptStatementFailedException: Failed to execute SQL script statement #11 of URL [file:/Users/pivotal/workspace/db_initialize_fail/build/resources/main/schema.sql]: CREATE FUNCTION count_clients() RETURNS integer LANGUAGE plpgsql AS $$begin select count(*) from clients; nested exception is org.postgresql.util.PSQLException: ERROR: unterminated dollar-quoted string at or near "$$begin select count(*) from clients"
  Position: 69
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:494) ~[spring-jdbc-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:231) ~[spring-jdbc-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:48) ~[spring-jdbc-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runScripts(DataSourceInitializer.java:169) ~[spring-boot-autoconfigure-1.4.0.M3.jar:1.4.0.M3]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runSchemaScripts(DataSourceInitializer.java:84) ~[spring-boot-autoconfigure-1.4.0.M3.jar:1.4.0.M3]
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.init(DataSourceInitializer.java:76) ~[spring-boot-autoconfigure-1.4.0.M3.jar:1.4.0.M3]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_60]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_60]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_60]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_60]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:365) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:310) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133) ~[spring-beans-4.3.0.RC2.jar:4.3.0.RC2]
    ... 48 common frames omitted
Caused by: org.postgresql.util.PSQLException: ERROR: unterminated dollar-quoted string at or near "$$begin select count(*) from clients"
  Position: 69
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2284) ~[postgresql-9.4.1208.jre7.jar:9.4.1208.jre7]
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2003) ~[postgresql-9.4.1208.jre7.jar:9.4.1208.jre7]
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:200) ~[postgresql-9.4.1208.jre7.jar:9.4.1208.jre7]
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:424) ~[postgresql-9.4.1208.jre7.jar:9.4.1208.jre7]
    at org.postgresql.jdbc.PgStatement.executeWithFlags(PgStatement.java:321) ~[postgresql-9.4.1208.jre7.jar:9.4.1208.jre7]
    at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:313) ~[postgresql-9.4.1208.jre7.jar:9.4.1208.jre7]
    at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:473) ~[spring-jdbc-4.3.0.RC2.jar:4.3.0.RC2]
    ... 60 common frames omitted

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sabramscommented, May 2, 2017

Thanks for info, all. I followed @uweschaefer 's advice with the different separator: left the semi colon in the define procedure and replaced all other semi colon separators with / and was able to continue after encountering this with an Oracle script. ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator(new ClassPathResource("schema.sql")); databasePopulator.setSeparator("/");

1reaction
philwebbcommented, Apr 11, 2017

This should ideally be fixed in SqlUtils. I’ve raise SPR-15438.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring JDBC DataSource Initializer fails for schema with ...
The Spring boot application crashes when schema.sql in the root of the classpath contains definitions of stored procedures. The schema.sql.txt ...
Read more >
Spring Boot MySQL Database Initialization Error with Stored ...
The issue is that the ; character within the stored procedure is being parsed out by the Spring ScriptUtils class that parses the...
Read more >
Spring Boot MySQL Database Initialization Error with Stored ...
The answer turned out to be very simple. Spring Boot has a DataSource separator property that can be set in the application.properties file:...
Read more >
13. Data access with JDBC - Spring
Calling a stored procedure or function that returns a result set is a bit tricky. Some databases return result sets during the JDBC...
Read more >
Spring JDBC Tutorial - Baeldung
Explore the capabilities offered by Spring to perform JDBC Authentication using an existing DataSource configuration.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found