Spring Boot 1.5.2.RELEASE database backed sessions - java

I am working on a project using Spring Boot 1.5.2.RELEASE and have been tasked with adding database backed HTTP sessions.
So, I got this working in the 2.0.0.RELEASE easily enough and the application started and created the tables spring_session and spring_session_attributes
Here's the properties I added to the later version that got things working:
spring.session.store-type=jdbc
spring.session.jdbc.initialize-schema=ALWAYS
Looking at spring-boot 1.5.2.RELEASE it seems to use spring-session 1.3.0.RELEASE as the managed version so I found the docs here: https://docs.spring.io/spring-session/docs/1.3.0.RELEASE/reference/html5/guides/httpsession-jdbc.html
No matter what I try I get the following error:
Whitelabel Error Page This application has no explicit mapping for
/error, so you are seeing this as a fallback. Tue Mar 20 14:02:06 GMT
2018 There was an unexpected error (type=Internal Server Error,
status=500). PreparedStatementCallback; bad SQL grammar [SELECT
S.SESSION_ID, S.CREATION_TIME, S.LAST_ACCESS_TIME,
S.MAX_INACTIVE_INTERVAL, SA.ATTRIBUTE_NAME, SA.ATTRIBUTE_BYTES FROM
SPRING_SESSION S LEFT OUTER JOIN SPRING_SESSION_ATTRIBUTES SA ON
S.SESSION_ID = SA.SESSION_ID WHERE S.SESSION_ID = ?]; nested exception
is org.postgresql.util.PSQLException: ERROR: relation "spring_session"
does not exist Position: 127
Here is my application.properties (I am trying to get the tables to appear in my PostgreSQL database)- these tables should be created for me automatically, right?
spring.datasource.url=jdbc:postgresql://localhost:5432/sandbox
spring.datasource.password=sandbox
spring.datasource.username=sandbox
spring.thymeleaf.cache=false
spring.template.cache=false
spring.session.store-type=jdbc
spring.session.jdbc.initializer.enabled=true

Can you try after adding this
spring.session.jdbc.table-name=SPRING_SESSION

Unfortunately I couldn’t get the tables to create automatically so I dug through the spring-session jar file for the schema and created them manually. Good news is that sessions are now persisted in my database!

Related

Does the combination of Spring Boot and H2 honor JPA-property javax.persistence.schema-generation.create-database-schemas?

I have a Spring Boot app that is configured to use JPA and its default-provider Hibernate. The app, in environment Non-Local, expects, and, through Oracle, is configured to connect to, an existing SQL DB, while the app, in environment Local, must, through H2, create it...embedded, in-memory, and private.
The DB utilizes one schema, as seen in this entity:
#Entity #Table(schema="foo",name="transactions")
Environment Local must, and is configured to, create (after first dropping any existing) tables and their schema. It is the creation, and dropping, of the latter - schema - that exhibits a problem:
Hibernate: drop table if exists foo.foo_transactions CASCADE
org.hibernate.tool.schema.spi.CommandAcceptanceException: Error executing DDL "drop table if exists foo.foo_transactions CASCADE " via JDBC Statement
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Schema "FOO" not found; SQL statement: drop table if exists foo.foo_transactions CASCADE [90079-200]
...and a similar one (Schema not found) for CREATE.
So as to ensure no overlap, in the classpath, between H2 and Oracle, I, in the POM, separate them by build-profile (local and non-local):
<profile><id>local</id>...<dependencies><dependency>H2</dependency></dependencies>
<profile><id>non-local</id>...<dependences><dependency>Oracle</dependency></dependencies>
My relevant configuration of Spring Boot is:
# file application.properties
spring.datasource.url=jdbc:oracle:thin...
# file application-local.properties
# blankness of following value masks default configuration, in application.properties
spring.datasource.url=
spring.jpa.properties.javax.persistence.schema-generation.create-database-schemas=true
That JPA-property, javax.persistence.schema-generation.create-database-schemas, is supposed to do EXACTLY what I desire: create, in addition to tables, any internal schema. Yet, as shown in the aforementioned errors, it doesn't work!
Does H2, or the combination of H2, JPA-provider Hibernate, and Spring Boot, not honor it??? Or, have I miscoded/misconfigured something?
I don't want to get bogged down in the following (as the preceding question is my main concern), but for full disclosure...
P.S. If I remove the schema and build and run locally, everything works fine. But, the non-local (Production) flavor mandates that schema, so I must comply, and wish to do so also locally.
P.P.S. I am, indeed, aware of H2's directive 'INIT=CREATE SCHEMA IF NOT EXISTS foo' (to be applied to the datasource URL), and it, if used, does alleviate the problem. However, if I do use it (thereby having to explicitly supply a url, thereby conceding Spring Boot's very nice and full auto-configuration [of H2]), it causes another problem, which I need to avoid:
2022-08-29 15:43:27.494 WARN 15288 --- [on(5)-127.0.0.1] o.s.b.f.support.DisposableBeanAdapter: Invocation of destroy method failed on bean with name 'inMemoryDatabaseShutdownExecutor': org.h2.jdbc.JdbcSQLNonTransientConnectionException: Database is already closed (to disable automatic closing at VM shutdown, add ";DB_CLOSE_ON_EXIT=FALSE" to the db URL) [90121-200]
P.P.P.S. Neither that recommendation (...;DB_CLOSE_ON_EXIT=FALSE) nor its sister (...;DB_CLOSE_DELAY=-1) nor their combination alleviate that problem (attempt to close an already-closed DB).
I've now figured it out - my relevant JPA-property, at least as far as Hibernate is concerned, was slightly wrong...
Whereas I used (as it had once worked for me in EclipseLink, lazily leading me to believe that it was generic to all JPA-providers)
javax.persistence.schema-generation.create-database-schemas
I should have used this
javax.persistence.create-database-schemas

