Consider we have a H2 database which is started from a web-application under Tomcat using Hibernate. In other words it is an embedded H2 database into the application.
The question: is it possible to programmatically stop this H2 server from this application and then start it again?
P.S. Server.createTcpServer(args).start(); or Server.shutdown(...) is not the way, because it is in the embedded mode.
In this particular case such an approach will be a workable solution:
To stop H2 database just use SHUTDOWN sql command:
session.createSQLQuery("SHUTDOWN").executeUpdate();
To restart H2 you don't need to do anything: the Tomcat's connection pool will do it automatically.
Related
We have a Java application (basically integration tests) that uses Hibernate (which uses Jdbc) to read/write data to the MySQL Database. Hibernate objects like sessions or transactions are created and configured via our own code (no Spring or other wrappers are being used). The issue is that periodically (multiple times during tests execution) we observe a "No database selected" exception. Database URL that we use for DataSource configuration already contains database name in it:
jdbc:mysql://localhost:3306/test?useSSL=false&createDatabaseIfNotExist=false&cacheServerConfiguration=true&cacheResultSetMetadata=true&useLocalSessionState=true&rewriteBatchedStatements=true&tcpNoDelay=true&tcpTrafficClass=16&alwaysSendSetIsolation=false&tcpSndBuf=1048576&tcpRcvBuf=1048576&characterEncoding=utf8&allowPublicKeyRetrieval=true
I tried to catch the Exception and test the connection's selected database by running select database() and it actually reports that the value is null on the database side.
Even more strange thing is that next queries on the same connection are executed against the normal database (so it somehow self-heals).
Does anybody know why can MySQL connections "lose" and then "restore" selected database?
Or maybe there is a way to trace the problem down. Will be grateful for any help or thought that you can provide
Versions:
Java 1.8.0_292
Mysql 5.6.31
Hibernate 5.4.2
JDBC mysql-connector-java 8.0.22
We are using spring boot, apache camel and multiple datasources.
spring boot version: 1.5.9 release
primary database: postgres
Secondary database:oracle
We have deployed spring boot jar in linux server.
Datasource properties for postgres is
x.datasource.url=
x.datasource.username=
x.datasource.password=
x.data.jpa.repositories.enabled=true
datasource properties for oracle
y.datasource.url=
y.datasource.username=
y.datasource.password=
y.data.jpa.repositories.enabled=true
when application keeps ideal and After some times we are trying first request failed and getting error jdbc connection failed but it is working in second request without restarting.
please let me know how to keep database active.
You can use hikari connection pool. Refer to this on how to implement. You can configure no. of connections you want to keep with db
My database.properties file is:
datasource.driver=org.h2.Driver
datasource.url=jdbc:h2:file:./test_database/comixed_db;create=true
datasource.username=sa
datasource.password=
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.hbm2ddl.auto=update
hibernate.show_sql=false
hibernate.format_sql=true
hibernate.batch.size=20
hibernate.current.session.context.class=org.springframework.orm.hibernate5.SpringSessionContext
hibernate.generate_statistics=false
hibernate.enable_lazy_load_no_trans=false
When my application starts up I see I am able to see the tables by using the h2.bat tool and peeking at the database from a web browser. However, when I shut down the application and then go back to the database with the h2.bat tool the tables are all gone!
AM I doing something incorrectly with my hibernate configuration? I am not using create-drop but update since this code is currently in flux and I'd like the tables to be adjusted as changes occur. But that doesn't seem to be the issue since it's at app shutdown that the tables keep going away.
Any help would be appreciated.
If you want spring boot to catch your hibernate properties you should prefix them with spring.jpa, so:
spring.jpa.hibernate.ddl-auto=update
Otherwise, and that is the case in my opinion, spring will use the default create-drop options as it is dealing with an H2 in-memory database.
By adding the following line to applications.properties:
spring.jpa.hibernate.ddl-auto=update
Spring-boot stopped dropping tables when the application exits.
I am trying to deploy my spring + Hibernate application on JBOSS. while i am starting the server, server start is getting hung. Last entry in the start up log is -
DEBUG: org.springframework.jdbc.datasource.DriverManagerDataSource - Creating new JDBC DriverManager Connection to [myDatabaseURL]
In my application there are around 80 hibernate mapping file. when I am removing all of them excluding one simple basic mapping file, application is able to connect to the DB.
I also try to connect to DB using PLSQL developer and I was able to connect. So in other words DB is working fine.
Any suggestion or pointer for debug ? What can be possible reasons which i am overlooking?
Thanks
I am using embedded database H2 in 2 web applications say WebApp1 and WebApp2. I run WebApp1 and execute some query to access H2 database. Mean while I run WebApp2, but it throws exception that H2 is currently been used by another process
My need is, I should be able to use H2 database by WebApp1 and WebApp2 at the same time. I analysed the below link but it tells about multiple connections but not about multiple applications.
http://www.h2database.com/html/features.html#multiple_connections
You can use the embedded mode for WebApp1 and the server mode for WebApp2: in order to get this trick, both urls must be like "jdbc:h2:mydb;AUTO_SERVER=TRUE"
More info here: http://h2database.com/html/features.html#auto_mixed_mode