0

I have connection issue between my spring boot/kotlin app and database. I tried following guides what google has to offer and some Stackoverflow posts. I added client/admin sql permisions to users. After following google guide https://github.com/spring-cloud/spring-cloud-gcp/tree/master/spring-cloud-gcp-samples/spring-cloud-gcp-sql-postgres-sample on this link and recreating everything as it is. I came to problem: deployed App does not connect to actual database, instead it uses data.sql and schema.sql objects from resources, also it does not do changes to actual database, but somewhere else, for example I am using this command to getdata:

 @GetMapping("/getdata")
    fun getTuples(): List<String>? {
        return jdbcTemplate!!.queryForList("SELECT * FROM users").stream()
                .map { m: Map<String?, Any?> -> m.values.toString() }
                .collect(Collectors.toList())
    }

result I get is the one provided in example:

0   "[luisao@example.com, Anderson, Silva]"
1   "[jonas@example.com, Jonas, Goncalves]"
2   "[fejsa@example.com, Ljubomir, Fejsa]"

the actual users table in Cloud database looks like this:

postgres=> select * from users;
       email        | first_name | last_name
--------------------+------------+-----------
 luisao@example.com | Anderson   | Silva
 jonas@example.com  | Jonas      | Goncalves
 fejsa@example.com  | Ljubomir   | Fejsa
 kkskldk            | sjndjfdf   | skdskd
(4 rows)

first 3 rows are same just because I added them manually. App does not see DDL/DML changes in database, only those I do in data.sql or schema.sql. So the question is where could be a problem? in this documentation https://cloud.spring.io/spring-cloud-gcp/multi/multi__spring_jdbc.html says that this dependency should do most of the stuff automaticly but somehow something goes wrong. Could it be that spring boot/hikari creates virtual enviroment in appengine which blocks database connection and you connect and see only Mock data from data.sql files? Or problem lies in configuration? My application.properties file:

server.error.include-message=always
spring.cloud.gcp.sql.database-name=[database name]
spring.cloud.gcp.sql.instance-connection-name=[Instance name]
spring.datasource.continue-on-error=true
spring.datasource.initialization-mode=always
spring.datasource.username=postgres
spring.datasource.password=[password]
spring.cloud.gcp.project-id=[project id]
Gertas
  • 1
  • 1
  • Did you have commit the data in postgre? – guillaume blaquiere Nov 13 '20 at 11:56
  • Yes I did commit within database, or can you activate commit with appengine too? – Gertas Nov 13 '20 at 14:04
  • It was sure to have the same data. On App Engine, it depends on your configuration and your code (especially annotation with Spring). Do you have the same behavior locally with the same database? – guillaume blaquiere Nov 13 '20 at 14:23
  • @guillaumeblaquiere well I did code excatly like in example. so it is not different, Should I share something else, that could make situation clear? launching app from intelej, and from appengine provides the same output. – Gertas Nov 13 '20 at 16:30
  • Great, if it's the same output locally and on AppEngine, it's a good status: the issue doesn't comme from App Engine but from the code. – guillaume blaquiere Nov 13 '20 at 19:25
  • Try to remove the *.sql file from the resources. and try again – guillaume blaquiere Nov 13 '20 at 21:18
  • @guillaumeblaquiere so I removen .sql files from resources. locally intelej does not recognise tables, also I redeployed app. so locally intelej does not recognise tables, but when call appengine old data from .sql files are still there. Somehow it does not show new data or new tables in real database. could it be that spring boot created some kind of virtual enviroment in appengine and it blocks access to database? – Gertas Nov 14 '20 at 09:03
  • I tested and... No issue, even with the SQL file and the init param to `always` (I though it was the issue, but not). No issue locally (IntelliJ also, start with `mvn spring-boot:run` command) and on AppEngine. Did you perform the query by hand directly in the postgres instance? – guillaume blaquiere Nov 14 '20 at 22:07
  • Yes i did perform query locally, I wanted to see if it data is saved into database. Maybe I lack some understanding how does it work behind. Does a query for you show diffrently? – Gertas Nov 15 '20 at 13:32
  • Honestly, it worked out of the box for me, no change, simply set my Cloud SQL postgres config and that's all! My question might be stupid, I simply try to understand what you did differently! – guillaume blaquiere Nov 15 '20 at 14:12
  • bad SQL grammar [SELECT * FROM lohn;]; nested exception is org.postgresql.util.PSQLException: ERROR: relation "lohn" does not exist Position: 15 with root cause – Gertas Nov 15 '20 at 14:28
  • Yeah I am tryong to understand it too. I created new Instance, and manually added table in postgress using gshell. Might be problems because I followed this tutorial before starting https://stackoverflow.com/questions/51328359/how-to-connect-to-cloud-sql-mysql-database-from-app-engine – Gertas Nov 15 '20 at 14:30
  • @guillaumeblaquiere So today I did test diffrent query. SELECT * FROM pg_catalog.pg_tables WHERE schemaname != 'pg_catalog' AND schemaname != 'information_schema'. It doesnt show public tables created by user. So connection exists but somehow not fully. What could be a couse of that? – Gertas Nov 16 '20 at 09:08
  • It's crazy!!! Do you use the same user? – guillaume blaquiere Nov 16 '20 at 10:27
  • So I solved the thing. apparently I was logged in false database. using \conninfo command I was able to verify the connection. apparently -d database command does not work in gshell anymore. that is why I created tables on default postgress database and not on the working one. @guillaumeblaquiere thanks for help. Appreciated. need to set this on solved. – Gertas Nov 17 '20 at 08:17
  • Hopefully, you fix it!! This made me mad!! It wasn't a user issue, but a database issue!! Easy to solve finally!! Enjoy your journey on Google Cloud!! – guillaume blaquiere Nov 17 '20 at 08:20
  • Hi @Gertas, good work finding the solution! Could you post an answer and [accept it](https://stackoverflow.blog/2009/01/06/accept-your-own-answers/)? It will make it more visible and help someone with the same issue as you find the solution. Thanks! – Jose V Nov 18 '20 at 13:46

0 Answers0