I am using HSQLDB for JUnit Tests, the test data is provided by XML Files with dbUnit. Our application uses an Oracle DB in production.
In order to suppert Oracle-specific queries I have added the property sql.syntax_ora=true to the url like this:
db.url=jdbc:hsqldb:mem:unitdb;sql.syntax_ora=true
When I run my JUnit test I get the exception
org.hsqldb.HsqlException: The table data is read only
I have also tried to execute the query SET DATABASE SQL SYNTAX ORA TRUE;
but this gives me the same exception.
Please help me with this problem. Thanks!
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
I have this Spring Boot Application, where I have used h2 database to cover Junit test case. Everything is working fine until I have this new scenario where I need to execute a MySql query using jdbcTemplate.
The jdbcTemplate is to execute below query
'INSERT IGNORE INTO someTableName ......'
It is working fine when running the service and from postman getting successful response but jUnit test cases are started failing after this change.
I have used #DataJdbcTest annotation to support h2 database based jUnit test case.
I am pretty much sure the issue is due to IGNORE keyword in sql query, but I need to know if there is simple solution for this issue.
You could try h2's MySQL Compatibility Mode.
From their docs:
INSERT IGNORE is partially supported and may be used to skip rows with duplicate keys if ON DUPLICATE KEY UPDATE is not specified.
In my project we are using oracle database as the main application db and H2 as in memory database to run only the integration test cases. Oracle db has many functions which are of the structure "schema.package.function()". The problem is, i'm not able to recreate the same function in H2 for integration test cases as H2 treats it in the form of "dbname.schemaname.functionname()".
for example : When code runs with oracle db , "SELECT MDM.NEXT_KEYS.NEXT_REF_SOURCE_KEY from dual" works. During integration test case on H2, it throws error "DB MDM not found". So i set the db name as MDM and schema name as NEXT_KEYS in h2 setup.It worked. But my next function PAYERS.KEY_TRANSLATIONS.CORE_ENHANCED_DESC_4_KEY fails now saying "DB PAYERS not found".
Changing the oracle functions is out of the equation as they are used by multiple teams.
If this is not possible with H2 , can you suggest a good alternate in-memory db for spring boot
Appreciate your help !
I was able to fix the issue. Adding the solution link here so that it might help someone
Setting this flag IGNORE_CATALOGS=TRUE fixed the issue. When it is enabled, name of catalog (database) is ignored.
spring.datasource.url = jdbc:h2:mem:testdb;MODE=Oracle;IGNORE_CATALOGS=TRUE
Refer the below link https://github.com/h2database/h2database/issues/2893
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/
I am using EclipseLink with Oralce DB and would like to get database name, schema version and url via EntityManager. I have already tried solution mentioned in following question
Getting a JDBC connection from EclipseLink
But this approach is not working for me and I have mentioned the problem in following question:
Getting sql connection via EclipseLink
I would like to know how can I get the required data directly via native query. I have an instance of entityManager and using it, I can execute native queries and get what I want