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

前端 未结 3 867
星月不相逢
星月不相逢 2021-02-20 11:18

I am developing springboot-springsession-jdbc-demo. When I simply run the code I get the following error. It looks to me some property must need to set in app

相关标签:
3条回答
  • 2021-02-20 11:30

    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
    
    0 讨论(0)
  • 2021-02-20 11:48

    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.

    0 讨论(0)
  • 2021-02-20 11:55

    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...

    0 讨论(0)
提交回复
热议问题