Can spring create a new schema (using flyway) at startup and then connect to it via default datasource?

My spring-boot app has following properties set,
spring.jpa.hibernate.ddl-auto=none
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/my-schema
spring.datasource.username=root
spring.datasource.password=*****
spring.flyway.check-location=false
spring.flyway.createSchemas=true
spring.flyway.schemas=my-schema
The schema 'my-schema' does not pre-exist and I would want for it to be created by flyway and then be used by spring-boot app to sping up HikarCP datasource.
If I run the application with the above configurations I get the following error upon startup:
Caused by: org.flywaydb.core.internal.exception.FlywaySqlException:
Unable to obtain connection from database: Unknown database 'my-schema'
Now, if I change,
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/
The application starts up perfectly and creates the schema. However, when it tries to query any table the thrown exception is:
java.sql.SQLException: No database selected
You can configure Flyway with a URL that's used purely for migrations and then configure your app to use a different URL. Something like this:
spring.flyway.url=jdbc:mysql://127.0.0.1:3306
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/my-schema
You'll also need to provide credentials for the Flyway-specific connection to the database using spring.flyway.user and spring.flyway.password.

Java Spring JDBC SPRING_SESSION table doesn't exist

I am developing a simple RESTfull service in Java Spring and using JDBCTemplate.
However, I am getting a run time error, which I don't understand. It complains about SPRING_SESSION table not existing, however I think Spring should be able to create necessary tables as needed.
application.properties:
spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/getfit?useSSL=false
spring.datasource.username=root
spring.datasource.password=***
Exception:
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [DELETE FROM SPRING_SESSION WHERE EXPIRY_TIME < ?]; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'getfit.SPRING_SESSION' doesn't exist
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:235) ~[spring-jdbc-5.0.4.RELEASE.jar:5.0.4.RELEASE]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) ~[spring-jdbc-5.0.4.RELEASE.jar:5.0.4.RELEASE]
Exception is triggered by some process attempting to delete record from table which doesn't exist.
Manually creating the table leads to more errors (not existing columns). Setting
hibernate.ddl-auto=update
also doesn't help.
Additionally, I have
modified MySQL configuration to allow upper case characters and this also did not help.
Interestingly enough, this issue happened after I formatted my OS and cloned project from github. Before everything was working fine.
Do you have any ideas what could go wrong? Let me know if you need me to show you some code.
Thanks for looking in to this :)
try to set the initialize-schema to always:
spring.session.jdbc.initialize-schema: always
Spring session creates a table to store sessions in the database. Since the required table doesn't exist it throws the error.
Here's a link you can refer
https://sivalabs.in/2018/02/session-management-using-spring-session-jdbc-datastore/

Converting java app from using TomEE to using TomEE+, currently using Hibernate 4, JPA queries stopped working

Converted java app from using TomEE to using TomEE+, currently using Hibernate 4, JPA queries stopped working. I get the following error when I ran a SELECT JPA query:
WARN org.hibernate.engine.jdbc.spi.SqlExceptionHelper - SQL Error: -5501, SQLState: 42501
ERROR org.hibernate.engine.jdbc.spi.SqlExceptionHelper - user lacks privilege or object not found: <Database table object name>
This was NOT happening when I used TomEE with Dozer. Dozer was taken out and excluded from the newly converted app. Need to know what i need to do to make this work.

Hibernate Exception during deploy

I´m using hibernate 4.3.6 in my vaadin project.
Every time I make changes in the sources code, it is expected that the application builds again and the new source code is deployed automatically to Tomcat. In other words, Tomcat should reload its context.
The problem is that during this operation hibernate throws an error:
GRAVE: Exception loading sessions from persistent storage
org.hibernate.HibernateException: registry does not contain entity manager factory: myproject
at org.hibernate.jpa.internal.EntityManagerFactoryRegistry.getNamedEntityManagerFactory
(...)
After that log, i get:
24/09/2014 13:14:43 org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/myproject] is completed
However, I cannot continue using the website, since I receive a message saying that session is lost.
My question is: what is this hibernate exception and how can I solve it?
EDIT:
This error only happens when I store in session a JPA Entity, for example: the logged user
I don´t know any way to get what you want, in Tomcat, except with JRebel. The staff of Vaadin itself uses and recommends. Link with interesting information about Vaadin+JRebel: http://zeroturnaround.com/blog/jrebel-case-study-vaadin-eliminates-redeploys-and-saves-10-of-development-time/
If in the future you decide to use Jetty instead of Tomcat, you can make settings and get dynamic reloading of the application as suggested here: https://blog.oio.de/2012/08/23/dynamic-reloading-of-vaadin-applications-with-maven-and-eclipse /

Categories

Resources