H2 database "if" query grammar [duplicate] - database

I have some problems with using a schema.sql file to create my sql schema when executing a junit test while this schema contains mysql specific expression. I have to add the mode=mysql to the H2 url.
For example something like this:
jdbc:h2:mem:testd;MODE=MYSQL
But Spring boot automatically uses the url defined in the enum
org.springframework.boot.autoconfigure.jdbc.EmbeddedDatabaseConnection with its url
jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE.
I have tried similiar approaches to get this to work, but spring does not take the spring.datasource.url=jdbc:h2:mem:testdb;MODE=MYSQL from my test-application.properties. All other settings from my test-application.properties have been read successfully.
If I let spring/hibernate create the schema (without the schema.sql file) with the javax.persistence annotations in my entities everything works fine.
Is there a simple way to add a mode?

Set
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL
in application-test.properties, plus
#RunWith(SpringRunner.class)
#DataJpaTest
#AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
#ActiveProfiles("test")
on the test class

I was having this same issue. It would not pick up the url when running tests. I'm using flyway to manage my scripts. I was able to get all of these working together by following these few steps.
Created a V1_init.sql script in src/test/resources/db/migration so that it is the first script run by flyway.
SET MODE MYSQL; /* another h2 way to set mode */
CREATE SCHEMA IF NOT EXISTS "public"; /* required due to issue with flyway --> https://stackoverflow.com/a/19115417/1224584*/
Updated application-test.yaml to include the schema name public:
flyway:
schemas: public
Ensure the test specified the profile: #ActiveProfiles("test")

I have tried similiar approaches to get this to work, but spring does not take the spring.datasource.url=jdbc:h2:mem:testdb;MODE=MYSQL from my test-application.properties
Did you try to append this parameters instead of rewriting the existing ones?
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL
All other settings from my test-application.properties have been read successfully.
I thought that file should be named application-test.properties.

I was able to run it with this config:
# for integration tests use H2 in MySQL mode
spring.datasource.url=jdbc:h2:mem:testdb;DATABASE_TO_LOWER=TRUE;MODE=MySQL;
spring.jpa.database-platform=org.hibernate.dialect.MariaDBDialect
The main trick here is to force Hibernate to generate SQL scripts for MariaDB dialect because otherwise Hibernate tries to use H2 dialect while H2 is already waiting for MySQL like commands.
Also I tried to use more fresh MariaDB103Dialect for MariaDB 10.3 but it doesn't worked properly.

You need to set MYSQL mode on h2 and disable replacing of datasource url for embedded database:
Modify application-test.yaml
spring:
datasource:
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false;MODE=MYSQL
test:
database:
replace: NONE

Related

How to select a database in run time using spring boot

I have created an application with 2 databases. I need to select one of them in run time. Is there any method or class in spring boot.
You can do it using application.properties configurations. You don't need to restart the server for those configurations to act.
Please refer to:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
spring.datasource.url=jdbc:mysql://localhost/test
spring.datasource.username=dbuser
spring.datasource.password=dbpass
spring.datasource.driver-class-name=com.mysql.jdbc.Drive
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html

JNDI datasource and ApplicationModule tester

I have an ADF application module and I have it configured to use a JNDI datasource (e.g., JNDI name = jdbc/ORCL).
That all works fine, but when I try to test my Application Module through the integrated tester in JDeveloper (i.e., run the application module directly instead of running a page), it cannot find the JNDI data source, even if my integrated Weblogic Server is started.
Is this just a drawback of using JNDI: that you cannot use the integrated application module tester anymore? Or is there a way to configure JDeveloper to make this all work together (JNDI + integrated AM tester)?
You should make use of the default JDBC url, i.e. java:comp/env/jdbc/ORCL instead of using the short form jdbc/ORCL. Many features of JDeveloper rely on the full url to work. For example, even if you don't manually create the datasource on your Integrated WebLogic Server, you still can right-click and run a page to test because behind the scene, JDeveloper automatically create a temporary datasource with that name to run the application.
When you deploy your application to an actual standalone server, the application will automatically trim the java:comp/env/ portion of the full url and look for the datasource with the name jdbc/ORCL. You don't need to shorten it yourself :).

Why are added tables from Java not visible when I open the H2 console?

