spring-projects / spring-session

Spring Session
https://spring.io/projects/spring-session
Apache License 2.0
1.86k stars 1.11k forks source link

jdbc `initialize-schema` creates table with default name, not the one from `table-name` property #1230

Closed jm-ck closed 5 years ago

jm-ck commented 5 years ago

Spring Session JDBC when requested to always initialize schema, creates tables named with default name (SPRING_SESSION) even when table-name param is overriding this name. Later at runtime, tables defined in table-name are missing, because not created by Spring Session.

Full config of SpringSession:

spring.session:
    store-type: jdbc
    jdbc:
      table-name: SPRING_SESSION_V2
      initialize-schema: always

version: 2.1.0.RELEASE

rwinch commented 5 years ago

Thanks for the report. It will help this issue get prioritized significantly if you an put together a minimal but complete sample

vpavic commented 5 years ago

This is actually more of a Spring Boot question, as it is actually Spring Boot that's in charge of initializing the database schema, rather than Spring Session.

That being said, Spring Boot will initialize schema using DDL scripts that are shipped within the spring-session-jdbc jar - and those are using SPRING_SESSION table name.

If you're configuring a custom table name for session storage using spring.session.jdbc.table-name property, and still have Spring Boot initialize the schema, you need to supply the path to custom DDL script using spring.session.jdbc.schema property.

jm-ck commented 5 years ago

here is the working sample with H2 and spring session setting table-name to some custom value: https://github.com/yacekmm/SpringSessionBugReportSample

After app startup the exception is thrown that table with custom name can't be found since the one with default name was created.

rwinch commented 5 years ago

Thanks for the sample. Did you see @vpavic comment?

jm-ck commented 5 years ago

Yes I did. I do not use Spring Boot to initialize the schema. I do it with a migration tool (liquibase) outside of the app - exactly like in the sample. Spring Boot only verifies the schema with

spring.jpa.hibernate.ddl-auto: validate

In previous Spring Session version < 2 with Spring Boot 1.5 the schema was initialized by a Spring Session. Also the tables are initialized by Spring Session 2.1.0 if default table names are used. So why the behavior is different when I change the table name? Why should I provide custom SQL schema file if I want a different name?

It sounds strange to me, this is why i find it as a bug

vpavic commented 5 years ago

I think there is some misunderstanding here. In the scope of this ticket, we're using the term to initialize database schema in the context of Spring Session, i.e. to setup database tables/objects that are required for Spring Session JDBC. And by setting the Spring Boot configuration property spring.session.jdbc.initialize-schema to always you are using Spring Boot to set up schema required by Spring Session JDBC.

As noted in my original comment, Spring Boot will initialize use scripts that are shipped within the spring-session-jdbc jar and those are available here. As you can see those script are using the default table name. There is no magic involved here that will dynamically generate the schema scripts, or adjust the existing ones when you supply a non-default table name using spring.session.jdbc.table-name.

If you believe this should be made more obvious, please open a ticket with Spring Boot, as it is Spring Boot's auto-configuration support for Spring Session that's the real subject of this discussion. If you decide to do so, feel free to update this ticket with a link to new one in Spring Boot.

Hopefully this clarifies things a bit @jm-ck?

jm-ck commented 5 years ago

ok, If you say so.

It was pretty intuitive for me that when I can set the table-name and force to initialize-schema which works with the default names, it should also work with custom names.

so maybe your explanations could be included in docs? An this issue is more like a feature request.

vpavic commented 5 years ago

Closing as I believe I've provided you with pointers to relevant info which explains how things are implemented.

so maybe your explanations could be included in docs? An this issue is more like a feature request.

As noted in the previous comment, that request should be opened against Spring Boot.