How can I specify the current schema on quarkus datasource? - oracle

Hi I am trying to configure the quarkus to connect to a oracle database. With the current configuration I am able to connect to the database, but I cannot specify the current schema.
I followed the documentation and try to use the new-connection-sql to set the current schema. But it doesn't seems to work.
quarkus.datasource.mydatasource.new-connection-sql=ALTER SESSION SET CURRENT_SCHEMA=SCHEMA_NAME
Here is my application.properties file
quarkus.datasource.mydatasource.db-kind=oracle
quarkus.datasource.mydatasource.jdbc.driver=oracle.jdbc.driver.OracleDriver
quarkus.datasource.mydatasource.jdbc.url=jdbc:oracle:thin:#(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=10.15.73.140)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=SN)))
quarkus.datasource.mydatasource.jdbc.min-size=3
quarkus.datasource.mydatasource.jdbc.max-size=20
quarkus.datasource.mydatasource.username=username
quarkus.datasource.mydatasource.password=password
quarkus.datasource.mydatasource.new-connection-sql=ALTER SESSION SET CURRENT_SCHEMA=SCHEMA_NAME
What could be the issue here?
Thank you.

This is working fine if you add the jdbc sub path name to the property
quarkus.datasource.mydatasource.jdbc.new-connection-sql=ALTER SESSION SET CURRENT_SCHEMA=SCHEMA_NAME
You can refer to these Quarkus configuration references:
https://quarkus.io/guides/datasource#quarkus-agroal_quarkus.datasource.-datasource-name-.jdbc.new-connection-sql
https://quarkus.io/guides/datasource#quarkus-agroal_quarkus.datasource.jdbc.new-connection-sql

You could try to set the schema in the connection url. But what you are trying to archieve, basically routing each user request to a specific schema, you should check the hibernate multitenancy support by this means, you can route each request to the database you want, but beware of the limitations regarding the parameters you can work with to know where to route your request.
Also check hibernate catalog and schema configuration parameters

Related

How to set up a connection between spring boot project and PostgresSQL DB

I created a spring boot project and I link it with my DB under PgAdmin.
I've modified the application.properties correctly and created a class named "user" and its repository "userRepository" but I don't know if this will automaticly create the user table in the DB (I used annotations) or I have to create it in PgAdmin ?
Do I need to specify any controller or webController if I want to have Crud operations ? And where does the CrudRepository exists do I need to generate it and how ?
Excuse me this my first experience with spring, I will really appreciate if you guys can help me.
Thanks in advance.
For your first question:
I don't know if this will automaticly create the user table in the DB
You have several ways how to create your tables, for example with property in application.properties file
spring.jpa.hibernate.ddl-auto = [none|validate|update|create-drop]
where none means this configuration not affect existing database
update means updating changes in database
create-drop means creating all tables from scratch according your project entities classes.
You can check this documentations for additional information
https://docs.spring.io/spring-boot/docs/1.1.0.M1/reference/html/howto-database-initialization.html
59.2 Initialize a database using Hibernate
also you can check how to initialize database with existing sql scripts files in documentation above
59.3 Initialize a database using Spring JDBC
For question: Do I need to specify any controller or webController if I want to have Crud operations ? And where does the CrudRepository exists do I need to generate it and how ?
Yes you should create controllers to handle requests and repositories to work with JPA.I also suggest check tutorial first, for example this one (first in google)
https://bezkoder.com/spring-boot-jpa-crud-rest-api/

jdbc data independence without using hibernate api?

I am trying to solve problem by using properties file but in properties file we can handle only database driver problem.if we want to mysql to oracle database need to change all my query.the problem is now how to make query independent without hibernate api?? please suggest

Change the Hibernate database schema at run time in spring boot application

I have A requirement where the current schema to be used is stored in db table;(schema_a or schema_b).
The application is loaded with default schema
spring.jpa.properties.hibernate.default_schema=schema_a
Now when the data in table is updated to B I want to consider all jpa query to use schema_b without any down time at server.
Your question is not clear enough to provide some code to your question, but you can catch inspiration from the following articles:
https://spring.io/blog/2007/01/23/dynamic-datasource-routing/
Spring Boot - Change connection dynamically

How to pass database name explicitly in Spring Boot Configuration?

I've a Spring Boot application of mine, which connects to a Postgres database. I've specified in application.properties the datasource url as -
spring.datasource.url=jdbc:postgresql://< server ip here >:5432/mydb
The jdbc url (jdbc:postgresql://< server ip here >:5432/) is actually stored in a separate external location which my application is able to read. Therefore, I want to specify only the database name in my properties file.
I don't want to pass the database name as some environment variable since it's not going to change.
I'm stuck at this point for quite some time now, how can I achieve the same?
Add this in your application.properties file
spring.jpa.hibernate.ddl-auto=create\update\none
spring.datasource.url=jdbc:postgresql://host:port/db
spring.datasource.username=username
spring.datasource.password=password
Have you tried using ${var} syntax like:
spring.datasource.url=jdbc:postgresql://${server-ip}:5432/mydb
See:
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-placeholders-in-properties
I finally implemented it this way.
Specified only database name in my application and created Datasource bean in a separate Spring Boot application (so that it can be reused across other projects as well).

Dynamic data source routing in spring

I've single DB with three identical schema in PostgreSQL.
Now I need to select particular schema for DB operation based on locale-key (store in user session). I found somewhere that this thing is similar to dynamic data source routing.
Anyone have any idea about How to implement this in Spring?
Will this effect transaction management in anyway?
Please do share any sample code if possible.
Any suggestion would be appreciated.
Thanks & Regards.
I suggest using this approach:
First define a locale interceptor either session-based or change interceptor in your Spring MVC configuration.
You can now use LocaleContexHolder to fetch the current locale on the attached thread.
Use the reference blog post to define your dynamic routing data source. The parameter on your data source router will come from locale. Use LocaleContextHolder to determine the locale then use it to determine which data source should be used.
If you have single DB, then the dynamic aspect should not be related to connection pooling - all connections are still for single database. All you need to do is to dynamically set proper schema after starting the transaction.
This may be achieved using some aspect with order higher than <tx:annotation-driven />. In this aspect you should acquire current connection:
DataSourceUtils.getConnection(dataSource)
and issue the following PostgreSQL statement (see: http://www.postgresql.org/docs/9.1/static/sql-set.html for information about schema parameter);
set schema 'schemaname-on-the-basis-of-session-parameter';
See also using schemas in postgres.
As for the transaction management - transactions are related to physical connections and sessions. Schemas on the other side are kind of namespaces, so you don't have to change transaction management, just set current schema at the beginning of each transaction during user request processing.

Resources