I use an H2 embedded database in Java, and after creating the database and adding some tables and data, it gets saved as a file in a directorey of my computer.
But whenever I open this file with the H2 Console, it shows no tables at all? Why are the tables not there?
I am using this URL in my java code: jdbc:h2:file://C:/Temp/H2/ourDB
And I log into the console with the following information:
After logging in, I can't see the tables that were created in Java?
You have used a different database URL in the H2 Console (not jdbc:h2:file://C:/Temp/H2/ourDB). You need to use the same one, otherwise it's a different database.
The accepted ans is correct- it's URL issue. But for recent user, I am posting the updated values (I am using SpringBoot 2 with Spring Data JPA). When you open H2 Console, the default URL shown for DB Connection is: jdbc:h2:~/test . If you connect using this URL, you won't find the tables you created in your app. But user generated tables to be seen use URL as jdbc:h2:mem:testdb. Pls, refer below figures:
Here EXCHANGE_VALUE was my JPA generated table, that i could see after connecting to db using URL as jdbc:h2:mem:testdb.

Play Siena failing to connect to MySQL on GAE

I am using play framework 1.2.7, gae module 1.6.0 and siena module 2.0.7 (also tested 2.0.6). This is a simple project that should run in play deployed on App Engine and connect to a MySQL database in Google Cloud SQL. My project runs fine locally but fails to connect to the database in production. Looking at the logs it looks like it is using the postgresql driver instead of the mysql one.
Application.conf
# db=mem
db.url=jdbc:google:mysql://PROJECT_ID:sienatest/sienatest
db.driver=com.mysql.jdbc.GoogleDriver
db.user=root
db.pass=root
This is the crash stack trace
play.Logger niceThrowable: Cannot connected to the database : null
java.lang.NullPointerException
at com.google.appengine.runtime.Request.process-a3b6145d1dbbd04d(Request.java)
at java.util.Hashtable.put(Hashtable.java:432)
at java.util.Properties.setProperty(Properties.java:161)
at org.postgresql.Driver.loadDefaultProperties(Driver.java:121)
at org.postgresql.Driver.access$000(Driver.java:47)
at org.postgresql.Driver$1.run(Driver.java:88)
at java.security.AccessController.doPrivileged(AccessController.java:63)
at org.postgresql.Driver.getDefaultProperties(Driver.java:85)
at org.postgresql.Driver.connect(Driver.java:231)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at play.modules.siena.GoogleSqlDBPlugin.onApplicationStart(GoogleSqlDBPlugin.java:103)
at play.plugins.PluginCollection.onApplicationStart(PluginCollection.java:525)
at play.Play.start(Play.java:533)
at play.Play.init(Play.java:305)
What is going on here? I am specifying the correct driver and url schema and it's using postgresql driver. Google Cloud SQL API access is enabled, the app is allowed to connect to the mysql instance, I am not using db=mem, ... I am stuck and can't figure out how to move forward! :-((
UPDATE: I thought I found the solution, but that was not the case. If I keep the %prod. prefix and create a war normally (or just don't define any DB properties), then the application will use Google DataStore instead of the Cloud SQL. If I create the war file adding --%prod at the end (or just delete the %prod. prefix in the application.conf), then it will keep failing to connect to the database showing the same initial error.
Any ideas please?
After being stuck for so long on this I just found the solution in no time after posting the question. Quite stupid actually.
The production environment properties in the application.conf file must be preceded by %prod. so the database config should read
%prod.db.url=jdbc:google:mysql://PROJECT_ID:sienatest/sienatest
%prod.db.driver=com.mysql.jdbc.GoogleDriver
%prod.db.user=root
%prod.db.pass=root
And everything runs fine.
EDIT: This is NOT the solution. The problem went away, but the app is using the DataStore instead of the Cloud SQL
At the end I ended doing a slight modification in play siena module source code and recompiling it.
In case anyone is interested, you will need to remove/comment/catch exception in this code around line 97 in GoogleSqlDBPlugin class:
// Try the connection
Connection fake = null;
try {
if (p.getProperty("db.user") == null) {
fake = DriverManager.getConnection(p.getProperty("db.url"));
} else {
fake = DriverManager.getConnection(p.getProperty("db.url"), p.getProperty("db.user"), p.getProperty("db.pass"));
}
} finally {
if (fake != null) {
fake.close();
}
}
For some reason the connection fails when initiated with DriverManager.getConnection() but it works when initiated with basicDatasource.getConnection(); which apparently is the way used by the module in the rest of the code. So if you delete the above block, and recompile the module everything will work as expected. If you are compiling with JDK 7, you will also need to implement public Logger getParentLogger() throws SQLFeatureNotSupportedException in the ProxyDriver inner class at the end of GoogleSqlDBPlugin file.
Strangely, I digged into the DriverManager.getConnection() and it looked like some postgresql driver is registered somehow, because otherwise I can't see why DriverManager.getConnection() would call to org.postgresql.Driver.connect().

How do I make a CRON job to use the export feature of phpmyadmin to export DB

I want to export some tables in my DB to an Excel/Spreadsheet every month.
In PHPMyAdmin there is a direct option of exporting the result of a query to the desired filetype. How do I make use of this export feature without another script to run a cronjob on a monthly basis?
Basically on a CPanel (the DB is hosted in the web) we just have to give the path to the script to be executed via a cronjob. But in PHPMyAdmin there is no such opportunity. Its an included feature of PHPMyAdmin where we generally click and do it mannually. So how do i do it in Cpanel?
Do you have ssh access to the box? Personally I'd implement this outside of phpmyadmin, as phpmyadmin is just intended for manual operations via the interface. Why not write a simple script to export the db?
Something like mysqldump database table.
Being a web-app, the export function is a POST request. In the demo application the URL is http://demo.phpmyadmin.net/STABLE/export.php, and then the post data contains all the required parameters, for example: (You can use Fiddler/Chrome dev tools too view it)
token:3162d3b849cf652c2577a45f90022df7
export_type:server
export_method:quick
quick_or_custom:custom
output_format:sendit
filename_template:#SERVER#
remember_template:on
charset_of_file:utf-8
compression:none
what:excel
codegen_structure_or_data:data
codegen_format:0
csv_separator:,
csv_enclosed:"
.....
The one tricky bit is the authentication token, but I believe this is also possible to overcome using some configuration and/or extract parameters (like the 'direct login' in http://demo.phpmyadmin.net/)
See here
How to send data using curl from Linux command line?
If you want to avoid all this, there are many other web-automation tools that can record the scenario and play it back.
just write a simple php script to connect to your database and use the answer here:How to output MySQL query results in CSV format?

Resources