How to read and write data from multiple databases by using spring batch update? - spring

I am working on spring batchupdate ,I search on google I didn't find any solution for my problem.
I have two databases(MySQL,ORALCE) I want to read data from mysql and write into oracle by using batch update .

Your problem is unclear.
You can first read the data from MySQL with one Spring JdbcTemplate object initialized with MySql data source, and then use another JdbcTemplate object, initialized with Oracle data source, to write the data.
If you want to do it in one transaction, you will have to use distributed transactions/XA libraries, such as Atomicos, and Spring distributed transaction manager. See here https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-jta.html for details on Spring integration with distributed transactions libraries.

Related

What is the best way to maintain queries in Spring boot application?

In My Application, Using the below technologies
Spring boot 2.7.x
Cassandra
spring batch 5. x
java 11
As part of this, I need to extract data from the Cassandra database and need to write out the file
so here I need to use queries to fetch data so
just want to know what is the best way to maintain all queries at one place so any query changes come in the future, I shouldn't build the app rather just need to modify the query.
Using a repository class is necessary. If you are using JPA i recommend using a repository for each Entity class. With JDBC it is possible to create a single repository which contains all the queries. To access the query methodes i would use a service class. In this way your code is structured well and maintainable for future changes.

what is the best way to create session from java spring boot using oracle Database?

I created user in oracle database and I am trying to create session but I find many ways in spring boot so what is the easy way if I want to create classe connections using the Username and Password ?
You can jdbc template, spring data JDBC or spring data JPA, well depending on your use case.
If your data model is quite complex, you should avoid using the JDBC template as you will need to write prepared statements which can be cumbersome. JPA will allow you to use object-oriented programming principles and also will help you map the entities to your database columns.
For example, if you are going to use spring data JPA, you need to set the application properties as follows:
spring.datasource.type=oracle.oracleucp.jdbc.UCPDataSource
spring.datasource.oracleucp.connection-factory-class-name=oracle.jdbc.pool.OracleDataSource
spring.datasource.oracleucp.sql-for-validate-connection=select * from dual
spring.datasource.oracleucp.connection-pool-name=UcpPoolBooks
spring.datasource.oracleucp.initial-pool-size=5
spring.datasource.oracleucp.min-pool-size=5
spring.datasource.oracleucp.max-pool-size=10
This would behind the scene create an Oracle Datasource. In this example, we are using Oracle Universal Connection Pooling. You can also use HikariCP which is quite popular.
check this out
If you want to use UCP with above properties then you must have SpringBoot version higher than 2.4.0.
Check out the Spring Boot code sample on GitHub.

Creating tables on fly with Spring Boot Data JPA

Trying to build application where tables & its fields are managed by master table & creation of tables & its fields happens on demand, on fly based on user data posted to server.
Tried to look over similar question like this but wasnt able to find clue how to execute dynamic queries in DB without creating Repository & Entity in spring boot.
Robert Niestroj is correct when he writes in the comments
If you have dynamic tables JPA is the wrong approach. JPA is for static schema
I guess in theory you could do something where you generate code and possibly restart your ApplicationContext but it is bound to be really painful and you won't benefit from using JPA.
You should look into more dynamic technologies. MyBatis and plain old JdbcTemplate or NamedParameterJdbcTemplate come to my mind.
Possibly with your own abstraction layer on top of it if you have recurring scenarios.

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

Spring batch uses relational database to store job metadata

I am aware of spring batch metadata tables are written to Relational database tables say MySQL,H2 etc. But my question is whether spring batch metadata tables can be written to elasticsearch . If so how to proceed further?
have you checked the Spring Batch Extensions module? This module provides an ItemReader and an ItemWriter for interacting with Elasticsearch. Hope it helps.

Resources