I have a springboot application with H2 and have tried out some simple api(s) that does CRUD. Now I am trying to move the db to MySQL running in a docker instance.
I have flyway configured with init script under /resources/db.migration package. I am able to connect to database from my intellij. I am able to build and start the application.
However, upon sending a REST request to save a resource, the app throws an exception. And I see java.sql.SQLSyntaxErrorException: Table 'task-db.customer' doesn't exist in the application log. But after building the project, I could see the customer table created via Flyway. So the table does exist before starting the application.
I tried rebuilding/invalidating cache and restarting intellij but did not help.The only fly configuration I have used is -flyway:locations: classpath:db/migration. Below is the DDL in the flyway init script.
CREATE TABLE IF NOT EXISTS `CUSTOMER` (
`CUSTOMER_ID` int NOT NULL AUTO_INCREMENT,
`FIRST_NAME` varchar(45) DEFAULT NULL,
PRIMARY KEY (`CUSTOMER_ID`)
);`
How can I resolve it? Where should I be looking at? Am I missing something?
Updated the tables to lowercase in the flyway script and entities and that worked.
I planned to use ExampleDS (java:jboss/datasources/ExampleDS) for Java development before going to production. It is the default data source in Wildfly, configured using the embedded H2 database for developer convenience. I need to see the data in these tables during development. I am developing in IntelliJ. There is a ”database” tab to the upper right. I have previously added databases here and I could see their content. But I don’t know how to add the ExampleDS database.
I then tried this approach instead:
1) Deploy a prebuilt WAR-file for H2 Console: https://www.cs.hs-rm.de/~knauf/JavaEE6/kuchen/H2Console.war )
2) Open URL http://localhost:8080/H2Console/h2
3) login with username/password = "sa"
I could then access the H2 console. It was however impossible to login with the default JDBC URL
java:jboss/datasources/ExampleDS
I tried with name and password sa, but it always says "wrong password".
So I read the documentation on
https://developers.redhat.com/quickstarts/eap/h2-console/
It says:
To access the test database that the greeter quickstart uses, enter these details:
JDBC URL: jdbc:h2:mem:greeter-quickstart;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
User Name: sa
Password: sa
So I tried that and I could finally connect. But there seems to be no tables in the database. The documentation says:
Take a look at the data added by the greeter application. Run the following SQL command:
select * from users;
I tried that, but got the message
Table "USERS" not found
The tables that I have created in my JPA app can also not be found. I assume that is because my persistance file points to
java:jboss/datasources/ExampleDS
So I changed it to
jdbc:h2:mem:greeter-quickstart;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1
Then I got this error when trying to run my JPA app from IntelliJ:
"WFLYCTL0412: Required services that are not installed:" => ["jboss.naming.context.java.jdbc:h2:mem:greeter-quickstart;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1"],
"WFLYCTL0180: Services with missing/unavailable dependencies" => [
Does anypne know how I should proceed with this to actually add my tables to a H2 database and then to see the data in these tables?
I think you don't have some of the concepts clear:
H2 has several options to create a database, using a file as basis or in-memory.
The in-memory is not stored in any place, for this reason if you configure it in your app like this:
jdbc:h2:mem:whatever
The jvm starts a database in their own memory, only available for the vm.
If you want to connect from outside I recommend to use the embedded or server mode.
Ofc an in-memory database will be empty, you have to populate it in start-up.
I used it a lot for testing. It's pretty cool.
Check the H2 cheatsheet:
https://www.h2database.com/html/cheatSheet.html
I have a application where I used these technologies:
Grails 3.3.0
JDK 1.8
Spring 4+
Mysql 8
GORM 6.1.6.RELEASE
org.grails.plugins:spring-security-core:3.2.0
Hibernate 5+
Problem is when I am trying to connect the application with Mysql 8(with 5.6+ it is working fine), I am not able to get the information related to user from Grails-Spring-Security plugin.
The application is running even connect to DB but wont be able to authenticate or fetch the information of the User like findByUsername where username is property in my user domain class.
I have User domain class defined in application.properties file.
grails.plugin.springsecurity.userLookup.userDomainClassName = 'com.aaa.User'
At some point I found this error but not sure whether it is related to this or not.
java.lang.IllegalStateException:
Either class [com.aaa.User] is not a domain class or GORM has not been initialized correctly or has already been shutdown.
Ensure GORM is loaded and configured correctly before calling any methods on a GORM entity.
Want to understand why it wont be able to fetch the information from DB. I have tried lot of things like change the GORM version to 6.1.7 and grails spring-secuirty-core plugin version but not able to get anything.
Any help would be appreciated.
Thanks,
Atul
We are able to solve the issue. It happens when grails-hibernate plugin look for the property(in my case its username as I am calling API findByUsername) which is present in the static api or not. If it is not there it throws the exception.
The thing worked for me is, I have to put the property in static mapping of User domain class:
static mapping = {
table 'userTable'
id column: 'id'
password column: 'userpassword'
username column: 'username'
}
I am not sure why it is happening, when I run the application with Mysql5.6+ its working.(The driver is according to it) but when go with Mysql8, it look for the property in static mapping.
One more point I would like to mention to fix the issue is, make sure you have tablename, columnname same as defined in DB. Case sensitivity is what expected.
As I mentioned the case sensitivity, if you are using linux with Mysql8, the lower_case_table_names=0, this check with the following as per Mysql official documentation:
Table and database names are stored on disk using the lettercase
specified in the CREATE TABLE or CREATE DATABASE statement. Name
comparisons are case sensitive. You should not set this variable to 0
if you are running MySQL on a system that has case-insensitive file
names (such as Windows or macOS). If you force this variable to 0 with
--lower-case-table-names=0 on a case-insensitive file system and access MyISAM tablenames using different lettercases, index corruption
may result.
So if the table name is static is in camel or upper case and in db it is lower case it wont match. and the error occurred.
Hope this helps to someone.
I use JDBC and created h2 database called usaDB from sql script. Then I filled all tables with jdbc.
The problem is that after I connect to usaDB at localhost:8082 I cannot see on the left tree
my tables. There is only INFORMATION_SCHEMA database and rootUser which I specified creating usaDB.
How to view the content of tables in my h2 database?
I tried query SELECT * FROM INFORMATION_SCHEMA.TABLES.
But it returned many table names except those I created. My snapshot:
I had the same issue and the answer seems to be really stupid: when you type your database name you shouldn't add ".h2.db" suffix, for example, if you have db file "D:\somebase.h2.db" your connection string should be like "jdbc:h2:file:/D:/somebase". In other way jdbc creates new empty database file named "somebase.h2.db.h2.db" and you see what you see: only system tables.
You can use the SHOW command:
Using this command, you can lists the schemas, tables, or the columns of a table. e.g.:
SHOW TABLES
This problem drove me around the twist and besides this page I read many (many!) others until I solved it.
My Use Case was to see how a SpringBatch project created in STS using :: Spring Boot :: (v1.3.1.RELEASE) was going to behave with the H2 database; to do the latter, I needed to be able to get the H2 console running as well to query the DB results of the batch run.
This is what I did and found out:
Created an Web project in STS using Spring Boot:
Added the following to the pom.xml of the latter:
Added a Spring configuration file as follows to the project:
This solves the Web project deficiencies in STS. If you run the project now, you can access the H2 console as follows: http://localhost:8080/console
Now create a SpringBatch project in STS as follows (the alternative method creates a different template missing most of the classes for persisting data. This method creates 2 projects: one Complete, and the other an initial. Use the Complete in the following.):
The SpringBatch project created with STS uses an in memory H2 database that it CLOSES once the application run ends; once you run it, you can see this in the logging output.
So what we need is to create a new DataSource that overrides the default that ships with the project (if you are interested, just have a look at the log messages and you will see that it uses a default datasource...this is created from:
o.s.j.d.e.EmbeddedDatabaseFactory with the following parameters:
Starting embedded database: url='jdbc:hsqldb:mem:testdb', username='sa')
So, it starts an in memory, and then closes it. You have no chance of seeing the data with the H2 console; it has come and gone.
So, create a DataSource as follows:
You can of course use a properties file to map the parameters, and profiles for different DataSource instances...but I digress.
Now, make sure you set the bit that the red arrow in the picture is pointing to, to a location on your computer where a file can be persisted.
Running the SpringBatch (Complete project) you should now have a db file in that location after it runs (persisting Person data)
Run the Web project you configured previously in these steps, and you WILL :=) see your data, and all the Batch job and step run data (et voila!):
Painful but rewarding. Hope it helps you to really BOOTSTRAP :=)
I have met exactly this problem.
From what you describe, I suppose that you connect your jdbc with the "real" h2 server, but you are connecting on web application to database by the wrong mode (embedded-in-memory mode, aka h2mem). It means that h2 will create a new database in-memory, instead of using your true database stored elsewhere.
Please make sure that when you connect to this database, you use the mode Generic H2 (Server), NOTGeneric H2 (Embedded). You can refer to the picture below.
Version of jar file and installed h2 database should be same.
If in case you have created and populated H2 database table using maven dependency in spring boot, then please do change the JDBC URL as jdbc:h2:mem:testdb while connecting to H2 using web console.
It is an old question, but I came across the same problem. Eventually I found out that the default JDBC URL is pointing a test server rather than my application. After correcting it, I could access the right DB.
I tried with both Generic H2 (Embedded) and the Generic H2 (Server) options, both worked as long as the JDBC URL: is provided correctly.
In grails 4.0.1 the jdbc URL for development is jdbc:h2:mem:devDb. Check your application.yml file for the exact URL.
For the people who are using H2 in embedded(persistent mode) and want to "connect" to it from IntelliJ(other IDEs probably apply too).
Using for example jdbc url as follows: jdbc:h2:./database.h2
Note, that H2 does not allow implicit relative paths, and requires adding explicit ./
Relative paths are relative to current workdir
When you run your application, your workdir is most likely set to your project's root dir
On the other hand, IDE's workdir is most likely not your project's root
Hence, in IDE when "connecting" to your database you need to use absolute path like: jdbc:h2:/Users/me/projects/MyAwesomeProject/database.h2
For some reason IntelliJ by default also adds ;MV_STORE=false. It disables MVStore engine which in fact is currently used by default in H2.
So make sure that both your application and your IDE use the same store engine, as MVStore and PageStore have different file layouts.
Note that you cannot "connect" to your database if your application is using it because of locking. The other way around applies too.
In my case the issue was caused by the fact that I didn't set the h2 username, password in java. Unfortunatelly, Spring didn't display any errors to me, so it was not easy to figure out. Adding this lines to dataSource method helped me fix the issue:
dataSource.setUsername("sa");
dataSource.setPassword("");
Also, I should have specified the schema when creating tables in schema.sql
Selecting Generic H2 (Server) solved for me. We tempted to use default Generic H2 (Embedded) which is wrong.
I am connecting to an external database (i.e. Oracle 10g) using my Play 1.2.4 application.
I have provided all the DB details in the application.conf file, then added the ojdbc.jar in the classpath and the connection seems to work fine. But the problem is everytime when the application access some data for the first time. it is trying to create the entity table (which is already present) and hence throws an exception.
I don't want to create any table since its already there with some data, hence how can I avoid this?
There might be some configuration to set in the application.conf for accessing the external database where the table is already available(I believe), kindly help me on this.
Below is the conf file:
application.mode=dev
%prod.application.mode=prod
db.url=jdbc:oracle:thin:#localhost:1521/orcl
db.driver=oracle.jdbc.OracleDriver
db.user=****
db.pass=****
#%test.module.cobertura=${play.path}/modules/cobertura
%test.application.mode=dev
%test.db.url=jdbc:h2:mem:play;MODE=MYSQL;LOCK_MODE=0
%test.jpa.ddl=create
%test.mail.smtp=mock
Note: Presently I am connecting to the external Oracle database and not with the embedded H2 database.
In your application.conf set the following property to none:
jpa.ddl=none