Capturing sql queries along with bind parameters into log files - java

I used p6spy/log4jdbc to capture the sql queries along with the bind parameters, connecting to Oracle database (hibernate 4.3.5 and oracle 10g is used) and push it to log files which are configured using logback.
The datasource is created by providing the proxy details required for p6spy/log4jdbc.
But, is there any way to enable/disable this feature and switch back to oracle datasource at runtime?
I guess it requires a runtime switch of datasource at runtime..?
Is there any other approach to capture the sql queries along with bind parameters with out using any external libraries like p6spy/log4jdbc..?

You can disable the logging for P6Spy by simply changing your logging configuration. Just change it from INFO to WARN or higher to disable the logging.

Related

How can I turn off logging in HSQL DB with URL parameter?

I am using java-ee application running on wicket.
The problem is, that the connection to the HyperSQL database with JDBC creates a huge log file *.log. Example of my URL:
jdbc:hsqldb:file:C:\database\text;hsqldb.sqllog=0;hsqldb.applog=0
I need to turn off the logging with some jdbc url parameter. I have tried these:
hsqldb.sqllog=0;hsqldb.applog=0;
That is not working, so I have tried this combination:
hsqldb.log_data=false;hsqldb.reconfig_logging=false
But with these parameters, when I stop the app, the data disappears.
The text.log file is the record of transactions performed on your database. If you disable it with hsqldb.log_data=false the transactions are not persisted to disk. The name is derived from the file name on your database URL.
The hsqldb.sqllog and hsqldb.applog settings are for diagnostics and default to 0.
The hsqldb.reconfig_logging=false is also for diagnostics and works together with hsqldb.applog setting. These settings are discussed in the Guide: http://hsqldb.org/doc/2.0/guide/management-chapt.html#mtc_monitoring_operation
So if you want your data to survive when your app shuts down, you should not disable the default log. See http://hsqldb.org/doc/2.0/guide/management-chapt.html#mtc_cache_persistence on how to reduce the size of the text.log file.

Difference between spring.jpa.hibernate.hbm2ddl and spring.jpa.hibernate.ddl

What is the difference between spring.jpa.hibernate.hbm2ddl and spring.jpa.hibernate.ddl?
I have found in this question: What are the possible values of spring.datasource.initialization-mode? that OP is using both in properties, however it seems like the origin of hbm2ddl is hibernate directly not Spring Data Jpa.
Nevertheless, reading the answer from another OP, it looks like pass-through only.
However in our commercial project with mariadb, when we do not close our spring boot application gracefully with spring.jpa.hibernate.hbm2ddl.auto=create, when the application is run again, it deletes old data and creates everything from scratch. On the other hand with spring.jpa.hibernate.ddl.auto=create every second run (after no graceful application shutdown) causes key constraint exceptions (DB is not being dropper before creation)
From this Link
By default, JPA databases are automatically created only if you use an embedded database (H2, HSQL, or Derby).
You can explicitly configure JPA settings by using spring.jpa.* properties. For example, to create and drop tables you can add the following line to your application.properties:
spring.jpa.hibernate.ddl-auto=create-drop
Hibernate’s own internal property name for this (if you happen to remember it better) is hibernate.hbm2ddl.auto.
From this Link
spring.jpa.hibernate.ddl-auto This is actually a shortcut for the "hibernate.hbm2ddl.auto" property.
Defaults to "create-drop" when using an embedded database and no schema manager was detected. Otherwise, defaults to "none".
From this Link
Spring Boot can automatically create the schema (DDL scripts) of your DataSource and initialize it (DML scripts).
It loads SQL from the standard root classpath locations: schema.sql and data.sql, respectively.
In addition, Spring Boot processes the schema-${platform}.sql and data-${platform}.sql files (if present), where platform is the value of spring.datasource.platform.
This allows you to switch to database-specific scripts if necessary. For example, you might choose to set it to the vendor name of the database (hsqldb, h2, oracle, mysql, postgresql, and so on).

Make schema created by Spring JDBC the default schema

