I'm building a spring boot application. I'm trying to enable SQL logs in application.yml. How can I do it?
We can use any one of these:
spring:
jpa:
show_sql: true
or
logging:
level:
org:
hibernate:
sql=debug
type:
descriptor:
sql:trace
Related
In my spring boot application, I have multiple profiles as below:
application.yml
application-dev.yml
application-local.yml
application-qa.yml
application-prod.yml
Each profile will have its own datasource configured. For testing purpose, same datasource configuration are passed in all profiles.
In application.yml if I mention spring.profiles.active=dev it is connecting to DB successfully. But if I mention spring.profiles.active=local then on server startup local profile is getting active with below error
***************************
APPLICATION FAILED TO START
***************************
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (the profiles lcl are currently active).
Below configuration passed in all profiles excluding application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
password: root
url: jdbc:mysql://localhost:3306/local_db?useSSL=false
username: root
jpa:
hibernate:
ddl-auto: update
show-sql: true
liquibase:
change-log: classpath:db/changelog/db.changelog-master.json
When trying to start a Spring Boot application for tests (with H2 database) with R2DBC and Liquibase configured, I get the following error:
2022-10-04 12:50:18.893 INFO 57774 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 67 ms. Found 2 R2DBC repository interfaces.
org.h2.message.DbException: Log file error: "/.testdb.trace.db", cause: "java.nio.file.FileSystemException: /.testdb.trace.db: Read-only file system" [90034-214]
Here's my config:
spring:
liquibase:
change-log: classpath:liquibase/db.changelog.xml
contexts: production
url: jdbc:h2:file:///./.testdb;MODE=MySQL
r2dbc:
url: r2dbc:h2:file:///./.testdb
h2:
console:
enabled: false
Is there anything I can do to fix that error?
The problem was caused by the URL, R2DBC requires it to look like this:
r2dbc:
url: r2dbc:h2:file:///./.testdb
But then if you try to copy it over to Liquibase part, the error appears. To fix that, remove forward slashes from Liquibase URL:
spring:
liquibase:
change-log: classpath:liquibase/db.changelog.xml
contexts: production
url: jdbc:h2:file:./.testdb;MODE=MySQL
Final version:
spring:
liquibase:
change-log: classpath:liquibase/db.changelog.xml
contexts: production
url: jdbc:h2:file:./.testdb;MODE=MySQL
r2dbc:
url: r2dbc:h2:file:///./.testdb
h2:
console:
enabled: false
Recently we upgraded to spring boot 2.2.5
We used to use the property spring.datasource.data to load SQL after "create" or "update" of the database.
Our Yaml looked like this:
--
spring:
profiles: default
application:
name:"EMBEDDED"
datasource:
url: "jdbc:postgresql://localhost/somedb"
driverClassName: org.postgresql.Driver
username: postgres
password: "some password"
data: "classpath*:data.sql"
jpa:
database: POSTGRESQL
show-sql: false
hibernate.ddl-auto: update
The important part is the "spring.datasource.data" property where we used to define a name of a SQL file that hibernate would run after updating the DB.
Note that we use the hibernate.ddl-auto to make that happen.
Now we have upgraded the spring boot and we also started using 2 data-sources as follows:
spring:
profiles: dev
application:
name:"EMBEDDED-DEV"
datasource:
src1:
jdbcUrl: "jdbc:sqlserver://localhost;databaseName=DB1"
username: user
password: password
initializationFailTimeout: 0
data: "classpath*:data.sql"
src2:
jdbcUrl: "jdbc:sqlserver://localhost;databaseName=db2"
username: user
password: password
initializationFailTimeout: 0
data: "classpath*:data.sql"
jpa:
database-platform: org.hibernate.dialect.SQLServer2012Dialect
show-sql: false
hibernate.format_sql: true
hibernate.ddl-auto: update
As you can see we have now 2 data sources, but when i try to add the "data" property as i used to do before i get this exception:
java.lang.RuntimeException: Property data does not exist on target class com.zaxxer.hikari.HikariConfig
Where should i put now the data property in order for it to work?
I have 3 environments in my application i.e dev, prod, test i for that i separate files like application-prod.yml,application-dev.yml,applicaton-test.yml and also application.properties i am running command -> mvn spring-boot:run -Dspring.profiles.active=prod but its taking profile as a default profile
application.properties
server.port=8002
application-prod.yml
--------------------------
spring:
jpa:
show-sql: true
properties:
hibernate: dialect:org.hibernate.dialect.Oracle10gDialect
ddl-auto: update
datasource:
username: scott
password: tiger
url: jdbc:oracle:thin:#localhost:1521:xe
driver-class-name: oracle.jdbc.OracleDriver
app:
message : This for DevEnvironment connect to Oracle
logging:
level:
org:
springframework:
web: DEBUG
If you are using maven command then try the following to set the profile :
mvn spring-boot:run -Dspring-boot.run.profiles=foo,bar
The above command sets the run time spring profile through the maven plugin.
Official doc.
When I invoke createClob method using connection object as shown below:
Clob clob = con.createClob();
Following exception is thrown:
Caused by: java.sql.SQLFeatureNotSupportedException: Method org.postgresql.jdbc.PgConnection.createClob() is not yet implemented.
at org.postgresql.Driver.notImplemented(Driver.java:659)
at org.postgresql.jdbc.PgConnection.createClob(PgConnection.java:1246)
at org.apache.commons.dbcp2.DelegatingConnection.createClob(DelegatingConnection.java:868)
at org.apache.commons.dbcp2.DelegatingConnection.createClob(DelegatingConnection.java:868)
I`m using database PostgreSQL 9.6.2 with JDK8 and using commons-dbcp2 connection pool, And added following Postgres dependency in pom.xml
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.1.1</version>
</dependency>
In class org.postgresql.jdbc.PgConnection, createClob implementation is as shown below which is throwing the exception:
#Override
public Clob createClob() throws SQLException {
checkClosed();
throw org.postgresql.Driver.notImplemented(this.getClass(), "createClob()");
}
What is the solution or workaround to overcome this issue? Or How can we set CLOB data in Postgres queries?
TL;DR
Set spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true in your application.yml or,
Set hibernate.jdbc.lob.non_contextual_creation=true in your persistence.xml
It's a known error in JBoss community.
This error appears in former versions and new version with Spring-Boot 2.0.0.RC1 as well and higher.
Solution:
Update your postgressql-driver with a newer backward compatible version.
Set spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true in your application.yml or,
Set hibernate.jdbc.lob.non_contextual_creation=true in your persistence.xml
If it's not working see this trick below:
The solution is to add this line in your property file (or something similar if your are not using spring)
spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults= false
So, your application.yml should looks like:
spring:
application:
name: employee-service
datasource:
url: jdbc:postgresql://localhost:5432/db_development
platform: POSTGRESQL
username: ...
password: ...
jpa:
hibernate:
ddl-auto: create-drop
dialect: org.hibernate.dialect.PostgreSQL9Dialect
show_sql: true
properties.hibernate.temp.use_jdbc_metadata_defaults: false
server:
port: 8080
Reference:
https://o7planning.org/en/11661/spring-boot-jpa-and-spring-transaction-tutorial
hibernate with c3p0: createClob() is not yet implemented
Thanks to Binakot for his comment bellow. I have updated the post.
put this in to application.properties
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
PostgreSQL doesn't really have "CLOB". Just use setString(String) or setObject(...) with Types.STRING.
using spring-boot 2.1.9.RELEASE I added the following and it worked
spring:
jpa:
properties.hibernate.temp.use_jdbc_metadata_defaults: false
database-platform: org.hibernate.dialect.PostgreSQL94Dialect