Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.spring_session' doesn't exist - Spring Boot

心不动则不痛 提交于 2019-12-05 01:30:50

I've just had a quite similar error while using spring-boot 2.0.5 (as opposed to 1.4.0 used by OP) with Postgres driver:

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [DELETE FROM spring_sessions WHERE EXPIRY_TIME < ?]; nested exception is org.postgresql.u
til.PSQLException: ERROR: relation "spring_sessions" does not exist
  Position: 13
        at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:234) ~[spring-jdbc-5.0.8.RELEASE.jar!/:5.0.8.RELEA
SE]
        // redacted...
        at java.lang.Thread.run(Thread.java:748) [na:1.8.0_181]
Caused by: org.postgresql.util.PSQLException: ERROR: relation "spring_sessions" does not exist
  Position: 13
        at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2433) ~[postgresql-42.2.2.jar!/:42.2.2]
        at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2178) ~[postgresql-42.2.2.jar!/:42.2.2]
        // redacted...
        at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:605) ~[spring-jdbc-5.0.8.RELEASE.jar!/:5.0.8.RELEASE]
        ... 16 common frames omitted

According to documentation, setting up Spring Session backed by a relational database is as simple as adding a single configuration property to your application.properties:

spring.session.store-type=jdbc

Note that in order to get the session tables auto-created, I had to also specify:

spring.session.jdbc.initialize-schema=always

Once this setting specified, Spring used the correct SQL initialization script from spring-session-jdbc jar. My mistake was not specifying that option - in which case embedded was being used as default value.

I'm not sure this is the best solution (or even the solution as i'm new to spring and am not aware of the repercussions of doing this) but I was having the same issue, managed to solve it by deleting the annotation @EnableJdbcHttpSession

According to Vedran Pavic:

The reason spring.session.* do not work for you is because your are using @EnableJdbcHttpSession. That means that you are configuring Spring Session explicitly, so Spring Boot backs off with its auto-configuration.

You should remove @EnableJdbcHttpSession and ensure Spring Boot is auto-configuring Spring Session. Additionally, you could also leave spring.session.store-type out, since Spring Boot should be able to deduce which session repository implementation are you using as long as you have only only SessionRepository implementation on the classpath (i.e. you depend only on spring-session-jdbc and no other Spring Session modules).

Original Post

This will solve your immediate problem, but i'm unsure if it will break something else...

I think you have multiple issues By default jdbc has H2 database. It is automatically created by spring. Check if it is there. So first run it on H2. Then copy the same database and table to MySQL. create same schema and table to MySQL then change connection to MySQL.

Spring Boot automatically creates a DataSource that connects Spring Session to an embedded instance of H2 database src/main/resources/application.properties

spring.datasource.url= # JDBC URL of the database.
spring.datasource.username= # Login username of the database.
spring.datasource.password= # Login password of the database.

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!