I'm using Spring Boot and JDBC for my database connection. I placed schema.sql at the classpath to initialize a schema and tables.
Because the schema doesn't exist yet while connecting to the datasource, I have to configure the datasource in application.properties like so:
spring.datasource.url=jdbc:mysql://localhost:3306/
schema.sql:
CREATE DATABASE IF NOT EXISTS <schema_name>
USE <schema.name>;
CREATE TABLE...
So I select the schema after creating it. This obviously doesn't persist for too long.
How do I configure this properly? Is there a way to select a default schema after the create script or maybe change the datasource url?
With JDBC you need to use Connection.setCatalog to switch between databases. You should not use USE <databasename> as the JDBC driver itself needs to be aware of which database it is operating on.
Based on your code from the schema.sql
USE <schema.name>;
This will not work for your java environment. The schema.sql will be executed and finished, which will not cater your requirement to set the default schema.
The General approach will be to use JDBC URL as;
jdbc:mysql://localhost:3306/DB_NAME
This will set the default db as DB_NAME.
Assumptions: I am assuming that you want to connect to single node DB without any loadbalancer or failover mechanism to be used. URL may change based on these features to be configured.
If you are not specifying the DB_NAME in the URL, it means their is no default schema.
You have 2 options to access the DB in that case.
1) Always use the Connection.setCatalog() method to specify the desired database in JDBC applications, rather than the USE database statement.
2) fully specify table names using the database name (that is, SELECT dbname.tablename.colname FROM dbname.tablename...) in your SQL. Opening a connection without specifying the database to use is generally only useful when building tools that work with multiple databases, such as GUI database managers.
For more details refer to the below mysql portal for reference.
https://dev.mysql.com/doc/connector-j/en/connector-j-reference-configuration-properties.html

How can I find all the SQL updates run in a Java Connection (from transaction start to commit)?

How can I find all the SQL update queries run in a Java Connection (from transaction start to commit)?
You can't do that with a normal Connection.
You could create wrapper classes for the actual Connection/Statement classes, that would track which statements have been executed. A better idea would be to just enable logging of SQL if your driver supports it, otherwise you're just recreating an existing functionality.
Some other options (on database level):
Enable and configure AUDITING; or
Enable SQL tracing of the relevant database session(s)

Does Spring embedded database support different SQL dialects?

H2 has a range of compatibility modes for various other databases such as MS SQL Server, MySQL, Oracle, etc that support different SQL dialects. However, when setting up an embedded database in Spring I do not find any corresponding setting. Does this imply that I have to use "plain" SQL without any dialect specific features if I for example use Oracle in production and H2 during test? Have I overlooked something?
which version of H2 database? per the documentation, you can set compatible mode by SQL statement (http://www.h2database.com/html/features.html#compatibility)
SET MODE PostgreSQL
just add this statement into your first sql script file loaded by Spring jdbc embedded-database
According to the H2 doc, the Oracle compatibility mode is quite limited.
For instance, you can not use PL/SQL procedures.
If you use Spring's EmbeddedDatabase, you cannot set the compatibility mode as-is; you have to implement you own EmbeddedDatabaseConfigurer and specify the compatibility mode through the JDBC URL (see below).
But also, to use the compatibility mode with H2 and Spring, you just have to set the mode in your JDBC URL (so it is not Spring related) in a classic way, using a DataSource:
jdbc:h2:~/test;MODE=Oracle
And if you use Hibernate, you have to specify the Oracle dialect instead of the H2 one.
You have 2 options:
use spring to start the H2 database as follows (check setName() to see how to pass H2 specific URL parameters to spring builder):
Spring code generates the URL as follows:
String.format("jdbc:h2:mem:%s;DB_CLOSE_DELAY=-1", databaseName)
So, in setName() you can all any H2 specific parameter in the URL.
private DataSource dataSource() {
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
EmbeddedDatabase db = builder
.setType(EmbeddedDatabaseType.H2)
.setName("testdb;DATABASE_TO_UPPER=false;MODE=Oracle")
.addScript("schema.sql")
.addScript("data.sql")
.build();
return db;
}
configure directly the DB URL, sth like:
org.h2.jdbcx.JdbcDataSource dataSource = new org.h2.jdbcx.JdbcDataSource();
dataSource.setURL("jdbc:h2:testdb;MODE=MySQL;DATABASE_TO_UPPER=false;INIT=runscript from 'src/test/resources/schema.sql'\;runscript from 'src/test/resources/data.sql'");
The main different is that (2) is executing scripts defined at INIT for every database connection creation and not once per DB creation! This causes various issues, like INSERTs failing due to duplicate keys etc..

Categories

Resources